• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2.3+ Problems with a toolbar

KKf

Member
I'm trying to make a decent toolbar but I'm having trouble getting the game to detect that I have something equipped in the toolbar.

I have come up with these errors:

1. I select the sword in slot 1, it changes to the sword sprite and removes it when I stop selecting slot 1, but if I equip a sword in slot 1 and 2 when I select slot 1 not if it changes the sprite when I am in slot 2

2. I select the sword in slot 1, change the sprite of the sword and remove it when I stop selecting it, but if I equip a sword in slot 1 and 2 when selecting slot 2 does nothing and only does it in slot 1

Here is an example of error 2



The code:
GML:
#region Change sprite depending on what you have equipped

if (global.tb_slot1 == item_1)
{
    
    if (global.tb_pos == 1)
    {
        sprite_index = spr_PlayerWS;
    }
    else
    {
        sprite_index = spr_Player;
    }
}
else if (global.tb_slot2 == item_1)
{
    
    if (global.tb_pos == 2)
    {
        sprite_index = spr_PlayerWS;
    }
    else
    {
        sprite_index = spr_Player;
    }
}

#endregion
 

Nidoking

Member
Judging by what little you've posted, I think there's a flaw in your design. Just look at it this way: If tb_slot1 is item_1, then the part that checks tb_slot2 won't run at all. And do you really want to have separate if checks for every possible item in every possible slot? Try storing whatever item IS equipped in the selected slot in some other (possibly global) variable, and checking only that to determine what sprite to use.
 

KKf

Member
Judging by what little you've posted, I think there's a flaw in your design. Just look at it this way: If tb_slot1 is item_1, then the part that checks tb_slot2 won't run at all. And do you really want to have separate if checks for every possible item in every possible slot? Try storing whatever item IS equipped in the selected slot in some other (possibly global) variable, and checking only that to determine what sprite to use.
You mean using an object in the empty spaces to determine that I don't have anything equipped?

I could try to check if in slot1 I have item1 selected like this with all the slots and also detect if in slot1 I have "air"
 

Nidoking

Member
You mean using an object in the empty spaces to determine that I don't have anything equipped?
I like to pre-fill all of the slots with noone for exactly that purpose. Whether you're using object IDs or instance IDs, noone won't match any valid entry. But seriously, whatever you do to select a slot, put whatever's in that slot into another global variable, and now you don't need to check "if" anything. This is literally about the simplest thing you can possibly do in programming.
 
Top