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

GML Help with puzzle game sprite randomization

U

Unofficial Horse Extract

Guest
I am trying to make one of those tiled puzzle games like Huniepop, but I can't figure out how to randomize the tiles. I tried this code first in a create event:
Code:
var character;
character = random(2);
if character == 2{
    sprite_index = placeHolderTile2;
}else if character ==1{
    sprite_index = placeHolderTile;
}else{
    sprite_index = Kiibo;
};
However, the image only defaults to Kiibo, and I don't think this takes into account individual objects of the same type. Any help would be appreciated.
 
S

SVG

Guest
are you talking about alternating tile colors?

If so then I can definitely help, I'll just copy my code and give it to you :)
 
U

Unofficial Horse Extract

Guest
are you talking about alternating colors?
No, I mean alternating sprites. For example, my Icon is the one named Kiibo, and the placeholder tiles would be other characters randomly placed on the board.
 
U

Unofficial Horse Extract

Guest
are you talking about alternating tile colors?

If so then I can definitely help, I'll just copy my code and give it to you :)
I could also use that code you mentioned with minor alterations though. Thank you for responding.
 
S

SVG

Guest
However, the image only defaults to Kiibo, and I don't think this takes into account individual objects of the same type. Any help would be appreciated.
i know why, it's the character = random(2); that you're using. Look up in the help manual in GM and read about random(), it will randomly pick any number between 0 and 2 as u asked it to, but the result can be 0.1412524 for example, the chances of it hitting on 2 or 1 is like 1 in a billion lol

Edit: maybe use
character = choose(0, 1, 2);
 
S

SVG

Guest
Here's a code for your alternating tile colors, this is if you have only 2 colors you want to alternate. By tile I mean game board tile, not GM version of tiles :p Just hope it helps somehow.

Code:
//Alternating tile colors
var posx, posy;
ary_tiles = 0;
with(obj_tile){
//sw & sh are my own variables for a standard sprite width and height for my tiles.
    posx = (x - xleft) / other.sw;
    posy = (y - yup) / other.sh;
   
    other.ary_tiles[posy, posx] = id;
}
var c = 0;
for(var i = 0; i < array_height_2d(ary_tiles); i++){
    for(var ii = 0; ii < array_length_2d(ary_tiles, i); ii++){
        if ary_tiles[i, ii] < 1 {
            ary_tiles[i, ii] = 0;
        } else {
            if (c + i) mod 2 == 0 {
                ary_tiles[i, ii].image_index = 0;
            } else {
                ary_tiles[i, ii].image_index = 1;
            }
        }
        c++;
    }
}
 
U

Unofficial Horse Extract

Guest
I will take some time to look at your second code but with the first option, I consistently get this image on my test board.
upload_2017-4-7_19-5-21.png
It's better than the first try, but I have no clue why it would keep showing up like this.
 
S

SVG

Guest
what do you mean? As is you restart the game and it does this everytime? If so then that's because GM when you hit play defaults to one "seed" and will generate randomized events, or whatever you wanna call them, exactly the same every time you hit that green play button. Try this but there are other ways of doing it too:

Code:
if keyboard_check_pressed(vk_escape) {
    room_restart();
}
put that in a step event and run your game, then press esc and it will restart the room every time you hit it, recreating different randomized tiles.

EDIT: Yep you got it :) for me I like having it not randomize() the seed just to I can do something over again to recreate a potential bug, then when you export your game it automatically randomizes it and you won't have to have that code in there.
 
Top