GML [SOLVED] Objects will start with a random hsp variable, but only when created by a certain object.

SirCaliber

Member
I have an object, oCorpse, that creates a random amount of oLoot objects when it is created. In those oLoot objects' create event, they get a random hsp and vsp variable. But the hsp only works if it was created by oCorpse, not by oChest.

(oChest) On Collision with oBolt:
Code:
with(other){
    instance_destroy()   
}

if(sprite_index = sBasicChestC){
repeat(irandom_range(5,8)){
    instance_create_layer(x,y-2,"Main",oLoot)
}
}


sprite_index = sBasicChestO
hp -= 1


here's the create event for oLoot:
Code:
vsp = irandom_range(-3,-9)
image_xscale = 0.5
image_yscale = image_xscale
hsp = irandom_range(-10,10)
sprite_index = choose(sEmerald,sRuby,sCoin)
grv = 0.56
hitfrom = 0


When the chest is shot with oBolt, the oLoot objects just go straight up and fall back down. No horizontal movement whatsoever. Any help I could get would be greatly appreciated.
 

Nidoking

Member
How certain are you that nothing else is affecting that? What if you set hsp to something like just 5 instead of a random number - does it behave the same way in both cases? Is it possible that the oLoot is colliding with the oChest in such a way that the horizontal movement is stopped before it starts moving? The problem is almost certainly in the parts you haven't posted here.
 

SeraphSword

Member
Do you have randomize() somewhere in your code? If not, any "random" code will always have the same value, meaning you'll get the same results every time.
 

SirCaliber

Member
How certain are you that nothing else is affecting that? What if you set hsp to something like just 5 instead of a random number - does it behave the same way in both cases? Is it possible that the oLoot is colliding with the oChest in such a way that the horizontal movement is stopped before it starts moving? The problem is almost certainly in the parts you haven't posted here.
Setting the hsp to 5 when oLoot is created doesn't change anything when created by the chest. Same result.
 

Kyon

Member
I'm not too sure how randomize() works.
start your game with somewhere in some create event with "randomize();" otherwise it'll use the same random seed everytime you start the game.

From the doc:
This function sets the seed to a random value. Should you need to keep a consistent value over a number of runs of a game, you should be using random_set_seed(). Please note, that when using the random number functions in GameMaker Studio 2 the initial seed is always the same, as this makes tracing errors and debugging far easier. Should you wish to test with true random, you should call this function at the start of your game. The function will return the new randomised seed value (an unsigned 32bit integer).
 

SirCaliber

Member
start your game with somewhere in some create event with "randomize();" otherwise it'll use the same random seed everytime you start the game.

From the doc:
This function sets the seed to a random value. Should you need to keep a consistent value over a number of runs of a game, you should be using random_set_seed(). Please note, that when using the random number functions in GameMaker Studio 2 the initial seed is always the same, as this makes tracing errors and debugging far easier. Should you wish to test with true random, you should call this function at the start of your game. The function will return the new randomised seed value (an unsigned 32bit integer).
thanks
 

SirCaliber

Member
start your game with somewhere in some create event with "randomize();" otherwise it'll use the same random seed everytime you start the game.

From the doc:
This function sets the seed to a random value. Should you need to keep a consistent value over a number of runs of a game, you should be using random_set_seed(). Please note, that when using the random number functions in GameMaker Studio 2 the initial seed is always the same, as this makes tracing errors and debugging far easier. Should you wish to test with true random, you should call this function at the start of your game. The function will return the new randomised seed value (an unsigned 32bit integer).
So does randomize() randomize every single thing in the game?
 

Nidoking

Member
This is clearly not a failure to call randomize (although that is something you should do if you're going to generate random numbers), because even if the random seed were the same, it wouldn't be generating zero over and over.

There's something in your game setting the hsp back to zero, and you haven't shown it to us. Look for everything that manipulates the hsp of an oLoot. One of those things is the culprit.
 

SirCaliber

Member
I've solved it! oLoot objects were colliding with the wall and setting their hsp to 0 immediately. Instead of setting hsp to 0 when they collide, I set it to hsp = -hsp * 0.7, so it bounces back a little bit. This solved the problem perfectly, and also created a nice little bouncing effect! Thanks for your help everyone! I even learned a little something about randomization.
 
Top