Problem with structs - SOLVED

Started by Eggie, Wed 22/07/2009 21:57:13

Previous topic - Next topic

Eggie

Well, here's the code I used:

struct NewCharacter {
 int health;
 int maxhealth;
 int strength;
 int defense;
 int speed;
 int time;
 String type;
};

NewCharacter shelley;
 shelley.health = 100;
 shelley.maxhealth = 100;
 shelley.strength = 10;
 shelley.defense = 8;
 shelley.speed = 10;
 shelley.time = 1000;
 shelley.type = "Human";



And here's the error I get when I try and build the game:

characters.asc(15): Error (line 15): Parse error: unexpected 'shelley'


Out of curiosity I pasted in the example code from the manual, which goes:

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

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



Same thing happened.

Any idea what could be causing this?

GuyAwesome

Where exactly have you put that code? AFAIK variables, including structs, have to be set in a function or at declaration, which isn't really practical with Structs... Just having them 'loose' in the script (as it looks like you've got) would probably kick up that error. The 'structs and arrays' example kind of mentions this, the one you quoted obviously doesn't but it still applies.

Khris

Exactly, this should work:

Code: ags
// global header

struct NewCharacter {
  int health;
  int maxhealth;
  int strength;
  int defense;
  int speed;
  int time;
  String type;
};


// global script

NewCharacter shelley;

function game_start() {
  shelley.health = 100;
  shelley.maxhealth = 100;
  shelley.strength = 10;
  shelley.defense = 8;
  shelley.speed = 10;
  shelley.time = 1000;
  shelley.type = "Human";
}

Eggie

Okay, I put it in game_start and it's all fine now. Thanks for the help.

TerranRich

All script that performs an action (even declaring a new instance of a struct) must go somewhere to be triggered, not just floating around in script-limbo. ;)
Status: Trying to come up with some ideas...

monkey0506

Actually you can declare the instance anywhere after the struct definition. However if you put the declaration inside a function it will only be available within that function. The same rules apply to any pointer and dynamic arrays.

You can put this at the top of the GlobalScript.asc for example:

Code: ags
MyStruct my_var;
String my_string;
Character *my_character;
int my_array[];


However the lines where you assign values must be inside a function:

Code: ags
function game_start() {
  my_var.my_int = 42;
  my_string = "The meaning of life, the universe, and everything you say?";
  my_character = cEgo;
  my_array = new int[Game.CharacterCount];
}


These rules don't apply to ints, floats, bools, or any enumerated types which at declaration may have a value assigned. Once the variable is defined however, any other changes to the value must again be made inside a function.

Again if the variable is defined within the function it will stop existing at the end of the function. So just keep these rules in mind and you should be good! ;)

SMF spam blocked by CleanTalk