2D阵列无法正确访问globalvar(GameMaker)

时间:2018-11-01 20:32:30

标签: game-maker gml game-maker-language game-maker-studio-2

我有一家商店正在尝试设置,调试器内部的所有帐户都在触发该事件,但是绝对不执行任何操作。

这是我的设置:

o_Shop-玩家互动以在room0内部创建一个图层

o_Shop_setup-创建数组,创建事件:

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

然后,我使用访问该数组的以下对象:

o_item_parent-o_shop_Mines / o_shop_AmmoHE的父级 -在o_item_parent抽奖活动中:

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-继承Draw事件,并且保持不变。

o_shop_Mines创建事件:

myid = 1;

o_shop_AmmoHE-继承Draw事件,不做任何更改。 o_shop_AmmoHe创建事件:

 myid = 2;

o_buy_parent-o_buy_Mines / o_buy_ammoHE的父级 o_buy_parent-创建事件:

image_speed = 0;
image_index = 0;

o_buy_parent步骤事件:

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按下事件:

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-从o_buy_parent继承步骤事件/向左按下事件

o_buy_Mines创建事件内部:

myid = 1;
event_inherited();

o_buy_ammoHE-从o_buy_parent继承步骤事件/向左按下事件

o_buy_ammoHE创建事件内部:

myid = 2;
event_inherited();

o_buy_ammoHE创建事件内部:

myid = 2;
event_inherited();

o_buy_ammoHE左按下事件内部:

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 CAPS中的所有变量都是使用o_init o_init is inside of room -> rm_LoadingScreen`内的globalvar方法创建的全局变量。

rm_LoadingScreen除了保留非持久性对象外什么都不做,该对象包含一个 可以更改STATE = GameState(枚举)的switch语句

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

除了商店,其他所有东西都在工作。昨天 did 工作正常,但我无法追踪自己的故障。与目前一样,代码会将在屏幕上绘制的数字递增

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

,但globalvar AMMO_AMOUNT不变。调试器将其显示为永不更改。

谁能提供一些反馈或第二眼?我已经盯着这个看了好几个小时了,但还是没发现问题。

1 个答案:

答案 0 :(得分:0)

您对AMMO_AMOUNT和其他带大写字母的名称的使用等同于常量的常数,用作初始值。无论如何,global.inventory [myid,4]中的价值似乎已经增加。 因此,如果要更改AMMO_AMOUNT的值,则应显式地进行更改,就像在注释行中一样:// AMMO_AMOUNT + = 5; 希望您不要误解XD

最后,您的代码应类似于: 在 o_buy_ammoHE 左按下事件内,最后一个 else 块:

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

如果没有帮助,我会认为一切都按预期进行,您只需要在需要时使用global.inventory [myid,4])。