Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Ryan Timothy B on Fri 06/05/2011 17:00:54

Title: Dynamic Array of Struct?
Post by: Ryan Timothy B on Fri 06/05/2011 17:00:54
I could've sworn you can make a dynamic struct array? I'm 90% certain I had done it before.


struct sTest {
  Character *theCharacter;
};
sTest test[];

function game_start()
{
  test = new sTest[Game.CharacterCount];
}


Results in the error: cannot create dynamic array of unmanaged struct
Title: Re: Dynamic Array of Struct?
Post by: Khris on Fri 06/05/2011 17:59:38
http://www.adventuregamestudio.co.uk/manual/DynamicArrays.htm  ;D

QuoteAlso, at present you can create arrays of basic types (int, char, etc) and of built-in types (String, Character, etc) but not of custom structs.
Title: Re: Dynamic Array of Struct?
Post by: Ryan Timothy B on Fri 06/05/2011 18:10:02
I did read that part in the manual. But like I said.. I was 90% certain I had done it before because of a thread I made about how I couldn't use Game.InventoryItemCount to define the indexes in an array struct. But then ended up figuring it out... Or maybe I found another solution. Now I'm trying to remember what it was that I did (this was a year ago. lol).

Oh well. Maybe I'm remembering wrong.
Title: Re: Dynamic Array of Struct?
Post by: Ryan Timothy B on Fri 06/05/2011 18:29:29
Oi. I think I remember what I had done.

I Defined a value that was equal to the amount of InventoryItems, and on GameStart I had it check whether Game.InventoryItemCount was equal to the Defined value. If it wasn't, I aborted the game.

It was much better than an unfriendly crashing glitch or having an array with an arbitrary value of 1000 or something.
Title: Re: Dynamic Array of Struct?
Post by: monkey0506 on Fri 06/05/2011 19:50:14
An alternative would be to use dynamic arrays outside of the struct and simply use a struct interface (specifically, struct functions) for getting/setting the items you need. I've essentially done this in my Verbcoin module which also uses the unsupported concept of indexers..but the official word would probably be that I shouldn't be using attributes like that, so I won't recommend doing that. Anyway, managing dynamic arrays that are associated with a struct yet exist outside of said struct is messy business, so it's debatable whether you would want to do that either.
Title: Re: Dynamic Array of Struct?
Post by: JoelCBarker on Fri 24/04/2015 21:36:32
Sorry to bump something four years old, but this seems like the spot to ask my question.

In my global script I declare a normal array, then in my global script header I define it as a struct of integers. That works fine as a fixed struct array (different room scripts assign values to the integers depending on the player's current room)...

...but now I want to make it a dynamic array whose size and values are reassigned by each room. I could declare a different fixed struct array in each room, but I was really hoping I could keep the info it stores in my global script so I can export and use it in other scripts. I can't use global variables because the size of the array ranges from 400 to 6400.

I just want a struct array whose size can be changed by each room, without losing the ability to use its values in other scripts or a workaround.

Quote from: monkey_05_06 on Fri 06/05/2011 19:50:14
use dynamic arrays outside of the struct and simply use a struct interface (specifically, struct functions) for getting/setting the items you need

the contents of the array are character values being parsed from strings, so during parsing I would store each value one at a time in a global variable and then (?)pass it(?) to a dynamic array within my global script?

I hope that makes sense, I'm really tired and I can barely remember what problem I had in the first place. Cannot create dynamic array of unmanaged struct.

Thanks!
Title: Re: Dynamic Array of Struct?
Post by: Monsieur OUXX on Wed 29/04/2015 08:39:07
Quote from: monkey_05_06 on Fri 06/05/2011 19:50:14
An alternative would be to use dynamic arrays outside of the struct and simply use a struct interface (specifically, struct functions) for getting/setting the items you need.

Yes, it's the closest thing you have to an array of structs.
Title: Re: Dynamic Array of Struct?
Post by: JoelCBarker on Wed 29/04/2015 13:12:07
I slept on it and changed my approach, there wasn't a real need for it but at the time I thought it might be a tidier way to do it. Thanks for responding however :-D