Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: nightmarer on Sat 13/03/2021 17:19:01

Title: Some guidance with arrays in structs
Post by: nightmarer on Sat 13/03/2021 17:19:01
Hello all again.

I'm trying to build somekind of SMS application for my game in which the main character will be able to read all the incomming messages other charaters may send to him.
To achieve this I have defined an struct in a script header in order to have two strigs, one with the sender, and another one with the text message.
Code (ags) Select

struct MsgPDA {
String Sender;
String Msg;
};


Following the struct documentation in the following link https://www.adventuregamestudio.co.uk/manual/ags43.htm#struct (https://www.adventuregamestudio.co.uk/manual/ags43.htm#struct)
Following the recommendation I am trying to combine it with an array.
Code (ags) Select
MsgPDA msg[10];
msg[0].Sender = "Sara";
msg[0].Msg = "Hello, its me.";


My intention is to call this array from any room. But I don't know where I need to place each part of the code, as I am receiving
My questions:

Thanks and regards.
Title: Re: Some guidance with arrays in structs
Post by: Crimson Wizard on Sat 13/03/2021 17:24:31
Arrays are declared, imported and exported just like any other global variable.

https://adventuregamestudio.github.io/ags-manual/ImportingFunctionsAndVariables.html

So, for example, in the header
Code (ags) Select

import MsgPDA msg[10];

and in the script body
Code (ags) Select

MsgPDA msg[10];
export msg;



But they may be initialized only inside functions, because AGS script does not support element assignment outside of a function.

If array supposed to have some content from the start, then natural place to assign its values is game_start function that you may add in your script module.
If array is initialized or changed later, then it depends... guess place this where it is meant to change.
Title: Re: Some guidance with arrays in structs [Solved]
Post by: nightmarer on Sat 13/03/2021 21:26:25
Thank you very much.