Difficulty with the random_range function --or maybe something else?

D

DomiShop

Guest
Hello everyone,

I am posting in the hopes that some kind soul out there can assist with a problem that has me thoroughly stumped. I'm having some issues with a bit of code I've intended to use for a weather system.

I'm tinkering with a basic top-down project where the character can move 8 directions on a 2D plane. The camera scrolls around the room to follow the player. I want to have a rain/snow/etc. system where the rain or snow sprites follow the camera instead of just spawning the rain objects in the entire room, since the rooms can get pretty large and it would become really inefficient. I made two very basic scripts to check the View's current X positions on both the leftmost part of the screen and the rightmost part of the screen, since I need to check these values all the time anyway:

Script "GetViewX":
Code:
return camera_get_view_x(camera_get_active());
Script "GetViewWidthX":
Code:
return camera_get_view_width(camera_get_active()) + GetViewX();
These scripts seem to work fine. As a debugging tool, I have both of these scripts print to the screen as int values, and they always show that no matter where the camera goes, the difference between "GetViewX" and "GetViewWidthX" is always 640. When the camera is as far left as it can go, the debug printing shows "GetViewX" is 0, and "GetViewWidthX" is 640. When shifted one pixel to the right, "GetViewX" instantly changes to 1, and "GetViewWidthX" changes to 641.

The trouble comes when I try to spawn a set number of raindrops between the values of GetViewX and GetViewWidthX, by using "random_range". Before I spawn the rain objects, I store the rain's x position in what is supposed to be a random variable:

Code:
rain_x = random_range(GetViewX(), GetViewWidthX());
When running the game and printing rain_x's value to the screen, rain_x is always between -2 and -1 for every instance of the rain object spawned, and therefore, it doesn't even show up on the screen. I've tried all kinds of variants on the code, but nothing seems to work. I have used random_range elsewhere with success, so I know I must be doing something incorrectly here. It's probably blindingly obvious, but I just don't know what the problem is.

The debug print shows with all the above code, when the character is standing as far left as possible "GetViewX" is 0, "GetViewWidthX" is 640, but rain_x is always some random value between -1 and -2. How is this possible?

Thank you all in advance for your assistance, and thank you for taking the time to read through what is probably a really obvious problem.
 

chamaeleon

Member
The debug print shows with all the above code, when the character is standing as far left as possible "GetViewX" is 0, "GetViewWidthX" is 640, but rain_x is always some random value between -1 and -2. How is this possible?
Have you tried using stuff like the below to really ensure you are seeing a truly unexpected behavior in random_range() and the fault is not in the arguments it is given?
Code:
show_debug_message(random_range(0, 640));
a = GetViewX();
b = GetViewWidthX();
c = random_range(a, b);
show_debug_message(string(a) + " " + string(b) + " " + string(c));
You talk vaguely about debug output, but you didn't show what those statements are or what the output is.
 
D

DomiShop

Guest
Hello chamaeleon,

First, thank you so much for replying. I'm going to incorporate and play with the code you've given me to see if I can glean anything new about the situation.

My debug code to print the variables is contained within the "Draw" event of my main controller object, and it looks like this:

Code:
with (weather_controller)
        { 
            draw_text(obj_Player.x +10, obj_Player.y + -50, "GetViewX " + string (GetViewX()));
            draw_text(obj_Player.x +10, obj_Player.y + -60, "GetViewWidthX " +string(GetViewWidthX()));
            draw_text(obj_Player.x +10, obj_Player.y + -70, "Rain X Pos" + string(rain_x));
          
        }
On any given step, the output drawn next to the player looks something like this:

GetViewX 0
GetViewWidth 640
Rain X Pos -1.78

EDIT:

To follow up, I tried your code...and it works. Which seems to make no sense at all, but here's the debug output:

80.02
0 640 436.53
 
Last edited by a moderator:
Might be because chameleon is storing the output of the scripts in a temp variable, rather than using the scripts within the random_range function. Just do that and you should be fine.
 
D

DomiShop

Guest
Might be because chameleon is storing the output of the scripts in a temp variable, rather than using the scripts within the random_range function. Just do that and you should be fine.
That's exactly what I thought at first, too, so I modified my code. But I still get the same result:
Code:
rain_a = GetViewX();
rain_b = GetViewWidthX();
rain_x = random_range(rain_a, rain_b);
The above code still results in a value of something between -1 and -2 for rain_x in my debug printout.
 
Then I'd say rain_x is actually being manipulated somewhere else. Try stepping through the debugger and seeing what value it is immediately after creation. It might be getting changed by something else at some other point.
 
D

DomiShop

Guest
Then I'd say rain_x is actually being manipulated somewhere else. Try stepping through the debugger and seeing what value it is immediately after creation. It might be getting changed by something else at some other point.
I gave it a shot, but I can't find anything else that could be changing it. I even tried renaming the rain_x variable to something really crazy (like "this_is_definitely_a_new_variable" or something close to that) to make sure it wasn't named as a variable somewhere else.
 
D

DomiShop

Guest
As an update, I (kind of) "solved" the problem by making the rain spawn relative to the player rather than the view by doing this:


Code:
rain_x = random_range(obj_Player.x -500, obj_Player.x +500);

but I'm still very curious as to what the problem with my original code could have been. I'm still completely stumped. I can't tell you both how much I appreciate your input, though.
 

TheouAegis

Member
Out of curiosity, your code, your own code, was using the draw a event for debugging. Chameleon's code was using message outputs for debugging. That would suggest that there is an object or instance somewhere in your project which is resetting rain_x to either a negative value or to some built-in constant like self or other before the draw event is run.
 
D

DomiShop

Guest
Out of curiosity, your code, your own code, was using the draw a event for debugging. Chameleon's code was using message outputs for debugging. That would suggest that there is an object or instance somewhere in your project which is resetting rain_x to either a negative value or to some built-in constant like self or other before the draw event is run.
I have to agree, and that seems like the only explanation for why it wasn't working, but I don't know how to explain the test run where I renamed the variable "rain_x" to something completely unique.
 

TheouAegis

Member
Oh yeah you mentioned that... Out of curiosity, did you change the variable name in the code that sets rain_x AND the code that draws rain_x? I'm just making sure you didn't change the code that sets it but left the code that draws it intact, so even though you did change the name of the variable in one block of code, you were still drawing the value of the old variable. I've done that before. LOL
 
D

DomiShop

Guest
Oh yeah you mentioned that... Out of curiosity, did you change the variable name in the code that sets rain_x AND the code that draws rain_x? I'm just making sure you didn't change the code that sets it but left the code that draws it intact, so even though you did change the name of the variable in one block of code, you were still drawing the value of the old variable. I've done that before. LOL
Hah -- I've absolutely done that before, too, but yes. I did change it in the spawn function as well.
 
Top