Multi-dimensional Arrays [New Problem!]

Started by monkey0506, Sat 25/12/2004 14:51:20

Previous topic - Next topic

monkey0506

Arrays have officially been implemented into AGS, but multidimensional arrays don't seem to work. I keep getting an error when I try to create one (such as int num[8][16], it returns the error "Unexpected "["). And something else. How do you make an array of strings? I can initialize a string array

i.e.

Code: ags
string strarray[30];


But I can't assign any values. I tried:

string strarray[30] = {
Ã,  "str1",
Ã,  "str2",
Ã,  "str3"
Ã,  };

But it returned "You can't assign a value to an array. So I tried:

StrCopy(strarray[1], "str1"), etc... but it said "Type mismatch: string with non-string." Oh well. It's Christmas. I set the clock ahead a day to work on my game, but I guess I can't figure out what I'm trying to do anyway. Oh well.

strazer

Try this:

Code: ags

// Main header script

struct mystrings {
  char str1[200];
  char str2[200];
  //...
};


Code: ags

// main global script

mystrings Blah[30];


Code: ags

//...

StrCopy(Blah[11].str1, "sometext");
StrCopy(Blah[22].str2, "someothertext");

//...


Technical Archive:
http://www.adventuregamestudio.co.uk/yabb/index.php?topic=13897.0
http://www.adventuregamestudio.co.uk/yabb/index.php?topic=13842.0
http://www.adventuregamestudio.co.uk/yabb/index.php?topic=6918.0

monkey0506

#2
Hey, that's great! ;D I'm still a little confused, but it works... That's what counts. I'll get this figured out as I go, and if I ever get this working right...you guys will definitely be amazed. If you're wondering what I'm talking about, I'm working on a secret project to help improve AGS. If you've watched what I'm doing, you may be able to figure it out... ::) EDIT: Please don't take this the wrong way. I am not trying to edit AGS...

EDIT: I think I understand what I was confused about now... That basically emulates a 3 dimensional array, right? Like, in, Blah[30], [30] would be the first dimension, the number of chars in the struct, [2], would be the second dimension, and the value in the array of the chars, [200], would be the third dimension, right? I only really wanted 2 dimensions, but I guess I can use the third dimension to aid with something else... Temporarily anyways...

strazer_away

You asked about how to make an array of strings. Since you can't do this in AGS directly (yet), you first have to declare the string as an array of 200 characters (letters) in a custom datatype (mystrings), hence
  char str1[200];
Then, you can make a normal array from this datatype with
  mystrings Blah[30];
30 is just an arbitrary number I chose for this example.

I deduced from your description that that was what you were trying to do.
The links I provided show you how use Blah as a multi-dimensional array.

Maybe you can describe in more detail what you're trying to achieve so we can help you better.

monkey0506

No... That's ok. :P Like I said, secret project. I understand why and how that script works. Thanks for the help!

monkey0506

#5
Ok, I have a problem now. I need those char arrays to be two dimensional. Well, I don't necessarily need them to be 2d, but it helps. I need 30 char arrays. I tried:

char strarray[6000]; // strarray[30][200]

But then when I go to change the value, i.e.:

Code: ags
StrCopy(blah[0].strarray[0], "str0");


I get the error "Type mismatch: string with non-string." I understand why (I'm calling an exact element of the array which makes AGS read it as a single char), but I don't know how to fix the problem. ??? Please advise!

strazer

Quote"Type mismatch: string with non-string."

That's because you're trying to copy a string ("str0") into a single letter (strarray[ 0 ]).

Remember we are using the char arrays as a replacement for strings! Leave them at 200 characters, that's the maximum length of strings in AGS at the moment.

Do the multi-dimensions on the parent, Blah in my example.

You probably want something like

Code: ags

// Main header script

struct mystrings {
  char str[200];
  //...
};


Code: ags

// main global script

mystrings Blah[150]; // 2D array, width 30, height 5: Blah[30][5]



Code: ags

//...

StrCopy(Blah[2*30+17].str, "sometext"); // y*width+x

//...

monkey0506

I understand why it wasn't working, and I understand everything you said, but that's not the problem. The problem is that I need 30 char arrays in the struct:

struct mystrings {
  char str0[200];
  char str1[200];
  char str2[200];
  char str3[200];
  char str4[200];
  char str5[200];
  ...
  char str27[200];
  char str28[200];
  char str29[200];
  }

I was just trying to find a better way of defining that many arrays. I guess that there isn't a better way. That's ok. I've almost got this thing figured out... If there is a better way of doing this, please tell me as it would help greatly, but otherwise this way will work, albeit lengthy.

RickJ

Read carefully and understand Strazer's last post and you will find the knowledge you seek.


"mystrings Blah[150]; // 2D array, width 30, height 5: Blah[30][5]" - Strazer



strazer

Yeah, and better use
  (2*30)+17
just to be sure. ;)

monkey0506

Oh... Well, what if I want Blah[300][30]? I realize that would be Blah[9000], but I'm not sure where you got the x and y values from... I believe the formula is like

Blah[15][4] =
  Blah[(15 * 30 + 4)] =
    Blah[454];

Right?

Gilbert

Yeah, for safety use (15 * 30) + 4 instead of (15 * 30 + 4).

Actually there is a whole archived thread dedicated to multidimensional arrays:
http://www.adventuregamestudio.co.uk/yabb/index.php?topic=13897.0

strazer

If y=15 and x=4 and height=30 then yes, it's
  Blah[(15 * 30) + 4]

With width=300, x=15, y=4 it's
  Blah[(15 * 300) + 4]

monkey0506

Yeah, well, it turns out that I don't even need that. Well, it doesn't really solve the new problem... Well, maybe it does solve it, but not really the way I wanted. It works better the way I have it right now anyway. And I'm nearly done with this project. There are a few bugs left to work out, and then I'll be done. ::)

SMF spam blocked by CleanTalk