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

Replacing items in an inventory

W

Wild_West

Guest
I'm having trouble finding the solution to a question/answer error in this check for if an item is collected and your inventory is already full.
The top part works fine but the bottom half , the stuff for asking if you want to replace an item in your inventory is all wrong.
When I try to collide with an item , it asks me the question even when all my inventory slots are empty despite my != "" part. and it only terminates the question if I replace slot 0, not the slot of my choice, although at the end I DO actually get the item to appear in that slot too, but it's also in slot 0 every time no matter what which of course is wrong.
I can't see what's causing this s I need a more keen eye, thanks in advance.


Code in item collision with player

:This part works:
-----------------------

//Look through the items array
for( c = 0; c < 6; c ++; )
{
//and if any empty spaces exist place the name of this object in that space.

if(menu_object.held_items[c] == "")
{menu_object.held_items[c] = name;}

/*if the array slot already has this object's name set in it,
add 1 more of the item to the slot's supply
then break out of the loop search so it doesn't fill ALL the empty spaces.*/

if(menu_object.held_items[c] == name)
{menu_object.number_of_items[c] += 1; break;}
}
instance_destroy();
//-----------------------------------------------------------------------------------------------------

:This stuff doesn't work:
--------------------------------

//If there's no space ask if you want to replace an item
for( s = 0; s < 6; s ++; )
{
if(menu_object.held_items != "")
{
replace_item = show_question
("Inventory is full, would you like to replace an item?");

if(replace_item == true)
{
replacement_slot = get_integer
("Choose one of the 6 inventory slots to place this item in.
#(Once replaced the previous item will be lost forever.)", 0);

menu_object.held_items[replacement_slot] = name;
menu_object.number_of_items[replacement_slot] = 1;
instance_destroy();
}else{ exit; }
}
}
Just what the menu room looks like :)
 

jo-thijs

Member
You're checking on this line:
Code:
if(menu_object.held_items != "")
if an array doesn't equal a string.
You forgot to index your array with .

Also, your code seems to be for when the inventory is full,
but your code is executed in a for loop, why is that?
 
W

Wild_West

Guest
You're checking on this line:
Code:
if(menu_object.held_items != "")
if an array doesn't equal a string.
You forgot to index your array with .

Also, your code seems to be for when the inventory is full,
but your code is executed in a for loop, why is that?

The loop checks all the slots in the array to see if any of them equal the name of an item just like how it checks if the slots don't equal the name of any items at the start. Isn't that how checking arrays is always handled?
I indexed the array in the create event using the same loop method by the way
 

jo-thijs

Member
Why was a part of my previous reply striped through?
I didn't do that.

Anyway, no that's not what you did.
You loop through every slot, and for every non-empty slot, you say the inventory is full (despite the fact it might not be full)
and ask if the player wants to replace an item (which might get asked 6 times).
Every time the player says he wants to replace an item, the player is asked in which slot he wants to put it (this might also be asked 6 times).
Why do ask that inside the loop?
Then, once a slot is given (you don't perform any safety checks here either),
you replace the item at that given slot.
Then the next iteration of the loop happens and the player can duplicate his new item 6 times!

I'm wondering, do you actually know how for loops work?
 
W

Wild_West

Guest
Why was a part of my previous reply striped through?
I didn't do that.

Anyway, no that's not what you did.
You loop through every slot, and for every non-empty slot, you say the inventory is full (despite the fact it might not be full)
and ask if the player wants to replace an item (which might get asked 6 times).
Every time the player says he wants to replace an item, the player is asked in which slot he wants to put it (this might also be asked 6 times).
Why do ask that inside the loop?
Then, once a slot is given (you don't perform any safety checks here either),
you replace the item at that given slot.
Then the next iteration of the loop happens and the player can duplicate his new item 6 times!

I'm wondering, do you actually know how for loops work?

Yeah I mean I did struggle with their general purpose at first but I've been messing around with them for a few things and I'm better at understanding them, not so much yet at using them.
I still get infinite looping issues any time I tried using a while loop so I generally stay away from it.
But I see what you mean, now that I hear someone else say it , it makes sense.
It's just this thing I have with how my head works, I get stuck on one thing and I forget the things I should be focusing on, hence my constant logic errors.

Anyway I've been messing with the code all afternoon and got a better setup nw so I'll adjust using what you said.

Also I don't know about the lines that confused me too
 
W

Wild_West

Guest
Ok well, let me know the results of the new setup.
Alright here's the new code without the loop logic screw up :p


///Money exchange script for buying items

show_message(flavour_text);

if(room == room4)
{

buy = show_question(
"This item costs : " +string(imperial_price) + " I$ " +string(price)
+"#Purchase This item?" );
//------------Ask The Question------------------------------------------------------------------------------

if(buy == false){ exit; }

if(buy == true)
{
amount_brought = get_integer("How many would you like to buy?", 1);
}
//-----------Pay The Correct Amount-------------------------------------------------------------------------

//Take away the appropriate amount of skullies/Imperials from the money based on how many were bought

if( money_handler.skullies >= price * amount_brought )
and(money_handler.imperial_skullies >= imperial_price * amount_brought )
{
show_message
(
"Thank You for your Patronage! Fortune be with you on your journey.
#Purchased + " +string(amount_brought) + " " +string(name)
);

money_handler.skullies -= price * amount_brought;
money_handler.imperial_skullies -= imperial_price * amount_brought;

//Look through the inventory array and if there's an empty slot, place the bought item there.
for( c = 0; c < 6; c ++; )
{
if(menu_object.held_items[c] == "")
{menu_object.held_items[c] = name;}

if(menu_object.held_items[c] == name)
{menu_object.number_of_items[c] += amount_brought; break;}
//break the loop so only the first found avialble slot is filled
}exit;

//f your inventory is full
if(menu_object.held_items[5] != "")
{
replace = show_question
("Your Inventory is full, would you like to replace an item slot?");

if(replace == false){exit;}else
if(replace == true)
{ which_slot = get_integer("which slot will you replace", 0);
menu_object.held_items[which_slot] = name;
}
}
exit;//so the bottom doesn't display after buying
}
//---------------------------------------------------------------------------------------------------------

//Pay with Imperials when low on skullies

if (buy == true) and (money_handler.imperial_skullies >= 1) and (money_handler.skullies < price)
{
pay_with_imperials = show_question
("You seem to have an insufficciant amount of Skullies in your wallet,
but I see you're carrying one or two Imperials with you, would you
like to pay using those?");
}

if(pay_with_imperials == false){exit;}else
if(pay_with_imperials == true)
{
money_handler.imperial_skullies -= 1;
money_handler.skullies += balance;

show_message
("Thank You, Here is your change.
#Got back " +string(balance) + " Skullies");
}
//----------Can't Pay Enough--------------------------------------------------------------------------------

if(buy == true) and (amount_brought > 0)
{
if(money_handler.skullies < price * amount_brought)
{ show_message("You don't have enough Skullies."); }

//See if the item doesn't need an imperial value check
if(imperial_price == 0){exit;}

//If item has Imperial Value
if(imperial_price > 0) and (money_handler.imperial_skullies < 1 )
{ show_message("You don't have enough Imperials."); }
}
}
__________________________________________________________________________________

It all works great now, I don't get the constant asking for replacing a slot of course, but I still can't seem to get the check for no empty slots to work right. I filled up my inventory with items, all but the 6th slot so I could test the buying again.
After I successfully brought and filled in my 6th space I brought another item to test the replacement and it let me buy it but didn't say my inventory was full, and after I exited the message box the item of course wasn't in my list of held items.

So all I need is to see where my flaw is for the full inventory check and I'll be golden
 

jo-thijs

Member
This code can never be run:
Code:
//f your inventory is full
if(menu_object.held_items[5] != "")
{
replace = show_question
("Your Inventory is full, would you like to replace an item slot?");

if(replace == false){exit;}else
if(replace == true)
{ which_slot = get_integer("which slot will you replace", 0);
menu_object.held_items[which_slot] = name;
}
}
exit;//so the bottom doesn't display after buying
because it is preceded by "exit", which quits further execution of the current script/event.
 
W

Wild_West

Guest
This code can never be run:
Code:
//f your inventory is full
if(menu_object.held_items[5] != "")
{
replace = show_question
("Your Inventory is full, would you like to replace an item slot?");

if(replace == false){exit;}else
if(replace == true)
{ which_slot = get_integer("which slot will you replace", 0);
menu_object.held_items[which_slot] = name;
}
}
exit;//so the bottom doesn't display after buying
because it is preceded by "exit", which quits further execution of the current script/event.
Yeah I know but I put that in since without it will n every message no matter what I choose to do, buy an item or quit.
I probably just did that without thinking about it since it was already working for the rest but yeah I see how it's skipping now that I look at it separately from the rest in conjunction with what happens when I run the script.
I don't know why it always takes outside eyes before I can see those errors but I gotta thank you a ton for all the help.
 
W

Wild_West

Guest
This code can never be run:
Code:
//f your inventory is full
if(menu_object.held_items[5] != "")
{
replace = show_question
("Your Inventory is full, would you like to replace an item slot?");

if(replace == false){exit;}else
if(replace == true)
{ which_slot = get_integer("which slot will you replace", 0);
menu_object.held_items[which_slot] = name;
}
}
exit;//so the bottom doesn't display after buying
because it is preceded by "exit", which quits further execution of the current script/event.

The ooonly thing wrong with it now is it'll still ask if I want to replace instantly after I've brought something and filled in the last slot even though I was aiming for getting that after the inventory was full and you'd clicked on an item to buy again, but it's not such a major issue that I can't just let it be since the rest is perfect and it's been 5 days of trying this lol
 

jo-thijs

Member
You should check if the inventory is full by checking if c == 6.
Since you break the loop once en empty slot (or a slot with the same item) is found,
the only way c can be 6 after the loop if the loop ended because the condition c < 6 failed.

This will still leave you with an issue though.
If slot 1 is empty, but slot 2 contains the same item that is being added,
thn the empty slot will be filled up, waisting space.
You have to use 2 loops to solve this.
 
W

Wild_West

Guest
You should check if the inventory is full by checking if c == 6.
Since you break the loop once en empty slot (or a slot with the same item) is found,
the only way c can be 6 after the loop if the loop ended because the condition c < 6 failed.

This will still leave you with an issue though.
If slot 1 is empty, but slot 2 contains the same item that is being added,
thn the empty slot will be filled up, waisting space.
You have to use 2 loops to solve this.
Hm, Okay well, the items do stack once their specific name is occupying a slot, so I'm not sure that'll be a problem but I'll try it for the check you added and see if that fixes that last little issue.
Thanks again
 
Top