Legacy GM How do I collect an item only once

W

Wartic

Guest
I'm having some trouble trying to collect an item only once in a level. I want to have an item to be collected once so if the player tries to replay the level again the item won't be there unless it was not collected before.

Any help will be appreciated.
 

dannyjenn

Member
You just need a global variable to keep track of whether or not the item has been picked up.
Here's a start:
Code:
// Game start:
global.foo = false;
Code:
// When the player picks up the item:
global.foo = true;
Code:
// The item's create event:
if(global.foo){
    instance_destroy();
}
If you have multiple items, you'll need multiple global variables, or better yet use a global array. If you wanted to you could do some trickery with the bitwise operators, but that's probably not necessary.
 
L

lord_berto

Guest
you will need a persistent object. create a new object and mark it as persistent.
we can call this object itemTracker.
now in the first level of your game, do a room code.

put this inside of it:
if not instance_exists(itemTracker)
{
instance_create(x,y,itemTracker)
}

this will create the itemTracker object when ur game starts, and will stay in the game forever until the game ends.

now inside the itemTracker object, make a create event, put this inside of it:
global.havepickedUpItemX = false; // this is a variable that we have set = to false;


and now in ur item that u want to disappear, when the player grabs it, run this code: global.havepickedUpItemX = true;

1 more step: in the same object u want to make disappear, make a room_start function and put this code inside:
if ( global.havepickedUpItemX == true )
{
instance_destroy()
}

let me know what happens
 
L

lord_berto

Guest
@lord_berto - If you're using a persistent object, then the havepickedUpItemX variable should not be global.
it'll work either way, just depends how u want to access the variable. the code at the beginning prevents the objectTracker from being re-created.
 

dannyjenn

Member
it'll work either way, just depends how u want to access the variable. the code at the beginning prevents the objectTracker from being re-created.
I realize the code works, but what I'm saying is, it's pointless to use a persistent object if you're using a global variable. (See the code I posted above. It uses a global variable and does the exact same thing as the code you posted, but without the extra object.)
 
L

lord_berto

Guest
I realize the code works, but what I'm saying is, it's pointless to use a persistent object if you're using a global variable. (See the code I posted above. It uses a global variable and does the exact same thing as the code you posted, but without the extra object.)
i see what you are saying. my mistake i did not know game maker's global variables carried on throughout rooms once declared. yeah yours works perfect.
 
Top