1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

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

Discussion in 'Programming' started by SirCaliber, Feb 13, 2020 at 9:30 PM.

  1. SirCaliber

    SirCaliber Member

    Joined:
    Dec 27, 2019
    Posts:
    9
    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.
     
  2. Nidoking

    Nidoking Member

    Joined:
    Nov 20, 2019
    Posts:
    114
    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.
     
  3. SeraphSword

    SeraphSword Member

    Joined:
    Aug 16, 2016
    Posts:
    54
    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.
     
  4. SirCaliber

    SirCaliber Member

    Joined:
    Dec 27, 2019
    Posts:
    9
    I'm not too sure how randomize() works.
     
  5. SirCaliber

    SirCaliber Member

    Joined:
    Dec 27, 2019
    Posts:
    9
    Setting the hsp to 5 when oLoot is created doesn't change anything when created by the chest. Same result.
     
  6. Kyon

    Kyon Member

    Joined:
    Jun 27, 2016
    Posts:
    510
    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 likes this.
  7. SirCaliber

    SirCaliber Member

    Joined:
    Dec 27, 2019
    Posts:
    9
    thanks
     
  8. SirCaliber

    SirCaliber Member

    Joined:
    Dec 27, 2019
    Posts:
    9
    So does randomize() randomize every single thing in the game?
     
  9. Kyon

    Kyon Member

    Joined:
    Jun 27, 2016
    Posts:
    510
    yes so make sure you only use it once. Like only when the game starts. (you dont have to do it every room only once in a game)
     
  10. Nidoking

    Nidoking Member

    Joined:
    Nov 20, 2019
    Posts:
    114
    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 likes this.
  11. SirCaliber

    SirCaliber Member

    Joined:
    Dec 27, 2019
    Posts:
    9
    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.
     

Share This Page