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

Legacy GM [SOLVED] Generating Random Number

M

MaikForte

Guest
The results are always the same even though I invoked randomize() during initialization of the room.

So I have this simple script

Code:
var rgb1 = script_utility_randomize_rgb();
var rgb2 = script_utility_randomize_rgb();
    
show_debug_message( rgb1.red );
show_debug_message( rgb2.red );
    
show_debug_message( rgb1.blue );
show_debug_message( rgb2.blue );
    
show_debug_message( rgb1.green );
show_debug_message( rgb2.green );
And this is my script_utility_randomize_rgb()

Code:
{
    var rgb = "";
    var min_range = 150;
    var max_range = 255;
    rgb.red = irandom_range( min_range, max_range );
    rgb.blue = irandom_range( min_range, max_range );
    rgb.green = irandom_range( min_range, max_range);
    
    return rgb;
}
Sample Output:

Execute #1:
Code:
192
192
245
245
213
213
Execute #2:
Code:
233
233
242
242
225
225
Execute #3:
Code:
213
213
245
245
150
150
 

obscene

Member
Maybe this is over my head but you are creating a string called rbg, and then trying to add a variable to it? GM has no idea what are you trying to do.

Maybe you need just return an array. ( rbg[0], rbg[1], rgb[2] )
 
G

GRArthas

Guest
for random numbers i use var test = choose(5, 8, 15, 32, 40);
 
what's happening here is the empty string "", is being interpreted as an object index, so when you modify .red, you are creating a variable ".red" and assigning it to an instance of whatever object index is pointed to by "", which is probably object index 0.

what you need to do is create some kind of a structure to hold the rgb components, like an array.

rgb[0] = irandom_range( min_range, max_range );
rgb[1] = irandom_range( min_range, max_range );
rgb[2] = irandom_range( min_range, max_range );
 
M

MaikForte

Guest
Seems like he's trying to treat it like a class object, which doesn't work in GML because it is not OOP.
Correct. I know a few Java and learning OOP concepts. I'm trying if I can create variable in this way.
 
M

MaikForte

Guest
what's happening here is the empty string "", is being interpreted as an object index, so when you modify .red, you are creating a variable ".red" and assigning it to an instance of whatever object index is pointed to by "", which is probably object index 0.

what you need to do is create some kind of a structure to hold the rgb components, like an array.

rgb[0] = irandom_range( min_range, max_range );
rgb[1] = irandom_range( min_range, max_range );
rgb[2] = irandom_range( min_range, max_range );
Will try this when I get home.

By the way, is there a much more clean solution like JSON or something like that. In that case, I wouldn't be confused which color is which while maintaining the codes.
 
M

MaikForte

Guest
This is to confirm that flyingsaucerinvasion on using an array instead of creating a variable works.

Editing the thread as Solved
 
If you want it to be more readable, you can either create macros or enums to create constant values, or you can use a ds_map. Maps work similar to arrays, but take a string value for the index, so it would look like this:

Code:
color [? "red"] = 123;
Typing on my phone or I would go into a bit more detail. you can research all three ideas in the manual though.
 
Top