How to choose "arrayA" or "arrayB" with a variable inside a function **SOLVED**

Started by Knox, Tue 22/02/2011 21:17:47

Previous topic - Next topic

Knox

If you look at the third block of code,  Ive got a line "if (aVariableArray[j].GetProperty("CustomPropertyA") == false)".

Im always getting an error with the "aVariableArray[j]" line in the 3rd block...mainly because I dont understand how to
use an certain array depending on another value (does that make sense?);

Im trying to get "aVariableArray" to be set to either aArrayA[] or aArrayB[], depending on which of these is called: iUseItemA or iUseItemsB.

Code: ags

//(in .ash):
#define SL_A_ITEMS 6
import InventoryItem* aArrayA[SL_A_ITEMS];

#define SL_B_ITEMS 6
import InventoryItem* aArrayB[SL_B_ITEMS];


Code: ags

//(in .asc):
InventoryItem* aArrayA[SL_A_ITEMS];
InventoryItem* aArrayB[SL_B_ITEMS];

void ItemLists()
{
  aArrayA[0] = iMic; aArrayA[1] = iRadio; aArrayA[2] = iBattery; aArrayA[3] = iKeysA; aArrayA[4] = iGun; aArrayA[5] = iPatrolClip;
  aArrayB[0] = iSpray; aArrayB[1] = iMints; aArrayB[2] = iMeds; aArrayB[3] = iShield; aArrayB[4] = iMask; aArrayB[5] = iSanitizer;
}


Code: ags
 
//in some function:
{
  if (this == iUseItemsA) aVariableArray[j] = aArrayA[];
  else if (this == iUseItemsB) aVariableArray[j] = aArrayB[];
  
  if (bEx == false)
  {  
    j = 0;
    while (j < iConstantNum)
    {
      if (aVariableArray[j].GetProperty("CustomPropertyA") == false)
      {
      ...blah etc...


If I did it "straight-up" without that variable, I would just do this and it works (but its not what I want cause I dont want to write all that code twice, or more, depending on how many arrays I might create in the future):

Code: ags

//pseudo code **EDIT**
    while (j < iConstantNum)
    {
      if (blah)
      {
        if (aArrayA[j].GetProperty("CustomPropertyA") == false)
        {
          ...stuff
        }
      }
    }
      
      
    OR:
      
    while (j < iConstantNum)
    {
       if (blah)
       {
         if (aArrayB[j].GetProperty("CustomPropertyB") == false)
         {
           ...stuff
         }
       }
    }
--All that is necessary for evil to triumph is for good men to do nothing.

Calin Leafshade

Unfortunately you cant set pointers to arrays in the way that youre trying to do (not in ags anyway)

just use a condition at the last possible second

Code: ags


while (j < iConstantNum)
    {
     if (this == iUseItemsA) aArrayA[j].GetProperty("CustomPropertyA") = false;
     else  if (this == iUseItemsB) aArrayB[j].GetProperty("CustomPropertyB") = false;
     }


     

Knox

crap well in this case it wouldnt save me space cause it would end up being something like this:
Methinks I have no choice but to duplicate lines in this function.

Code: ags

while (j < iConstantNum)
{
  if (this == iUseItemsA)
  { 
    if (aArrayA[j].GetProperty("CustomPropertyA") == false)
    {
      ...do stuff with arrayA...
    }

  }
  else if (this == iUseItemsB)
  {
    if (aArrayB[j].GetProperty("CustomPropertyB") == false)
    {
      ...do exact same stuff as above but with arrayB 
    }
  }
}
--All that is necessary for evil to triumph is for good men to do nothing.

monkey0506

Well actually if you would be willing to use dynamic arrays instead of static arrays, you can essentially create a pointer to the array..

Code: ags
//(in .ash):
#define SL_A_ITEMS 6
import InventoryItem* aArrayA[];

#define SL_B_ITEMS 6
import InventoryItem* aArrayB[];


Code: ags
//(in .asc):
InventoryItem* aArrayA[];
InventoryItem* aArrayB[];

void ItemLists()
{
  aArrayA[0] = iMic; aArrayA[1] = iRadio; aArrayA[2] = iBattery; aArrayA[3] = iKeysA; aArrayA[4] = iGun; aArrayA[5] = iPatrolClip;
  aArrayB[0] = iSpray; aArrayB[1] = iMints; aArrayB[2] = iMeds; aArrayB[3] = iShield; aArrayB[4] = iMask; aArrayB[5] = iSanitizer;
}

function game_start()
{
  aArrayA = new InventoryItem[SL_A_ITEMS];
  aArrayB = new InventoryItem[SL_B_ITEMS];
  //ItemLists(); // I'm guessing you would probably be doing this here?
}


Then with that, you could do the following:

Code: ags
//in some function:
{
  InventoryItem *aVariableArray[] = aArrayA;
  int aVariableArraySize = SL_A_ITEMS;
  if (this == iUseItemsB)
  {
    aVariableArray = aArrayB;
    aVariableArraySize = SL_B_ITEMS;
  }
  if (bEx == false)
  {  
    int j = 0;
    while (j < aVariableArraySize)
    {
      if (aVariableArray[j].GetProperty("CustomPropertyA") == false)
      {
        // ...blah etc...
      }
    }
  }
}


See, the way dynamic arrays work..they really are simply pointers to the dynamically-created array. So all you have to do is create a new dynamic array (pointer) and assign it to the same value as the array you want to work with.

Edit: Oops, forgot to make aVariableArray a dynamic array haha. Fixed that.

Knox

Hey Mooo0onkey!

That's exactly what I was looking for, dynamic arrays...Ill go look it up in my for dummies book.

I'll go try that out as soon as Ive got access to the cpu, hope I can get it to "veurk".

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

monkey0506

There are some limitations to the way dynamic arrays are implemented in AGS right now, such as the fact that there is no way to check the size, which is why I created a separate variable to determine the size of the array. Also dynamic arrays have a maximum size of 1000000. You probably won't have to worry about that, but it's good to know that it's there.

Dynamic arrays (read as: dynamic array pointers) can be null (like other pointers) in which case trying to work with them will throw a null pointer reference error. You check if the array (pointer) is null just like you would with pointers, i.e.:

Code: ags
if (aVariableArray == null) return;


Also, there's no standard library of functions for working with dynamic arrays. So if you have to resize the array, or other operations manipulating the array itself (as opposed to the items within the array) then you have to script that yourself.

These are actually items that I'd like to see addressed once the engine is open-sourced, though as long as you understand them they aren't really difficult to overcome.

Edit: Something else I just thought of is that you cannot import a function that returns a dynamic array (dynamic arrays can be returned from functions, as well as being used as function parameters) directly as part of a struct definition. If you want to have a function that returns a dynamic array associated with a struct, the function has to be defined as an extender of the struct. This means that you cannot currently return a dynamic array from a static function of a custom struct.

And one last thing, you have to be careful not to confuse static and dynamic arrays. The type of arrays you're accustomed to working with are static. Dynamic arrays are not directly interoperable with static arrays. You can create a function, for example, to copy the items between them, but you can't assign a static array into a dynamic array. Keep that in mind as well.

Knox

Wow, well Im gonna have to reread that 5 times! hehe...thanx for the explanations too, its appreciated.

Ok, gonna go try that out now!
--All that is necessary for evil to triumph is for good men to do nothing.

Knox

ooooh...nice! It works, thanx guys for all your help, (once again) ;D

Next round of beers on me.
--All that is necessary for evil to triumph is for good men to do nothing.

SMF spam blocked by CleanTalk