Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: FortressCaulfield on Sun 09/06/2024 17:44:41

Title: Arrays of structs
Post by: FortressCaulfield on Sun 09/06/2024 17:44:41
I'm trying to have an encyclopedia feature where the char can look things up from a listbox that populates through the game as she encounters various things.

The listbox feature works fine, though it would be nice to have options to sort it, but I'm coming up empty on how to get the game to match the definitions.

In my mud days, I'd have used an array of structs, like so:

struct IGEntry {
  String name;
  String desc;
};

IGEntry IGList[2] = {
{"Elf", "Elves are known for their long beards and fondness for mining."},
 {"Dwarf", "Dw...orf? I'm not sure that's a real word. Are you making things up?"}};

Then when the player clicks an item from the list I'd use a while loop to compare the text to each "name" in the list and then display the corresponding text. AGS hates this idea and won't let me declare a defined array like this.

When I try to declare each item one at a time, like this

IGList[0] = {"Blah","Blah"};

I get an error "unexpected IGList". As far as I can tell, I'm copying the syntax exactly from the manual. What's the problem?

Also, is there just an easier way to do this I'm missing?
Title: Re: Arrays of structs
Post by: Crimson Wizard on Sun 09/06/2024 17:56:22
Unfortunately, AGS script does not support this classic syntax of array initialization.

So, instead you would have to do it hard way, like:
Code (ags) Select
IGList[0].name = "Elf";
IGList[0].desc= "Elves are known for their long beards and fondness for mining.";
// etc

Regarding better alternatives for storage, AGS script supports a Dictionary type that seems to be matching your purpose:
https://adventuregamestudio.github.io/ags-manual/Dictionary.html


Also, an alternate way to initialization could be having these texts in a separate text file and read back using File script API. Optionally, such file could be also packed inside game package, with the use of a option in General Settings called "Package custom data folder(s)":
https://adventuregamestudio.github.io/ags-manual/GeneralSettings.html#compiler
https://adventuregamestudio.github.io/ags-manual/File.html

Reading from a file has its pros and cons, but I think the advantage here is that you may script a simpler entry initialization in a loop, instead of writing many lines of "[N].name = "abcdefg"; in script", and a separate text file may be modified without recompiling a script.
For example, you may have a file directly put into a game folder for the time of developing a game, and told to be packed inside later when you distribute it.
Title: Re: Arrays of structs
Post by: FortressCaulfield on Sun 09/06/2024 22:59:45
Thanks but I tried that and I got the same "unexpected IGList" error.

I ended up scrapping it and just doing a switch function for the gui list that houses the thing but I'd like to solve the problem anyway for potential future use.
Title: Re: Arrays of structs
Post by: eri0o on Sun 09/06/2024 23:13:53
What's your code? What AGS version?
Title: Re: Arrays of structs
Post by: Crimson Wizard on Sun 09/06/2024 23:16:10
Quote from: FortressCaulfield on Sun 09/06/2024 22:59:45Thanks but I tried that and I got the same "unexpected IGList" error.

Are you doing this outside of functions by a chance? AGS only allows to assign at variable declaration for trivial types, such as int and float.
Assigning struct members and objects are commands, and must be inside some function. If this is a one time initialization, then usually inside "game_start".
Title: Re: Arrays of structs
Post by: FortressCaulfield on Mon 10/06/2024 00:11:03
yeah that must be it. I assumed it'd be like a basic variable declaration