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

Legacy GM linking objects to an inventory

W

Wild_West

Guest
Does anyone know a good way to use a switch statement and array value?
I', trying to get my inventory system finished and the only thing left is to let the layer be ale to choose an item in the list and then spawn it in the game room.

I have all the array positions set up so the name variable of the object in question is set in the inventory

like name = "name of item";

Then if yu collide with the item a loop checks for available space in the inventory and puts it there, represented by it's name string., if it finds the name already exists it just add 1 to the supply of that item.

when I open the inventory room I can scroll up and down to select an item name.
what I need is to be able to click a key and get the object that represents that item name string to be created.

Keep in mind I'm not greatly experienced with looping yet but
I tried using a loop and a switch together like this :

for( a = 0; a < 6; a ++)
{
switch( menu_object.held_items[a] )
{
case "Rock Shard" : item_found = rock_shard; break;

case "fayth charm" : item_found = fayth_charm; break;

case "Pick up Axe" : item_found = pick_up_axe; break;

case "Gate Key" : item_found = key; break;

case "Jump Shot" : item_found = jump_shot_item; break;

case "Magic Bomb" : item_found = magic_explosive; break;

case "Health Up" : item_found = hp_recover_item; break;

case "LAN recovery" : item_found = lan_recover_item; break;

case "Paralyz Heal" : item_found = paralyzed_heal_item; break;

case "Scarred heal" : item_found = scarred_heal_item; break;

default : break;
}
}

to link the name's with the object's they belong to and then based on the value of inventory's menu position 0 through 5 I loop again to see which item name is actually there, then just go back to the current game area and create it.

The only issue is the object never spawns and I know it's gotta be because of my "loop logic" because I've had a dreadful time getting this farwith buying items and checking the inventory because of it.

So can someone give me a little tip on the way looping should be done when it comes to switch statements, and arrays? I'd really appreciate it The tutorials I've seen only ever explain the basics.

This is my switch for attempting to make the objects :

if(room == item_menu)
{
use_item = show_question("Use this item?");


if(use_item == false){ exit; }else
if(use_item == true)
{
switch(menu_position)
{
case 0 :
room_goto(current_room);
if(room == current_room)
{
instance_create(player_parent.x +30, player_parent.y- 40, item_found);
menu_object.number_of_items[0] -= 1;
if(menu_object.number_of_items[0] < 1){ menu_object.held_items[0] = ""; }
}
break;
//-------------------------------------------------------------------------------------
case 1 :
room_goto(current_room);
if(room == current_room)
{
instance_create(player_parent.x +30, player_parent.y- 40, item_found);
menu_object.number_of_items[1] -= 1;
if(menu_object.number_of_items[1] < 1){ menu_object.held_items[1] = ""; }
}
break;
//-------------------------------------------------------------------------------------
case 2 :
room_goto(current_room);
if(room == current_room)
{
instance_create(player_parent.x +30, player_parent.y- 40, item_found);
menu_object.number_of_items[2] -= 1;
if(menu_object.number_of_items[2] < 1){ menu_object.held_items[2] = ""; }
}
break;
//-------------------------------------------------------------------------------------
case 3 :
room_goto(current_room);
if(room == current_room)
{
instance_create(player_parent.x +30, player_parent.y- 40, item_found);
menu_object.number_of_items[3] -= 1;
if(menu_object.number_of_items[3] < 1){ menu_object.held_items[3] = ""; }
}
break;
//-------------------------------------------------------------------------------------
case 4 :
room_goto(current_room);
if(room == current_room)
{
instance_create(player_parent.x +30, player_parent.y- 40, item_found);
menu_object.number_of_items[4] -= 1;
if(menu_object.number_of_items[4] < 1){ menu_object.held_items[4] = ""; }
}
break;
//-------------------------------------------------------------------------------------
case 5 :
room_goto(current_room);
if(room == current_room)
{
instance_create(player_parent.x +30, player_parent.y- 40, item_found);
menu_object.number_of_items[5] -= 1;
if(menu_object.number_of_items[5] < 1){ menu_object.held_items[5] = ""; }
}
break;
}
}
}
 

trg601

Member
Well, for starters, is the code for spawning the objects being run by a persistent object? It will not run the code after room_goto(current_room); if otherwise.
I also think that the problem might be with the for loop and switch statement put together, as I am not entirely sure you can do that.

Also just a suggestion, you could try instead of using a switch statement for determining the objects, you could use a ds_map with the keys being the string names and the values being the room names, which would make the code to look it up a lot simpler, and it would be easier to add new objects in.
 
W

Wild_West

Guest
Well, for starters, is the code for spawning the objects being run by a persistent object? It will not run the code after room_goto(current_room); if otherwise.
I also think that the problem might be with the for loop and switch statement put together, as I am not entirely sure you can do that.

Also just a suggestion, you could try instead of using a switch statement for determining the objects, you could use a ds_map with the keys being the string names and the values being the room names, which would make the code to look it up a lot simpler, and it would be easier to add new objects in.

Yeah I had doubts about the loop and switch being used together like hat but I wasn't sure f it was a logic flaw or just incompatible.
I haven't ever actually used a ds_map yet I've still been trying to really cement my ability with other things before trying to move on to anything with higher complexity.
Are ds_maps very tricky to learn?
Since you said it'd be simpler I assume no, but I've realized I have a lot more trouble learning these things without a proper teacher, and beyond youtube tutorials I'm pretty much n my own.

Oh and yes my objects for handling the menu is persistent already
 

trg601

Member
I think it is a logic flaw, I think using break inside the switch statement might break the loop too. I remember I had a switch statement inside of a for loop before and it did not work.

I think an easy fix you could do for that is put the switch statement inside of a script, and put the 'a' variable as one of the arguments. Something like: scr_menuitem(a)

And ds_maps are not super complicated, I think they could make your life easier.
I could help you learn them if you wanted, and I tried to find a good tutorial for them on youtube, but unfortunately both the ones I found forgot to destroy the ds_map after using it, which is very bad and can cause memory leaks. Just send me a pm and I'll help you out.

Edit: Or you could PM @NazGhuL, ninja'd lol.
 
W

Wild_West

Guest
I think it is a logic flaw, I think using break inside the switch statement might break the loop too. I remember I had a switch statement inside of a for loop before and it did not work.

I think an easy fix you could do for that is put the switch statement inside of a script, and put the 'a' variable as one of the arguments. Something like: scr_menuitem(a)

And ds_maps are not super complicated, I think they could make your life easier.
I could help you learn them if you wanted, and I tried to find a good tutorial for them on youtube, but unfortunately both the ones I found forgot to destroy the ds_map after using it, which is very bad and can cause memory leaks. Just send me a pm and I'll help you out.

Edit: Or you could PM @NazGhuL, ninja'd lol.
Okay great, Thanks I really appreciate it
 
W

Wild_West

Guest
No.

You clearly overcomplicated your inventory system. I'm willing to help you. I'll PM you.
I don't think it's overly complex, I mean trust me I've been trying very hard to ensure I don't make overly complicated setups.
I can send you the inventory setup outside this one part for creating the objects held if yu want to see it.
 
Top