( Resolved ) How do I make sure that each time I launch the game that my item is a random color?

Z

Zoltars55

Guest
Hello

How do I make sure that each time I launch the game that my item is a random color? thank you.
 
Last edited by a moderator:

TsukaYuriko

☄️
Forum Staff
Moderator
First, randomize the seed by calling randomize.

Next, assign a color by using the irandom function with c_white passed as an argument. This will create a random color out of all possible colors (due to the way colors are stored, white has the highest possible value, therefore you'll be creating a random color between the lowest possible and highest possible one).
 

FrostyCat

Redemption Seeker
To set a random color to an object please
That's not specific enough a description. There are 16777216 colours to choose from, and countless ways to pick random colours from that set. You have to understand your needs first, then devise your own strategy as to which ones you want and pick accordingly.

Assume image_blend for the target, and that you have already called randomize() as TsukaYuriko instructed. Here are 3 common methods:

Picking from a predefined set:
GML:
image_blend = choose(c_red, c_yellow, c_green);
Generating a random colour (This is equivalent to TsukaYuriko's method using irandom(c_white)):
GML:
image_blend = make_color_rgb(irandom(255), irandom(255), irandom(255));
Generating a random colour of uniform saturation and value (i.e. picking from a rainbow):
GML:
image_blend = make_color_hsv(irandom(255), 255, 255);
 
What both Tsuka and Frosty said are exactly how you would accomplish this. Put one of those code blocks in the Create Event (with randomize() preceding it) and that is literally it. No-one's going to make a video tutorial to explain something this simple, and a text tutorial would be literally what has already been written.
 
  • Like
Reactions: KPJ

TsukaYuriko

☄️
Forum Staff
Moderator
Please point out what you are having difficulties to understand about the previous posts, especially FrostyCat's. I can by no stretch of the imagination see how it could have been made any clearer.
 

GameDevDan

Former Jam Host
Moderator
GMC Elder
  1. create an object
  2. give it a "create" event with a block of code in it
  3. in the block of code write the following:
GML:
image_blend = irandom(c_white);
4. put the object in the room
5. press play
 
Z

Zoltars55

Guest
Hi
GameDevDan
I did what you said, but I can't see anything when I run the game. Should I create a sprite?

1 -- I create a white sprite by default
2 -- I create an object to which I attribute my sprite and place it in the room.
it's after I don't know how to put on each lacement of the game that my object changes color.
 

KPJ

Member
Yes, image_blend is a sprite related keyword, so you are going to have to create a sprite and assign it to your object.

Edit: After reading your last edit, could you please expand on the issue you are having as I'm having a hard time understanding what you said 👍
 
Z

Zoltars55

Guest
HI Kpj
What I'm trying to do is make my sprite change color every time I launch the game.
 

KPJ

Member
Oh, I know about your goal, I was talking about what you meant by this
it's after I don't know how to put on each lacement of the game that my object changes color.
Also, could you please post the code that you've tried? Thanks
 

sylvain_l

Member
you are taking shortcut you shouldn't, in your create event all you have is
GML:
image_blend =  random(255);
that's going to give you a color black or very near, (so logically you don't see it on a black background; change the background color to white so you'll see your black object)

as TsukaYuriko told you you need to use the highest value for a color which is c_white

GML:
image_blend =  irandom(c_white );
or as FrostyCat suggested you can build it

GML:
image_blend = make_color_rgb(irandom(255), irandom(255), irandom(255));
 

XonicCraft

Member
That's not specific enough a description. There are 16777216 colours to choose from, and countless ways to pick random colours from that set. You have to understand your needs first, then devise your own strategy as to which ones you want and pick accordingly.

Assume image_blend for the target, and that you have already called randomize() as TsukaYuriko instructed. Here are 3 common methods:

Picking from a predefined set:
GML:
image_blend = choose(c_red, c_yellow, c_green);
Generating a random colour (This is equivalent to TsukaYuriko's method using irandom(c_white)):
GML:
image_blend = make_color_rgb(irandom(255), irandom(255), irandom(255));
Generating a random colour of uniform saturation and value (i.e. picking from a rainbow):
GML:
image_blend = make_color_hsv(irandom(255), 255, 255);
Thanks, I used the latest code and it helped me a lot
 
Top