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;
}
}
}
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;
}
}
}