Arrays within arrays, and .GetAtScreenXY [SOLVED]

Started by dronon, Sun 23/07/2006 14:04:18

Previous topic - Next topic

dronon

Two questions here.

1.  Can I have an array inside an array, and how would I retrieve data from it? For instance, if I set up

int arrayA[2]  (preferably containing numbers, although it doesn't have to), and
String arrayB[3]  (which must contain strings),

How would I write code to say, "Get the string at arrayAB location (x,y)"?

2. After setting a variable called targtype, I've got a series of code which looks like this:
Code: ags

if (targtype == 0) {
  Hotspot* whatami = Hotspot.GetAtScreenXY(mouse.x, mouse.y);}
  else if (targtype == 1) {
  Object* whatami = Object.GetAtScreenXY(mouse.x, mouse.y);}


Is there a way to get rid of the if/then and compress it into something like:

//OtherWord//* whatami = //OtherWord//.GetAtScreenXY(mouse.x, mouse.y);

where //OtherWord// is either Hotspot or Object, depending on what targtype is set to?

(I hope this made sense!)

Ashen

1)
I'm not sure what you mean. Multi-dimensional arrays aren't currently supported by AGS, but it can be worked around. Try a forum search if this is what you meant, or post again (or wait for someone who understands the question) if it's not.
Or, do you mean structs? Then you could have, for example, arrayA[1].arrayB[2] = "Hello";, and a function like:
Code: ags

String ArrayString(int A, int B) {
  return arrayA[A].arrayB[B];
}


That would allow:
Code: ags

Display(ArrayString(1, 2));

To display "Hello".
Obviosly, you could also have a function to write the array String (SetArrayString(1,2,"Hello");).

2)
Unfortunately, the way you've got it is the only way. There's nothing like a Location pointer type that'd do what you're asking (be Object/Hotspot as needed).
I know what you're thinking ... Don't think that.

dronon

Er... okay, now I'm in over my head on structs, but yes, I'm trying to construct a 2-dimensional array that can store strings of any length at points (x,y). What code do I use to declare the struct at the start? Searching back through the posts, I found this one, but it's for an earlier version and I'm not sure it's what I'm after.

monkey0506

You can't currently have two dimensional arrays in AGS.  There are a couple of alternatives:

1) Create a large array. i.e., if you want array[a] you could accomplish this by creating an array[a*b] and then accessing the members like array[(a_index * b_max) + b_index].

2) Use a struct:

Code: ags
struct MyStringArrayStruct {
  String Strings[3];
  };

MyStringArrayStruct Array[2];


Then you could access the Strings via:

Code: ags
Array[1].Strings[0]


And the like.

dronon

I've almost got it working now!

In #sectionstart game_start (maybe that's a mistake), I put:
Code: ags

struct MyStringArrayStruct {
  String Strings[3];
  };
MyStringArrayStruct Array[2];
Array[1].Strings[2] = "Hello world.";


That works all by itself, but I can't seem to access the data elsewhere. Later on in #sectionstart repeatedly_execute I've got an if/then situation that, if circumstances are true, then (int A=1) and (int B=2). But when I try to transfer the data into a String called "output":

Code: ags

String output = String.Format("%s",Array[A].Strings[B]);


I get the error message, "undefined symbol 'array' ". However, if I move that code into #sectionstart game_start after the first bit of code, and define A and B there, it works fine.

What am I doing wrong? Thanks for all your help so far!

Gilbert

Move the declaration part:
Code: ags

struct MyStringArrayStruct {
  String Strings[3];
  };
MyStringArrayStruct Array[2];

outside of game_start(), on top of the global script.
Otherwise they're local to the game_start() function only.

monkey0506

Wow...I didn't mean for my example code to be taken to such extreme literality.

Okay, try something like this:

Code: ags
// main global script

struct MyStringArrayStruct {
  String Strings[3];
  };

MyStringArrayStruct Array[2];

// inside of game_start (between the brackets '{' and '}')

Array[1].Strings[2] = "Hello world.";


You have to define the struct and the instance (i.e., Array[2] is an instance of the MyStringArrayStruct struct) outside of all functions in order to access the instance from other functions. If you define it inside of game_start then it is only accessible to game_start. Putting it between the #sectionstart and the function itself effectively declares it outside of all functions, however it's usually best to put your scripts outside of those #section[...] parts except if you're putting into the function.

The #sectionstart and #sectionend are there to help the editor find the functions so that you can use the Script menu to access your scripts easier.

And...the naming convention I used is rather silly.  I'm not really sure what it is you are trying to accomplish, but you can change "MyStringArrayStruct" to pretty much anything you like (as long as it doesn't contain symbols (except underscore), doesn't start with a number, and doesn't contain spaces).

And you can change "Strings" to a more friendly name as well. I was just trying to provide an example. Perhaps you should tell us what it is you will be using this for? That could help to come up with a better name that "MyStringArrayStruct".

[EDIT:]

Hmm...Firefox is acting weird today.  And Gilbot beat me to posting.

dronon

Hurrah, that worked! Thanks everyone!  I don't understand what struct is doing, but at least now I can implement it, and learned a bit more about where to put game code.

Don't worry about the string names, I'll be changing those; I used the same ones in my examples to show how I was interpreting the earlier posts.

What am I using this for?  Well, I'm using the Sierra-like template that comes with AGS and I'm trying to implement a LucasArts-style bar at the bottom of the screen to explain what'll happen, in a verb-object (-preposition-target) way. "Open door" "Use dog on cat", etc. The 2D-array is going to store the verbs, depending on mouse.Mode, whether the target is a character, object, and so on; there'll be a custom property called "VerbNo" that'll store a number which will figure into it.

I've got the underlying code figured out now. All that's left is to figure out how I want to implement the matrix or matrices. I figure I can set up various classes of verbs for different situations, but I'm still thinking about how to have one unusual verb appear when there's a specific object/target combination.  I think it'll be simpler to use the Interaction manager when that's the case.

There's another situation where I'd like to use a 2D matrix later on in the game, assuming I get that far!

SMF spam blocked by CleanTalk