Because you can't make dynamic arrays of unmanaged struct

Started by Baguettator, Fri 24/11/2023 19:10:41

Previous topic - Next topic

Baguettator

Hi !

I want to find the easiest way to code that. I was sad when I understood that I can't make a dyanmic array of a unmanaged struct :

Code: ags
struct Item
{
  String name;
};

Item items[]; // Game Editor allows declaring that without crashing
items = new Item [50]; // Game Editor doesn't allow it and crashes !

So,my goal was to have a struct like this :

Code: ags
struct Map
{
  String titlecode;
  String titleFR;
  String titleEN;
  bool categories[17]; // true if the map is in that category
};

Map maps[];

maps=DynamicArray_AddMap(); // My custom function that adds a new "raw" for this dynamic array, I made it also for strings, int etc...

I want dynamic arrays because I allow the player to create his own maps, and so I don't want to :

- limit the player to X maps
- take too much memory for the game if I make a limit like "Map maps [1000]", that couldn't be reached but is not needed too...

As I can't make dynamic array of my struct Map, how would you do that ?

Any help appreciated :)

Crimson Wizard

#1
Keeping the script syntax problem separate, is there an actual point in having all Maps in memory at once?

The usual solution is to store map data in files, and load only 1 map at a time. This way you only need 1 Map struct object in script.


Regarding the scripting issue, I might suggest trying AGS 4 which has more scripting abilities:
https://www.adventuregamestudio.co.uk/forums/ags-engine-editor-releases/ags-4-0-early-alpha-for-public-test/

But even with more script features, I still strongly recommend to find optimal ways of storing data, and only create things that are needed right now.

eri0o

I wrote something here but when I posted it disappeared, so I will summup with have you tried ags dictionaries?

Baguettator

The main goal is to access to some of their data quicker than using "File.Open" hundred of times. All of their data is stored in .txt files, but I want to sort maps at a moment and accept those the player can play and those the player can't. So it's quicker if I remember once (when the game is launching the first time) some of their data instead of doing "File.Open, ReadRawLineBack" hundred of times to find their data and sort them.

Crimson Wizard

#4
Quote from: Baguettator on Fri 24/11/2023 19:25:36All of their data is stored in .txt files, but I want to sort maps at a moment and accept those the player can play and those the player can't.

Ah, I'm sorry, I now see why do you need this.

Because in AGS 3 you cannot have Strings in managed structs, and only managed structs in dynamic arrays, the existing solutions are:
1) Have each parameter in a separate dynamic array.
2) Create a dynamic array of Strings, and store all the preloaded data in a single string, for example, separating values with commas or another sign.
3) Use "Dictionary" type, as eri0o suggested.
https://adventuregamestudio.github.io/ags-manual/Dictionary.html
To elaborate a little, with Dictionary you may store data using dynamic "keys" like "mapN.propertyX".
Code: ags
Dictionary *dict = Dictionary.Create();
...
dict.Set(  String.Format("map%d.titlecode", map_num), titlecode );
dict.Set(  String.Format("map%d.titleFR", map_num), titleFR);
dict.Set(  String.Format("map%d.titleEN", map_num), titleEN);
...
String titlecode = dict.Get( String.Format("map%d.titlecode", map_num) );
There's still a problem that everything in Dictionary is a string, so you will have to invent how to convert numeric and other values.


In AGS 4 you could do more, but if you already have a complete game that may not be easy to upgrade.

Baguettator

Quote from: Crimson Wizard on Fri 24/11/2023 19:29:43Ah, I'm sorry, I now see why do you need this.

Because in AGS 3 you cannot have Strings in managed structs, and only managed structs in dynamic arrays, the existing solutions are:
1) Have each parameter in a separate dynamic array.
2) Create a dynamic array of Strings, and store all the preloaded data in a single string, for example, separating values with commas or another sign.
3) Use "Dictionary" type, as eri0o suggested.

In AGS 4 you could do more, but if you already have a complete game that may not be easy to upgrade.

OK, so I'll use the solution 2 ! (why I haven't think about it..?) The Dictionnaries seem nice, but perhaps more complicated because I'll need to use another material just for that thing.

Why upgrading to AGS 4 would be not so easy ? My game is large, but uses very "simple" things like Dynamic Sprites, some Draw functions, many custom structs to store datas in memory, and a lot of GUIs. When upgrading, which things need to be reworked ? And what are the "in AGS 4 you could do more" things ?

Crimson Wizard

Quote from: Baguettator on Fri 24/11/2023 21:37:07Why upgrading to AGS 4 would be not so easy ? My game is large, but uses very "simple" things like Dynamic Sprites, some Draw functions, many custom structs to store datas in memory, and a lot of GUIs. When upgrading, which things need to be reworked?

I cannot tell for certain, every project is different, but there are three things to keep in mind:
1) AGS 4 is still "alpha", it is useable, but not so many people are actively using it compared to 3.* versions, therefore some bugs may be kept unnoticed until now.
2) AGS 4 has a large number of old functions removed. If you were using those, you'll have to rework parts of your game script.
3) If you choose to use the new script compiler, this compiler is stricter than the old one, and does not let certain syntax that it considers incorrect. So you might have to adjust your script again, or live with getting compiler's warnings.

Quote from: Baguettator on Fri 24/11/2023 21:37:07And what are the "in AGS 4 you could do more" things ?

The AGS 4 forum thread notes the new features:
https://www.adventuregamestudio.co.uk/forums/ags-engine-editor-releases/ags-4-0-early-alpha-for-public-test/
Also, specifically the additions for the script syntax are mentioned here:
https://github.com/adventuregamestudio/ags/wiki/New-compiler%27s-end-user-cheat-sheet

SMF spam blocked by CleanTalk