How to remove and add elements in an array? **SOLVED**

Started by Knox, Tue 25/05/2010 18:53:25

Previous topic - Next topic

Knox

Lets say I have 3 gui's, and each gui can close each other with keyboard shortcuts if one of  them is opened...as in, say guiA is open and I press "b" to open guiB, guiA will close and guiB will open...same thing for any other combination of gui's + their shortcuts. Also, if none are open, pressing the shortcut button wont close anything, it will just open the gui assigned to that shortcut.

Ill try to explain what Im after, let me know if it isnt clear:

Id like to add the names of these gui's into an a global string array so that I can use it later in a function, and be able to exclude the function parameter's gui name from the array and do stuff to each remaining element in that array...something like this:

Code: ags

String guiList[3] = {gA, gB, gC}

function hideGui(this GUI*)
{
  if (this.Visible) return;
  else
  {
    //exclude "this" from array guiList[]; (not sure how to write this line)-->
    guiList[] -= this //(something like this?)
    for $each in guiList[] //for each element now remaining in the array after removing "this"
    {
      $each.Visible = false; //set the visibility to false
    }
  }
}


I looked up Arrays in the Manual but Im still not too sure how to do this...Im thinking I might have to convert the gui names into ints and then use their index? Im confused!

--All that is necessary for evil to triumph is for good men to do nothing.

Ryan Timothy B

Code: ags

#define guiAmount 3

GUI* guiList[guiAmount];

function game_start() 
{
  guiList[0] = gA;
  guiList[1] = gB;
  guiList[2] = gC;
}

function hideOtherGUIs(this GUI*)
{
  if (!this.Visible)
  {
     int i;
    while (i<guiAmount)
    {
      if (this!=guiList[i]) guiList[i].Visible=false;
      i++;
    }
  }
}


I'm not 100% certain this is what you're asking for.  Didn't test it, I wrote it in the forum instead of within AGS.

It's not actually possible to remove elements from an array.  But it is possible to create dynamic arrays, but once you change the amount in it, it resets the data in the array.

Knox

Hey Ryan :)

Ok, so the array elements have to be declared inside game_start...why? Global Ints + Strings, etc...can be declared at the top of the global script, so I just want to know how come its different for arrays?

Ok, Ill try this out and let you know what happens. Im going to look up #define in google to see what it means :P

Thanks
--All that is necessary for evil to triumph is for good men to do nothing.

Khris

Assigning a value at the point of declaration outside of functions only works for primitive types (bool, int, etc.), not arrays (not even Strings, IIRC).

#define defines a so called constant; it's similar to a variable in the respect that its name is an arbitrary alphanumeric string and that it can contain a discreet value. It's not possible to change the contents though. The upside is that it can contain any sequence of letters.

Actually, every occurence in the script of the constant's name is replaced with its contents immediately before compile time.

Thus the following will compile:

Code: ags
#define test gui[0]

// inside some function:

  test.Visible = false;


Regarding your code:
You tried to declare the list of GUIs as String array. Regardless of the feasibility of the method in general, you can't use a String like that.
Like I told you in the other thread, there's no way of converting the contents of a string into a variable or pointer other than by a long list of if-elses.

You have to remove this way of thinking, at least when working with AGS.

There's (almost?) always another way of achieving what you want.
In your case, pressing the A-C keys will generate an ASCII value of 65-67 which you can translate directly into the GUI's ID by simply subtracting a number around 64.

Also, like Ryan showed in his example, it's not necessary at all to alter the list of GUIs stored in the array.
All you need to do is loop through them and skip the one supposed to be made visible.

Knox

Wow, thanks for explaining things guys...I got it working perfectly...just to say I wasnt using 3 guis (a, b and c)...its actually 15 and much more going on then just turning off and on gui's...:P Now I can add more gui's to the list in the future and it will behave perfectly without having to re-type the same code all over again! Im starting to really enjoy scripting :)

ps: I just bought a C++ for Dummies book...hope it helps :P
--All that is necessary for evil to triumph is for good men to do nothing.

SMF spam blocked by CleanTalk