Combining Structs and Arrays - Coding

Started by None, Mon 14/05/2012 05:01:43

Previous topic - Next topic

None

Okay so I created an external script, and wanted to declare a bunch of variables that I could access later,
I thought I followed the AGS manual nearly verbatim, but apparently I'm missing something; I get:
QuoteSpellFinder.asc(18): Error (line 18): Parse error: unexpected 'spells'

Here's the code:

Code: ags

struct Spell {
  String name;
  int tool;
  int element;
  int incantation;
  int mark; 
  int gem;
};
//Declare Spells
Spell spells[10]; //Total Number of Spells
//Tool (1 - Wand, 2 - Athame)
//Element (1 - Earth, 2 - Water, 3 - Fire, 4 - Air)
//Incantation ( 1 - Whispers of Wisdom, 2 - Song of Strength, 3 - Words of Power, 4 - Call of Destiny)
//Mark (1 - The Hand of Glory, 2 - The Eternity Loop, 3 - The Pentacle Star, 4 - The Three Fates)
//Gem Color (1 - Ruby, 2 - Sapphire, 3 - Emerald, 4 - Diamond, 5 - Obsidian)

//Declare Spell 0 - Air of Destiny
spells[0].name = "Air of Destiny";
spells[0].tool = 1;
spells[0].element = 4;
spells[0].incantation = 4;
spells[0].mark = 4;
spells[0].gem = 4;


I declare 9 more spells using the same method above, but I didn't see any point including anything past the first one.
Get back to me soon guys, danke.
Edit by Andail: fixed ags code format

Ryan Timothy B

With AGS you cannot declare the initial value of a struct it must be within a function. So you should likely be doing this within the SpellFinder  game_start  function.

Code: ags

function game_start() {
  spells[0].name = "Air of Destiny";
  spells[0].tool = 1;
  spells[0].element = 4;
  spells[0].incantation = 4;
  spells[0].mark = 4;
  spells[0].gem = 4;
}


monkey0506

Keep in mind that in AGS assignments always have to be inside a function so it knows when you want the code to be run. Integer-based types (bool, char, short, int, and any enum type) and floats can be initialized as you might do in other languages, but pointer types (including String), arrays, and struct members can't be initialized directly (unless in the case of a pointer/String it is being defined within a function).

None

Okay now, I have created a custom struct called 'Spell' like so:
Code: ags

struct Spell {
  String name;
  int tool;
  int element;
  int incantation;
  int mark; 
  int gem;
};

Now I use this to create three different structs:
Code: ags

Spell spells[10]; //Total Number of Spells
Spell plspell[1]; //Page Left Spell
Spell prspell[1]; //Page Right Spell

Then in a function call later I tried this:
Code: ags

plspell[0] = spells[0];
prspell[0] = spells[1];

That's when the editor gives me this error:
Quote
SpellFinder.asc (184): Error (line 184): cannot assign to 'plspell'

Do I have to assign each value of the struct to the other individually???
e.g.
Code: ags

plspell.name = spells[0].name;


Can't find anything on this in the provided documentation... Need...data... can't...compute... bzzzt.  :P

monkey0506

structs in AGS are a bit more primitive than that, there's not really any way to implement constructors.

If this is your entire implementation then you could add a member function like this:

Code: ags
struct Spell
{
  String name;
  int tool;
  int element;
  int incantation;
  int mark;
  int gem;
  import void Copy(int spellID);
};

Spell spells[10];
Spell plspell; // what's the point of a 1 element array?
Spell prspell;

void Spell::Copy(int spellID)
{
  if ((spellID < 0) || (spellID >= 10)) return;
  this.name = spells[spellID].name;
  this.tool = spells[spellID].tool;
  this.element = spells[spellID].element;
  this.incantation = spells[spellID].incantation;
  this.mark = spells[spellID].mark;
  this.gem = spells[spellID].gem;
}


Alternatively you could use other methods, such as serializing the data into a String and then unserializing it (but this is a lot less straight forward for your implementation).

Code: ags
// usage
plspell.Copy(5); // plspell = spells[5];

None

Thanks, took me reading over it a few times to figure out exactly what all your code does, but I think I understand it.  Learning...Learning...Learning.
The one element arrays were just in case there was something about the struct types, such that they had to match EXACTLY. I tend to try everything before I ask for help, no reason to clog up the forums. Thanks and hopefully I won't need you guys for awhile. XD

Monsieur OUXX

Quote from: charlescreations on Tue 15/05/2012 05:28:13
Thanks, took me reading over it a few times to figure out exactly what all your code does, but I think I understand it

What monkey told you is that in AGS script you can't do this: myStruct1 = mystruct2. You can only do this : Mystruct1.member1 =  Mystruct2.member1,  Mystruct1.member2 =  Mystruct2.member2, etc. (for each member of the structure).
...and then, if you feel like it, gather those instructions in a "copy" function, once and for all.
 

monkey0506

Yes, and since AGS doesn't support pointers (or references) to custom struct types, you can't pass them into a function and have to reference a global array...or serialize the data. Or other methods. (roll) In any case, yes, this is just a member function that copies each member from a global instance in the array into one of your other instances (such as plspell).

SMF spam blocked by CleanTalk