Shamus Locke
Shamus Locke

Reputation: 3

2D Array not properly accessing globalvar (GameMaker)

I have a shop that I am attempting to set up that by all accounts inside of the Debugger is triggering, but doing absolutely nothing.

This is the set up that I have:

o_Shop - Player interacts to create a layer inside of room0

o_Shop_setup - Creation of Array, Create Event:

global.inventory[1, 1] = "Mines";               //Item Name
global.inventory[1, 2] = sp_Blue_mine_UI;       //Item Sprite
global.inventory[1, 3] = 50;                    //Cost
global.inventory[1, 4] = MINE_NUMBER;           //Current Inventory
global.inventory[1, 5] = MINE_NUMBER_MAXIMUM;   //Maximum amount

//HE Ammo
global.inventory[2, 1] = "HE Ammo";             //Item Name
global.inventory[2, 2] = sp_ammoHE;             //Item Sprite
global.inventory[2, 3] = 5;                     //Cost
global.inventory[2, 4] = AMMO_AMOUNT;           //Current Inventory
global.inventory[2, 5] = AMMO_AMOUNT_MAXIMUM;   //Maximum amount

Then, I use the follow objects that access that array:

o_item_parent - Parent of o_shop_Mines/o_shop_AmmoHE - Inside of o_item_parent Draw Event:

draw_set_font(fnt_small);
draw_set_halign(fa_center);
draw_set_valign(fa_middle);
draw_set_color(c_white);
draw_text(x, y - 100, global.inventory[myid, 1]);
draw_sprite(global.inventory[myid, 2], 0, x, y - 30);
draw_set_color(c_yellow);
draw_text(x, y + 32, string(global.inventory[myid, 3]) + " Coins");
draw_set_color(c_white);
draw_text(x, y + 64, string(global.inventory[myid, 4]) + " / " + string(global.inventory[myid, 5]));

o_shop_Mines - Inherits the Draw Event with no changes.

o_shop_Mines Create Event:

myid = 1;

o_shop_AmmoHE - Inherits the Draw Event with no changes. o_shop_AmmoHe Create Event:

 myid = 2;

o_buy_parent - Parent of o_buy_Mines/o_buy_ammoHE o_buy_parent - Create Event:

image_speed = 0;
image_index = 0;

o_buy_parent Step Event:

if (COINS >= global.inventory[myid, 3])
{
    image_index = 0;    
}
else
{
    image_index = 1;    
}

if (global.inventory[myid, 4] == global.inventory[myid, 5])
{
    image_index = 1;    
}

o_buy_parent Left Pressed Event:

if (global.inventory[myid, 4] == global.inventory[myid, 5])
{
    audio_play_sound(snd_gunReload, 1, false);
}
else if (COINS < global.inventory[myid, 3])
{
    audio_play_sound(snd_game_over, 1, false);  
}
else
{
    //audio play sound snd_purchase_complete
    COINS -= global.inventory[myid, 3];
    global.inventory[myid,4]++;
}

o_buy_Mines - Inherits Step Event/Left Pressed Event from o_buy_parent

Inside of o_buy_Mines Create Event:

myid = 1;
event_inherited();

o_buy_ammoHE - Inherits Step Event/Left Pressed Event from o_buy_parent

Inside of o_buy_ammoHE Create Event:

myid = 2;
event_inherited();

Inside of o_buy_ammoHE Create Event:

myid = 2;
event_inherited();

Inside of o_buy_ammoHE Left Pressed Event:

if (global.inventory[myid, 4] == global.inventory[myid, 5])
{
    audio_play_sound(snd_gunReload, 1, false);
}
else if (COINS < global.inventory[myid, 3])
{
    audio_play_sound(snd_game_over, 1, false);  
}
else
{
    //audio play sound snd_purchase_complete
    COINS -= global.inventory[myid, 3];
    global.inventory[myid,4]++; //breakpoint triggers fine, does nothing.
    //Have also tried the below line, doesn't work, but commented out    currently:
    //AMMO_AMOUNT += 5;
}

All variables in ALL CAPS are global variables created using globalvar method inside of o_init o_initis inside of room ->rm_LoadingScreen`

rm_LoadingScreen does nothing but hold this object which is not persistent, this object contains a switch statement that can change STATE = GameState (enum)

`STATE = GameState.menu` is called by default which calls
scr_menuLoader();
break;

Everything is working except the store. It did work yesterday but I'm having trouble tracking down what I broke. As currently, the code is incrementing a number that is drawn to the screen by

draw_text(x, y + 64, string(global.inventory[myid, 4]) + " / " + string    (global.inventory[myid, 5]));

but the globalvar AMMO_AMOUNT is not changing. The debugger shows it as never changing.

Can anyone provide some feedback or a second set of eyes? I've been staring at this for hours and I just can't see a problem.

Upvotes: 0

Views: 218

Answers (1)

Juanpa
Juanpa

Reputation: 173

The use you are doing for AMMO_AMOUNT and other names with caps is the equivalent for a constant, used as an initial value. Anyway It seems the value in global.inventory[myid,4] has already increased. So if you want to change the value of AMMO_AMOUNT you should do it explicitly, just like you have in the commented line: //AMMO_AMOUNT += 5; I hope you haven't commented it by mistake XD

In the end, your code should be like: Inside of o_buy_ammoHE Left Pressed Event, the last else block:

else
{
    //audio play sound snd_purchase_complete
    COINS -= global.inventory[myid, 3];
    global.inventory[myid,4]++; 
    AMMO_AMOUNT = global.inventory[myid,4];
}

if it doesn't help, I would think that everything is working as expected and you just have to use global.inventory[myid, 4]) where needed.

Upvotes: 0

Related Questions