GameMaker switching colors randomly

zim32

Member
I am randomly generating a level and trying to get it to randomly pick the bricks color each time it creates another brick put while it a randomly select a color it will only be one color through out the entire thing I can't figure out why relevant code below
the level obj in the create event
Code:
randomize();
columon=irandom_range(5,35)
for(i=1;i<columon;i++){
starttile=irandom_range(0,4)
if(starttile=0){
xxstart=0
}
else
{
if(starttile=1){
xxstart=29
}
else
{
if(starttile=2){
xxstart=58
}
else
{
if(starttile=3){
xxstart=87
}
else
{
if(starttile=4){
xxstart=116
}
}
}
}
}
row=irandom_range(1,65)
for(j=1;j<row;j++){
xx=(j-1)*29
yy=(i-1)*14
global.tilecolor=irandom_range(1,3)
instance_create_layer(xx+xxstart,yy,"Instances",obj_tile)
}
}
the brick in the draw event
Code:
if(global.tilecolor == 1)
{
    draw_self();
}

else
{
    if(global.tilecolor == 2)
    {
    draw_sprite(spr_tileblue,0,x,y)
    }
else
{
if(global.tilecolor==3){
draw_sprite(spr_tilegreen,0,x,y)
}
}
    
}
 

TheouAegis

Member
It's because you're setting the brick color to a global variable, not an instance variable inside each of those bricks.
 

zim32

Member
It's because you're setting the brick color to a global variable, not an instance variable inside each of those bricks.
so you're saying that cause it's a global after it draws it as one color when the global switches it redraws it as the new color I guess that make sense for some reason I thought the draw event was a one time thing
 

zim32

Member
I was trying to fix it and I got a strange error I can't possibly see what the problem could be
Object: obj_tile Event: Draw at line 17 : unknown function or script draw
brick create event
Code:
if(global.tilecolor == 1)
{
    brickcolor = 1;
}

else
{
    if(global.tilecolor == 2)
    {
        brickcolor = 2;
    }

    else
    {
        if(global.tilecolor == 3)
        {
            brickcolor = 3;
        }
    }
}
 

TheouAegis

Member
it never changes the global variable. You set the global variable inside that loop when you create the bricks. That's the only time it gets changed. That is done before the drawing. The very last value of the variable is what gets drawn.
 

zim32

Member
it never changes the global variable. You set the global variable inside that loop when you create the bricks. That's the only time it gets changed. That is done before the drawing. The very last value of the variable is what gets drawn.
wait it creates all the bricks before it draws them ? also do you know the answer to my newest problem
Object: obj_tile Event: Draw at line 17 : unknown function or script
brick create event
Code:
if(global.tilecolor == 1)
{
    brickcolor = 1;
}

else
{
    if(global.tilecolor == 2)
    {
        brickcolor = 2;
    }

    else
    {
        if(global.tilecolor == 3)
        {
            brickcolor = 3;
        }
    }
}
no idea what the problem could be
 

TheouAegis

Member
I would say copy and paste the actual error. With all of its line formatting and everything. But at a guess, I would say that you are showing me the wrong code that the error is pointing to and that perhaps by the sounds of the error you meant to comment something out and forgot a slash.

that code that you did post though if it's in the create event of the brick should be fine in the brick even with that global variable. That's essentially what I was saying to do. But your error is in the draw event of the brick, so you need to post the code for the draw a event of the brick.
 

zim32

Member
I would say copy and paste the actual error. With all of its line formatting and everything. But at a guess, I would say that you are showing me the wrong code that the error is pointing to and that perhaps by the sounds of the error you meant to comment something out and forgot a slash.

that code that you did post though if it's in the create event of the brick should be fine in the brick even with that global variable. That's essentially what I was saying to do. But your error is in the draw event of the brick, so you need to post the code for the draw a event of the brick.
I did copy paste the error when I ran it in the compile errors part it gave me
Object: obj_tile Event: Draw at line 17 : unknown function or script draw
Also it hast to be the code I showed you cause I didn't get this error until I created that code
 

zim32

Member
I would say copy and paste the actual error. With all of its line formatting and everything. But at a guess, I would say that you are showing me the wrong code that the error is pointing to and that perhaps by the sounds of the error you meant to comment something out and forgot a slash.

that code that you did post though if it's in the create event of the brick should be fine in the brick even with that global variable. That's essentially what I was saying to do. But your error is in the draw event of the brick, so you need to post the code for the draw a event of the brick.
nvm i found the problem it was something really stupid
but anyway can you answer my earlier question are you saying it creates all the bricks before it draws them ?
 

zim32

Member
I would say copy and paste the actual error. With all of its line formatting and everything. But at a guess, I would say that you are showing me the wrong code that the error is pointing to and that perhaps by the sounds of the error you meant to comment something out and forgot a slash.

that code that you did post though if it's in the create event of the brick should be fine in the brick even with that global variable. That's essentially what I was saying to do. But your error is in the draw event of the brick, so you need to post the code for the draw a event of the brick.
well it works now thanks for the help
 

TheouAegis

Member
So the first code that you posted in your original post wouldn't work because you were using the global variable to determine what to draw in the draw event. The global variable was only getting set one time, so all bricks would use the same value. That code that you posted for the create event of the bricks would be fine because you are storing the value of that global variable inside the bricks themselves as they are created.
 
N

nickvm98

Guest
Do you have like 4 different tilesets for each colour? Definitely look into palette swapping. It'll save memory rather than storing 4 different tilesets.
 
Top