Some guidance with arrays in structs

Started by nightmarer, 13 Mar 2021, 17:19

« previous - next »


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

struct MsgPDA {
String Sender;
String Msg;

Following the struct documentation in the following link
Following the recommendation I am trying to combine it with an array.
Code: ags
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:

  • The struct I guess it goes in the header of the script. Is that right?
  • Where I should place the array definition? If I enter in the body of the script it says that msg is unexpected.
  • Do I need to do something else to call this array from any room?

Thanks and regards.

Crimson Wizard

Arrays are declared, imported and exported just like any other global variable.

So, for example, in the header
Code: ags

import MsgPDA msg[10];

and in the script body
Code: ags

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.

SMF spam blocked by CleanTalk