• 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!

[SOLVED] 1D/2D array confusion

YoSniper

Member
Hello community,

I recently ran into an issue using arrays that I don't remember experiencing in Game Maker 8.

Basically, I am trying to initialize values to be stored in a persistent object that basically tracks my game data, and is responsible for saving to files and loading data back from them. Within this object, I keep track of data for three separate save slots.

This is a segment of the Create Event from this object:
Code:
var ii, jj;

for(ii = 0; ii < 3; ii += 1) {
    character_selected[ii] = "KID";
    gun_selected[ii] = "NORMAL";
}

for(ii = 0; ii < 6; ii += 1) {
    for(jj = 0; jj < 3; jj += 1) {
        if ii < 5 {
            guns_unlocked[ii, jj] = false;
        }
        characters_unlocked[ii, jj] = false;
    }
}
Note that the _selected arrays are meant to be 1D, while the _unlocked arrays have to be 2D. In GM8, rather than use the comma notation, I would have initialized them as
Code:
guns_unlocked[ii][jj] = false;
but GM:Studio apparently doesn't like it when I do that, as it detects a syntax error.

This isn't the problem so much, until I try to instantiate a Player object, which is given its character attribute from this object.

Script create_player(x, y, character, is_npc)
Code:
/*
Creates an instance of a Player object and gives it the input character.
The ID of the created instance is returned.
*/

var new_inst, obj_index;
if argument3 {
    obj_index = NPC;
} else {
    obj_index = Player;
}
new_inst = instance_create(argument0, argument1, obj_index);
new_inst.character = argument2;

if argument2 == "WIMPLY" {
    new_inst.mask_index = sprBigWimplyMask;
}

return new_inst;
This script is called from within a load_game script as such:
Script load_game(save_slot)
Code:
/*
Loads the saved game from World attribute data, including the room, player's x and y,
difficulty, game time, and characters and guns unlocked.
*/

with(CharacterParent) {
    instance_destroy();
}

room = World.save_room[argument0];
create_player(World.save_x[argument0], World.save_y[argument0],
            World.character_selected[argument0], false);
if World.difficulty[argument0] > 1 {
    if not instance_exists(Clock) {
        instance_create(0, 0, Clock);
    }
    Clock.timer = get_clock_time(World.save_id[argument0]);
}
I expect that by default, the Player would be assigned the string value "KID" to its character attribute. But what I get instead is the complete list { {"KID", "KID", "KID", } }. This manifests itself as an error later.

Why is the full list getting passed to my Player object. At first I thought it was because I'm calling a script from within another script, both of which utilize argument0 in their code. However, after eliminating that problem, I still get the same issue.

Are all arrays initialized as 2D and I have to add a second index to all of them? What is happening?

Any help is appreciated.
 
P

ParodyKnaveBob

Guest
Howdy, YoSniper,

From what you show, I don't immediately see a logic error. However, clearly, you do more than what you show. Have you run the (insanely better than GM8's) Debugger module yet? As an outsider looking in, what I would do is...
  1. Set a breakpoint at the top of initializing World in the first place and Step Through Code to examine every variable, array, etc. you're assigning, to make sure they are what you believe they are.
  2. Set a breakpoint at the top of loading and likewise examine everything there, too. It sounds like you're somehow passing an array into that create_player() script, meaning World.character_selected[argument0] is set not to a string but to an array...
  3. ...which means I'd Ctrl+Shift+F to further investigate everywhere World.character_selected gets touched.
I know porting surprises can be a pain. I hope this helps,
Bob
 

YoSniper

Member
I found the issue. It was buried.

Apparently I was calling the create_player script once from within the load_game script, where everything was fine, but I had forgotten about where I was also calling it from within the room's Creation Code. It was there that I had forgotten to index the array and was passing a bad argument to the new Player instance.
 
Top