Author Topic: Combining Structs and Arrays - Coding  (Read 600 times)  Share 

charlescreations

  • I'm not back, I never left, it's called lurking.
Combining Structs and Arrays - Coding
« on: 14 May 2012, 05:01 »
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:
Quote
SpellFinder.asc(18): Error (line 18): Parse error: unexpected 'spells'

Here's the code:

Code: Adventure Game Studio
  1. struct Spell {
  2.   String name;
  3.   int tool;
  4.   int element;
  5.   int incantation;
  6.   int mark;
  7.   int gem;
  8. };
  9. //Declare Spells
  10. Spell spells[10]; //Total Number of Spells
  11. //Tool (1 - Wand, 2 - Athame)
  12. //Element (1 - Earth, 2 - Water, 3 - Fire, 4 - Air)
  13. //Incantation ( 1 - Whispers of Wisdom, 2 - Song of Strength, 3 - Words of Power, 4 - Call of Destiny)
  14. //Mark (1 - The Hand of Glory, 2 - The Eternity Loop, 3 - The Pentacle Star, 4 - The Three Fates)
  15. //Gem Color (1 - Ruby, 2 - Sapphire, 3 - Emerald, 4 - Diamond, 5 - Obsidian)
  16.  
  17. //Declare Spell 0 - Air of Destiny
  18. spells[0].name = "Air of Destiny";
  19. spells[0].tool = 1;
  20. spells[0].element = 4;
  21. spells[0].incantation = 4;
  22. spells[0].mark = 4;
  23. spells[0].gem = 4;
  24.  

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
« Last Edit: 14 May 2012, 08:38 by Andail »
~ The Evolution of Roger ~

Re: Combining Structs and Arrays - Coding
« Reply #1 on: 14 May 2012, 05:09 »
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: Adventure Game Studio
  1. function game_start() {
  2.   spells[0].name = "Air of Destiny";
  3.   spells[0].tool = 1;
  4.   spells[0].element = 4;
  5.   spells[0].incantation = 4;
  6.   spells[0].mark = 4;
  7.   spells[0].gem = 4;
  8. }
  9.  

charlescreations

  • I'm not back, I never left, it's called lurking.
Re: Combining Structs and Arrays - Coding
« Reply #2 on: 14 May 2012, 05:14 »
Thank you! That fixed it!
~ The Evolution of Roger ~

monkey_05_06

  • AGS Project Admins
  • #1 Straight Basher
Re: Combining Structs and Arrays - Coding
« Reply #3 on: 14 May 2012, 05:36 »
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).
By and large I didn't accomplish what I set out to do, but I did accomplish a fair bit. So, there is that.

charlescreations

  • I'm not back, I never left, it's called lurking.
Okay now, I have created a custom struct called 'Spell' like so:
Code: Adventure Game Studio
  1. struct Spell {
  2.   String name;
  3.   int tool;
  4.   int element;
  5.   int incantation;
  6.   int mark;
  7.   int gem;
  8. };
  9.  
Now I use this to create three different structs:
Code: Adventure Game Studio
  1. Spell spells[10]; //Total Number of Spells
  2. Spell plspell[1]; //Page Left Spell
  3. Spell prspell[1]; //Page Right Spell
  4.  
Then in a function call later I tried this:
Code: Adventure Game Studio
  1. plspell[0] = spells[0];
  2. prspell[0] = spells[1];
  3.  
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: Adventure Game Studio
  1. plspell.name = spells[0].name;
  2.  

Can't find anything on this in the provided documentation... Need...data... can't...compute... bzzzt.  :P
« Last Edit: 15 May 2012, 04:58 by charlescreations »
~ The Evolution of Roger ~

monkey_05_06

  • AGS Project Admins
  • #1 Straight Basher
Re: Combining Structs and Arrays - Coding
« Reply #5 on: 15 May 2012, 05:14 »
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: Adventure Game Studio
  1. struct Spell
  2. {
  3.   String name;
  4.   int tool;
  5.   int element;
  6.   int incantation;
  7.   int mark;
  8.   int gem;
  9.   import void Copy(int spellID);
  10. };
  11.  
  12. Spell spells[10];
  13. Spell plspell; // what's the point of a 1 element array?
  14. Spell prspell;
  15.  
  16. void Spell::Copy(int spellID)
  17. {
  18.   if ((spellID < 0) || (spellID >= 10)) return;
  19.   this.name = spells[spellID].name;
  20.   this.tool = spells[spellID].tool;
  21.   this.element = spells[spellID].element;
  22.   this.incantation = spells[spellID].incantation;
  23.   this.mark = spells[spellID].mark;
  24.   this.gem = spells[spellID].gem;
  25. }

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: Adventure Game Studio
  1. // usage
  2. plspell.Copy(5); // plspell = spells[5];
By and large I didn't accomplish what I set out to do, but I did accomplish a fair bit. So, there is that.

charlescreations

  • I'm not back, I never left, it's called lurking.
Thanks.
« Reply #6 on: 15 May 2012, 05:28 »
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
~ The Evolution of Roger ~

Monsieur OUXX

  • Mittens Serf
  • Mittens Half Initiate
    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Re: Thanks.
« Reply #7 on: 15 May 2012, 14:49 »
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.

monkey_05_06

  • AGS Project Admins
  • #1 Straight Basher
Re: Combining Structs and Arrays - Coding
« Reply #8 on: 15 May 2012, 17:49 »
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).
By and large I didn't accomplish what I set out to do, but I did accomplish a fair bit. So, there is that.