• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker How do I check if an array position has been initialized

This should be a simple fix but I can't find anything on the forums. I want to find out if a specific position in an array has been set as I don't know how big the array is going to be. I don't want to initialize them all with a loop as I don't want to artificially cap the number of entries, nor do I want to expand the array size manually.

I found is_undefined but that only seems to work for the array as a whole and not individual values. I can't ask if it's null because it might not exist to ask. I'm a bit confused by this.

For example if test[0] exists but test[1] does not I can't do test[1] += "xyz" but I can do test[1] = "xyz";

If I can't check for its existence I can't clear it before using it in case it already contains data.

Edit: I believe the answer is going to be no there is no function to do this. Please correct me if there is, but I'm being forced to pre-assign the array values and bite the bullet. I didn't want to do this because it requires extra checks and unnecessary code and also puts an artificial limit on the number of entries that can be made. This makes me a little sad to be honest.
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
The only thing you can do is check the size of the array, as trying to check the value for an entry that doesn't exist will crash the game... So, if you want to know if the array position 4 is initialised, you check the array size using the array_length function, and if it's 5 or more then you know that the position HAS been initialised already. Can I ask why you don't want to pre-create the array and set all the values to undefined, for example? This is much better in terms of performance and memory, as each time you add a new position to the array, it has to deallocate the current memory and reallocate it again for the new size, so setting the size once means this memory reallocation isn't required and keeps performance up.

EDIT: Just re-read your post and you actually answer my question in the edit... :p
 
I'm writing a fully functional text box with all sorts of features. I have tried using one sting with line breaks etc. This gets very messy very quickly.

I will have to do it all manually. This is a shame. I'm sure it can be done with a string but it causes so many issues I'd rather just put each line in a separate array entry otherwise it gets really hairy.

Thanks

Edit: Actually I can wangle it with array_get_height. I can think of situations when this is no good but I can just about manage with this.
 

TheouAegis

Member
Programs waste memory all the time. You can fill the array with undefined, which is still a real value, but your entries will be unlikely to accidentally get set. You could store the number of valid entries in a variable or in the first entry of the array. If your array could never have a functioning entry of 0 (e.g., if the array is holding instance IDs), then just default to 0. You could have a variable storing the last valid index. You could assign a kill value (e.g. -1) to the last index, so if the kill value is detected, you stop looping through the array. The latter is much more common if you are going to loop through the array and by the sounds of it, that is exactly what you'd want, bit either would work.
 
Is this a game maker specific limitation? I'm sure in other languages you can ask if something exists. You can in game maker with is_undefined but it just doesn't stretch to individual positions inside the array.

For example, in PHP you can do array_key_exists. Which is in my humble opinion is far more elegant.
 
it just doesn't stretch to individual positions inside the array
What do you mean by this?

Like others suggested, you can make a system manually. Here is a pseudocode for what you want.
GML:
/// @function array_create_und(size)
return array_create(size, undefined);

/// @function array_index_exists(array, index)
if (index < array_length_id(array)) return !is_undefined(array[index]);
return 0;

/// Usage
array = array_create_und(5);
array[1] = 100;
show_debug_message(array_index_exists(array, 0));    // false
show_debug_message(array_index_exists(array, 1));    // true
 
Top