Arrays of structs

Started by FortressCaulfield, Sun 09/06/2024 17:44:41

Previous topic - Next topic

FortressCaulfield

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:

Code: ags
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?
"I can hear you! My ears do more than excrete toxic mucus, you know!"

-Hall of Heroes Docent, Accrual Twist of Fate

Crimson Wizard

#1
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
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.

FortressCaulfield

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.
"I can hear you! My ears do more than excrete toxic mucus, you know!"

-Hall of Heroes Docent, Accrual Twist of Fate

eri0o

What's your code? What AGS version?

Crimson Wizard

#4
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".

FortressCaulfield

yeah that must be it. I assumed it'd be like a basic variable declaration
"I can hear you! My ears do more than excrete toxic mucus, you know!"

-Hall of Heroes Docent, Accrual Twist of Fate

SMF spam blocked by CleanTalk