Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: aedwards00 on Wed 31/03/2010 18:47:37

Title: Setting array values after declaration (inside structs)
Post by: aedwards00 on Wed 31/03/2010 18:47:37
Hey, this isnt specifically an AGS problem but a general c++ one. I figured someone here might be able to help anyway though.

Is it possible to create an a structure containing an array, then create an array of those structures, and then assign values to each array element all in one go?

To illustrate:


struct Mystruct {
  int intarray[26];
}



Mystruct structarray[5];

structarray[0].intarray[26] = {VALUES HERE};


My coding is pretty rusty so there might be some really obvious errors in there, but thats the general effect I'm trying to achieve. If I remember right then I don't think it is possible, but I just don't really want to have to manually assign 26 values into each of 8 different arrays.
Title: Re: Setting array values after declaration (inside structs)
Post by: Gord10 on Wed 31/03/2010 21:52:31
It is possible. You could try it by yourself with



int il=0, ij;
while(il<5) {
  ij=0;
  while(ij<26) {
    structarray[il].intarray[ij]=il*26+ij;
    ij++;
  }
  il++;
 
}



And use similar loops for displaying the values, which should be numbers between 0 and 130.

Don't forget to put a ; after your struct declaration.


struct Mystruct {
 int intarray[26];
};
Title: Re: Setting array values after declaration (inside structs)
Post by: aedwards00 on Wed 31/03/2010 21:59:55
Ah I think I didn't make clear that I have a set of specific values that I need to put into each array, each in a specific element.
Title: Re: Setting array values after declaration (inside structs)
Post by: Kweepa on Thu 01/04/2010 00:06:49
In that case I think you're hosed, unless loading from a text file is acceptable.
Title: Re: Setting array values after declaration (inside structs)
Post by: aedwards00 on Thu 01/04/2010 11:33:36
I thought as much  >:(

I've kind of worked around the problem though by creating separate arrays initialised with the right values, then looping to copy from these into the arrays inside the structs. Its a bit ugly having a load of extra arrays hanging around but it gets the job done  :P

Thanks to both for your suggestions though :)