Struct with arrays

Started by nightmarer, Tue 20/04/2021 00:18:43

Previous topic - Next topic

nightmarer

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
struct MsgPDA {
 String Sender;
 String Message;
};


And in the body of the script I have declared the array as:
Code: ags
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
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.

Crimson Wizard

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.

nightmarer

Great stuff. Thank you very much!

Khris

How about this:
Code: ags
// header

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

import MsgPDA msg[30];


Code: ags
// 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
  msg[0].Set("Alice", "Hello world!");

nightmarer

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
msg[PDAmsg-1].Set(sender, sms);

Khris

If you just want to keep calling a function and have the index increase automatically:
Code: ags
// add to global header:

import void SetNextMessage(String sender, String sms);

And
Code: ags
// add to main script

int message_count = 0;

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


Like this?

nightmarer

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.

Khris

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?

nightmarer

#8
Sure, it is a little bit long as it is linked with a menu, but here we go.
Headers:
Code: ags
struct MsgPDA {
  String Sender;
  String Message;
  import void Set(String sender, String sms);
};

import MsgPDA msg[30];


Body:
Code: ags
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();
}

nightmarer

Ok, I have already realized that I was overwritting everything, I have solved it just checking first if the index is empty.

SMF spam blocked by CleanTalk