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

SOLVED Random background

Viciousse

Member
Hi Everyone,

I have a backgroung sprite with 6 frames, and i want to set it random everytime that the room is loaded.

Is there a way to do that? cause i could not find anything so far.

Thank you.
 

curato

Member
easier thing is to make them 6 different backgrounds and then turn off and on which one is visible.
 

woods

Member
@curato

var lay_id = layer_get_id("Instances");
if layer_get_visible(lay_id)
{
layer_set_visible(lay_id, false);
}
else
{
layer_set_visible(lay_id, true);
}

change "instances" to "backgrounds" ?


@Viciousse
i would use a controller object..
 

curato

Member
yes, you just change instances to the name of the background you want to hide and it then you basically to an irandom then do a switch statement or an if block that makes one visible and the rest invisible.

EDIT: here you go assuming all the layers are set to not visible. I will leave you some logic work if you want to swap them around during the game.

GML:
// assuming all the backgrounds are already set to visible false
randomize();
var i; // temp var to decide
i = irandom(6);
switch(i)
{
    case 0:
        var lay_id = layer_get_id("BG1");
        break;
    case 1:
        var lay_id = layer_get_id("BG2");
        break;
    case 2:
        var lay_id = layer_get_id("BG3");
        break;
    case 3:
        var lay_id = layer_get_id("BG4");
        break;
    case 4:
        var lay_id = layer_get_id("BG5");
        break;
    case 5:
        var lay_id = layer_get_id("BG6");
        break;
}
layer_set_visible(lay_id, true);
Edit fixed the code a bit. What i get from coding from the hip i guess.
 
Last edited:

TheouAegis

Member
layer_background_sprite(layer_background_get_id("Background"), choose(bg1, bg2, bg3, bg4, bg5, bg6));

I mean, we are assuming thus is GMS2. You should specify what version you are using.
 
Last edited:

Viciousse

Member
Hi,

Working on GM2, the question is where to put that code? in the "room creation code"? because it dosent work :/
 

TheouAegis

Member
Did you assign a sprite to a background layer (called "Background" in my example) already? The background element needs to already be in place.
 

Viciousse

Member
Did you assign a sprite to a background layer (called "Background" in my example) already? The background element needs to already be in place.
Didn't work niether !

yes, you just change instances to the name of the background you want to hide and it then you basically to an irandom then do a switch statement or an if block that makes one visible and the rest invisible.

EDIT: here you go assuming all the layers are set to not visible. I will leave you some logic work if you want to swap them around during the game.

GML:
// assuming all the backgrounds are already set to visible false
randomize();
var i; // temp var to decide
i = irandom(6);
switch(i)
{
    case 0:
        var lay_id = layer_get_id("BG1");
        break;
    case 1:
        var lay_id = layer_get_id("BG2");
        break;
    case 2:
        var lay_id = layer_get_id("BG3");
        break;
    case 3:
        var lay_id = layer_get_id("BG4");
        break;
    case 4:
        var lay_id = layer_get_id("BG5");
        break;
    case 5:
        var lay_id = layer_get_id("BG6");
        break;
}
layer_set_visible(lay_id, true);
Edit fixed the code a bit. What i get from coding from the hip i guess.
I could make this work:
2020-07-13_16-02-51.png

But I am facing a strange issue: sometimes, the "Background" is picked. why so?
 

Yal

šŸ§ *penguin noises*
GMC Elder
irandom_range() is usually more readable, so you don't need to worry about whether the limits are included or not and risk those pesky off-by-one errors. Also, choose() would turn that switch statement into a single line of code: lay_id = choose("BG1","BG2","BG3","BG4") .
 
Top