How can I find the first empty element in an array?


(Nate ) #1

Hey guys! I don’t do much work with arrays and am still fairy new to FlashPunk and its special methods. Say I have an array, it is my inventory, it is currently filled with 9 empty inventory images.

How would I go about searching the array for the first empty slot and then replacing that slot with say a freshly picked up rock image?

I keep getting errors that I am unfamiliar with and am not sure if it is because my array is filled with entities or if I am doing something else wrong. Probably both… lol

Thanks


(Nate ) #2

Let me rephrase this. What is the easiest way to find the next empty slot in my array?


(Nate ) #3

Okay this is my last update to this question, I have been doing some looking around and I now think that I need one of the following but not sure how to plug it up!

classFirst() typeFirst()

Would anyone be able to walk me through using these?

EDIT:

I would like to find the first element in the array that is the empty inventory image, I would then like to save the index number of that spot. Is that doable with the above mentioned functions?

Thanks guys!


(Zachary Lewis) #4

You’ll need to modify this function based on the types contained in your array, but this is the basic design pattern.

/** @return	The first empty index, or -1 if array is full. */
function GetFirstEmptyIndex():int
{
  var myArrayLength:uint = myArray.length;
  for (var i:uint = 0; i < myArrayLength; i++)
  {
  	// Perform the desired check on the item.
    if (myArray[i] == null)
    {
    	return i;
    }
  }

  // The array returned no values.
  return -1;
}

(Nate ) #5

Okay thank you!

This is very close to what I am looking to do. I probably set this up poorly. But I start out with an array of 9 empty inventory slots that traces out like this:

EmptyInventory,EmptyInventory,EmptyInventory,EmptyInventory,EmptyInventory,EmptyInventory,EmptyInventory,EmptyInventory,EmptyInventory

When I pick up a rock for example it traces this: Rock,EmptyInventory,EmptyInventory,EmptyInventory,EmptyInventory,EmptyInventory,EmptyInventory,EmptyInventory,EmptyInventory

How would I go about finding the next element in the array that is holding the EmptyInventory image?


(Nate ) #6

When I execute the provided code this is what I get:

[Fault] exception, information=ReferenceError: Error #1069: Property 0 not found on Number and there is no default value.

I am assuming that is because the array is already filled with items. I am trying to find out if there is a way to search the array by type or entity or something like that.


(Nate ) #7

This is the line it highlights with the above mentioned error:

					if (myArrayLength[i] == null)

(Abel Toy) #8

myArrayLength is a number containing the length of your array. You’re doing myArrayLength[0] which would be equivalent to myArrayLength.0, and numbers don’t have a 0 property.

What you need to do is myArray[i] == null. :stuck_out_tongue:


(Nate ) #9

Okay thanks!

I fixed it but it still is not doing anything. I need it to basically figure out the next element in the array that contains the entity Inventory. How would I go about checking this?


(Abel Toy) #10

Using the code @zachwlewis posted, to get the first space you’d do this:

myArray[GetFirstEmptyIndex()];

To get the first element after an empty, space you’d do something like this:

var index:int = GetFirstEmptyIndex() + 1;
if(index < myArray.length)
{
    myArray[index];
}

(Nate ) #11

Okay so I am still slightly lost… is there a way to do a loop that checks for the EmptyInventory entity in the array? Or can I only search numeric values? Because from the get go the array is populated with 9 empty inventory entities, and when you pick up a rock, the rock replaces the first inventory space. Making index 1 the next available empty inventory space, is there a way I can check in the loop something like this:

		if (theGameWorld.imageArray[i] == emptyInv)
				{
					trace("the next emptyInv spot is" + i);
				}

(azrafe7) #12

If you’re already populating your array/vector with EmptyInventory objects (hence following a slightly different approach to what suggested above), then you can check for:

    if (theGameWorld.imageArray[i] is EmptyInventory) ...

A simpler approach (following @zachwlewis example) would be something along these lines (untested):

    protected const MAX_ITEMS:int = 9;
    public var inventory:Vector.<Item> = new Vector.<Item>;

    /** Adds item to inventory (returns index or -1 if the inventory is full) */
    public function addItem(item:Item):int {
        var idx:int = inventory.length;
        if (idx < MAX_ITEMS) {
            inventory[idx] = item;
        } else {
            idx = -1;
        }
        return idx;
    }

    ...
    
    if (addItem(new Item(...)) < 0) throw new Error("Inventory full!");   

For unordered arrays (and moreover easier removal of items) you could also take a look into the Dictionary class.


(Nate ) #13

Okay thank you! Of course IS was the part that I was missing! So I have this loop here and what it does is it searches the array for emptyInventory and I want it to save out the index where it finds the first empty inventory then stops the loop.

How would I go about doing that? This is the loop with no guts really.

This is what I have so far:

		for (var i:int = 0; i < 9; i ++)
			{				
				if (theGameWorld.imageArray[i] is emptyInventory)
				{
					//save the value of i where it first finds an emptyInventory

					//end the loop so it does not just pick up the last emptyInventory
				}
			}

(Nate ) #14
	for (var i:int = 0; i < 9; i ++)
			{				
				if (theGameWorld.imageArray[i] is Inventory)
				{
					index = i;
					break;
				}
			}

I just did this and it seems to be working! I might just start from scratch lol