Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: nightmarer on Tue 20/04/2021 00:18:43

Title: Struct with arrays
Post by: nightmarer on Tue 20/04/2021 00:18:43
Hello.

I am trying to create someking of an instance msg app for the inventory, and I am using a struct with an array, at is explained in AGS documentation.
This is the struct in the header of a script:
Code (ags) Select
struct MsgPDA {
String Sender;
String Message;
};


And in the body of the script I have declared the array as:
Code (ags) Select
MsgPDA msg[30];

And through the following function above in the same script I'm trying to make it able to call from any room or the game.
Code (ags) Select
void MessageContent(int index, String sender, String sms) {
  msg[index].Sender = sender:
  msg[index].Message = sms;
}


It says that msg is an undefined token.
What am I doing wrong?

Regards.
Title: Re: Struct with arrays
Post by: Crimson Wizard on Tue 20/04/2021 00:58:38
Quote from: nightmarer on Tue 20/04/2021 00:18:43
And through the following function above in the same script I'm trying to make it able to call from any room or the game.
<...>
It says that msg is an undefined token.

Variables must be declared before their use. If I read what you say correctly, you declared function before the variable.
Title: Re: Struct with arrays
Post by: nightmarer on Tue 20/04/2021 01:01:40
Great stuff. Thank you very much!
Title: Re: Struct with arrays
Post by: Khris on Tue 20/04/2021 08:22:22
How about this:
Code (ags) Select
// header

struct MsgPDA {
  String Sender;
  String Message;
  import void Set(String sender, String sms);
};

import MsgPDA msg[30];


Code (ags) Select
// main script

MsgPDA msg[30]; export msg;

void MsgPDA::Set(String sender, String sms) {
  this.Sender = sender:
  this.Message = sms;
}


Now you can do this in your room script:
Code (ags) Select
  msg[0].Set("Alice", "Hello world!");
Title: Re: Struct with arrays
Post by: nightmarer on Tue 20/04/2021 10:19:06
Hello Khris, thank you.
This should be working better but I need understand how this is working right now, because finally I'm tryingto set it from the function itself, as the array index is a global variable that increases everytime you got a new message. So there won't be any need to stablish the array index from the room.

Code (ags) Select
msg[PDAmsg-1].Set(sender, sms);
Title: Re: Struct with arrays
Post by: Khris on Tue 20/04/2021 10:51:44
If you just want to keep calling a function and have the index increase automatically:
Code (ags) Select
// add to global header:

import void SetNextMessage(String sender, String sms);

And
Code (ags) Select
// add to main script

int message_count = 0;

void SetNextMessage(String sender, String sms) {
  msg[message_count].Set(sender, sms);
  message_count++;
}


Like this?
Title: Re: Struct with arrays
Post by: nightmarer on Tue 20/04/2021 21:16:06
Thank you Khris, that is working.
Now the problem seems that every index of the array is returning the same content. It seems that I need global array the keep the information of every index stored.
Title: Re: Struct with arrays
Post by: Khris on Tue 20/04/2021 22:16:30
That definitely shouldn't happen, and you should already have a global array, as per the code in my first reply.

Can you post what you have in your headers / scripts?
Title: Re: Struct with arrays
Post by: nightmarer on Tue 20/04/2021 22:25:55
Sure, it is a little bit long as it is linked with a menu, but here we go.
Headers:
Code (ags) Select
struct MsgPDA {
  String Sender;
  String Message;
  import void Set(String sender, String sms);
};

import MsgPDA msg[30];


Body:
Code (ags) Select
Label *listslots[9];
MsgPDA msg[30]; export msg;

function new_PDAmsg() {
  PDAmsg+=1;
  aNokia.Play();
  Display("Inbox messages: %i", PDAmsg);
}
String LengthCorrection (String concat){
  concat = concat.Truncate(25);
  concat = concat.ReplaceCharAt(concat.Length -1, '.');
  concat = concat.ReplaceCharAt(concat.Length -2, '.');
  concat = concat.ReplaceCharAt(concat.Length -3, '.');
  return concat;
}

String ConcatMsg(int msgindex) {
  String concat = msg[PDAmsg-1].Sender;
  concat = concat.Append(" - ");
  concat = concat.Append(msg[PDAmsg-1].Message);
  if(concat.Length > 19) {
    concat = LengthCorrection(concat);
  }
  return concat;
}

void MessageList() {
 
  listslots[0] = Msg1; listslots[1] = Msg2; listslots[2] = Msg3;
  listslots[3] = Msg4; listslots[4] = Msg5; listslots[5] = Msg6;
  listslots[6] = Msg7; listslots[7] = Msg8; listslots[8] = Msg9;
 
  int i;
  if (PDAmsg < 9) {
    for (i = PDAmsg; i > 0; i--) {
      listslots[PDAmsg - i].Text = ConcatMsg(PDAmsg - 1);
    }
  }else {
    for (i = PDAmsg; i > PDAmsg - 9; i--) {
      listslots[PDAmsg - i].Text = ConcatMsg(PDAmsg - 1);
    }
  } 
}

void MsgPDA::Set(String sender, String sms) {
  this.Sender = sender;
  this.Message = sms;
}

void MessageContent(String sender, String sms) {
  new_PDAmsg();
  msg[PDAmsg-1].Set(sender, sms);
  MessageList();
}
Title: Re: Struct with arrays
Post by: nightmarer on Wed 21/04/2021 01:29:37
Ok, I have already realized that I was overwritting everything, I have solved it just checking first if the index is empty.