Beginner's problem with variables and struct (solved)

Started by Manu, Fri 04/06/2021 12:44:25

Previous topic - Next topic

Manu

I'm trying to understand AGS scripting language a bit better, and I'm following this guide: https://adventuregamestudio.github.io/ags-manual/ScriptKeywords.html

I declared this struct in GlobalScript.ash:
Code: ags

struct Weapon {
  int damage;
  int price;
  String name;
};


and then I added these lines to the room1.asc
Code: ags

Weapon sword;
sword.damage = 10;
sword.price = 50;
sword.name = "Fine sword";


But I receive a Parse error "unexpected 'sword'".

Can you tell me what's wrong with the code? The initial version was different, but then I decided to copy and paste from the guide, to see what happens, but it doesn't work.
Thaks!

Crimson Wizard

Unlike plain variables of basic types, member of the struct cannot be set outside of the function.

fernewelten

#2
You can declare a global variable, by putting "Weapon sword;" outside of any functions. But the rest of the lines that you quoted need to go within the body of some function.

So let's say you create an event handler that is called whenever the player enters the room after fade-in. (How:
- In the "Explorer" pane tree, navigate to your room, 
- doubleclick "Edit room",
- in the first field of the "Properties" pane below the "Explorer" pane, select "[Your room name] (Room; Number [number of your room]",
- hit the Flash icon of the "Properties" pane,
- put the cursor in the field that is to the right of "Enters room after fade-in",
- click on the "..." icon on the right-hand side.)

You'll find a stub in your room editor window:
Code: ags

function room_AfterFadeIn()
{

}


Now you can complete that stub as follows:
Code: ags

Weapon sword;
function room_AfterFadeIn()
{
    sword.damage = 10;
    sword.price = 50;
    sword.name = "Fine sword";
}


Now, whenever the player enters the room and the room has faded in, then the function "room_AfterFadeIn" will be called and so the fields of your variable "sword" will be set to values that you assign.

Crimson Wizard

TBH I'd rather recommend using "before fade-in", which corresponds to "room load" event. That seems more logical, and will also prevent any potential glitches if these variables are displayed somewhere on screen, as "after fade-in" is called after room is already displayed for a while (while transition is performing).

Manu

Now I get it. The point is you cannot place statements outside functions, I guess because AGS doesn't know when to run them. It totally makes sense. Declaration of variables is ok, but not an assignment.
In fact, it's not true that you can assign basic types, also something like "variable = 1;" doesn't work.

Thanks!

Crimson Wizard

#5
Quote from: emabolo on Fri 04/06/2021 14:21:24
In fact, it's not true that you can assign basic types, also something like "variable = 1;" doesn't work.

I was not elaborate enough in my previous comment, thing is, you can assign basic types if you put assignment in the same line with the declaration:
Code: ags

int variable = 1;

This is not run as a script command, but instead these values are written in the script variable memory and loaded along with the script.

SMF spam blocked by CleanTalk