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

GameMaker Object Random spawn locations

J

jiminy123

Guest
I have put a chest in my game, when opened it drops potions and gold. I am trying to make them spawn randomly around the chest when opened but they always spawn in the same place.

this is the code I am using for when the player presses "F" near the chest.

if (distance_to_object(oPlayer) < 32 ) and (image_index = 0)
{
image_index = 1;
instance_create_layer(x+-irandom_range(48,64),y+-irandom_range(48,64), "Objects", oHpPotion);
instance_create_layer(x+-irandom_range(48,64),y+-irandom_range(48,64), "Objects", oManaPotion);
instance_create_layer(x+-irandom_range(48,64),y+-irandom_range(48,64), "Objects", oCoin);

}


I have randomize(); in the create event for the chest, have tried putting it in the keyboard check event and in a step event. Items always spawn in the same place on top of each other.
 
Last edited by a moderator:

Tthecreator

Your Creator!
Okay a few things to try:
try making the random range bigger (like from -100 to +100) and see if that makes any difference.
Also do you by any chance have any code in the create event of any of these objects that might change their position?
Maybe, for testing you could have some code like:

Code:
//I haven't tested this code, there might be some typos in here:

hpPotionX = x+irandom_range(-100,100);
hpPotionY = y+irandom_range(-100,100);

manaPotionX = x+irandom_range(-100,100);
manaPotionY = y+irandom_range(-100,100);

show_message("positions: hp: ("+string(hpPotionX)+";"+string(hpPotionY)+") mana: ("+string(hpPotionX)+";"+string(hpPotionY)+")")

instance_create_layer(x+hpPotionX,y+hpPotionY, "Objects", oHpPotion);
instance_create_layer(x+manaPotionX,y+manaPotionY, "Objects", oManaPotion);
the message it shows should give you random numbers.
If not somethings else may be happening.
If you get random numbers but the items still spawn at the same place, you might have some other code somewhere else interfering with this code.
 
J

jiminy123

Guest
Okay a few things to try:
try making the random range bigger (like from -100 to +100) and see if that makes any difference.
Also do you by any chance have any code in the create event of any of these objects that might change their position?
Maybe, for testing you could have some code like:

Code:
//I haven't tested this code, there might be some typos in here:

hpPotionX = x+irandom_range(-100,100);
hpPotionY = y+irandom_range(-100,100);

manaPotionX = x+irandom_range(-100,100);
manaPotionY = y+irandom_range(-100,100);

show_message("positions: hp: ("+string(hpPotionX)+";"+string(hpPotionY)+") mana: ("+string(hpPotionX)+";"+string(hpPotionY)+")")

instance_create_layer(x+hpPotionX,y+hpPotionY, "Objects", oHpPotion);
instance_create_layer(x+manaPotionX,y+manaPotionY, "Objects", oManaPotion);
the message it shows should give you random numbers.
If not somethings else may be happening.
If you get random numbers but the items still spawn at the same place, you might have some other code somewhere else interfering with this code.
haven't got anything in the items to effect their position, am just eating at but will try the other stuff u suggested when i get a chance, thanks :)
 
J

jiminy123

Guest
You use +- and I'm not sure what that does but it might be a problem.
i think it means that it can be a negative or positive number. i might be wrong though. But I have tried it with just a + and had the exact same issues :/
 
J

jiminy123

Guest
Okay a few things to try:
try making the random range bigger (like from -100 to +100) and see if that makes any difference.
Also do you by any chance have any code in the create event of any of these objects that might change their position?
Maybe, for testing you could have some code like:

Code:
//I haven't tested this code, there might be some typos in here:

hpPotionX = x+irandom_range(-100,100);
hpPotionY = y+irandom_range(-100,100);

manaPotionX = x+irandom_range(-100,100);
manaPotionY = y+irandom_range(-100,100);

show_message("positions: hp: ("+string(hpPotionX)+";"+string(hpPotionY)+") mana: ("+string(hpPotionX)+";"+string(hpPotionY)+")")

instance_create_layer(x+hpPotionX,y+hpPotionY, "Objects", oHpPotion);
instance_create_layer(x+manaPotionX,y+manaPotionY, "Objects", oManaPotion);
the message it shows should give you random numbers.
If not somethings else may be happening.
If you get random numbers but the items still spawn at the same place, you might have some other code somewhere else interfering with this code.
i tried moving it to the objects themselves and same issue arises :/
 

TheouAegis

Member
A +- B means add the negation of B to A. So in your original code, all you were doing was subtract {48,64} from x and y. That's basic math.
 
Top