• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Formulas returning NaN [SOLVED]

R

RealsLife

Guest
I've This code

Code:
event_inherited(); //parent gives his code
slider_value = obj_options_menu_controller.surf_height;
instance_create(x,y,obj_vbar);
obj_vbar_parent.scroll_power = 20;
obj_vbar_parent.slider_px_height = sprite_height;
obj_vbar_parent.real_slider_height = slider_value;
real_surface_position = obj_vbar_parent.real_surface_position;
Inside the obj_vbar

Code:
//scrollbar limits
topLimit = obj_vslider_parent.topLimit;
bottomLimit = obj_vslider_parent.bottomLimit;

//bar start position
y = topLimit;
scroll_power = 10; //scrollpower
grab = false;
slider_px_height = 0; //hoogte sprite obj_vslider_parent.sprite_height;
real_slider_height = 0; //echte waarde scaled met hoogte sprite obj_vslider_parent.slider_value;

//real surface height variables scaled for your scrollbar
scroll_scale = real_slider_height/slider_px_height;
real_surface_position = 0;
px_on_slider = 0;
Now when drawing the variables it looks like assigning them to variables inside obj_bar works but the formulas inside object bar just return NaN. Like you see the default variables are 0 and 0/0 returns NaN(Or at least I think that's what causes it). Also object_bar has a step event where the formules are used and real_surface_postion is the whole time changed based on the position.

How can I make this work? I just want the static obj_bar variables to be changed from another object so that for every different slider I make I can just use a prent a few variables and done.
 

Roderick

Member
0/0 returns NaN
You cannot divide by zero; it's a mathematical impossibility. There is no solution to the equation, so it returns "NaN", or "Not a Number". Change slider_px_height to a non-zero number, and it should run without that error.
 
R

RealsLife

Guest
You cannot divide by zero; it's a mathematical impossibility. There is no solution to the equation, so it returns "NaN", or "Not a Number". Change slider_px_height to a non-zero number, and it should run without that error.
I know I said it myself that's why I'm asking how I can I assign the numbers given... They should not be 0 it's just a default to declare them the real numbers are the ones I give in code part 1
 

Bingdom

Googledom
I think what you can use is is_real()
I haven't tested but you could do something like this:
Code:
if !is_real(value) {
    value = 0;
}
 
R

RealsLife

Guest
I think what you can use is is_real()
I haven't tested but you could do something like this:
Code:
if !is_real(value) {
    value = 0;
}
Just default them to some other value, like 1.
I've changed the values to 1 and I don't get NaN but my object still doesn't get the values I've assigned to them :(...

EDIT: I don't think !is_real(value) can work or at least not in my case. The values come from another object and every slider will be a different object. So gettings the values from that object would be somthing like !is_real(obj_slider.value) but the obj_slider will be different in all cases.
 

Tsa05

Member
Edit: Updating, since realizing that there's one way to do this, not 2.

If I'm reading this correctly, you need to use an alarm_event.

It looks like your code does this in the create event:
1) Set a value
2) Do a calculation based on the value

And here's what happens when you run your code:
1) Create an object called obj_vbar_parent
2) Ooh, an object was created, let's do it's create event code!
3) Set a value
4) Do a calculation
5) Set some values in obj_vbar_parent

So, the calculation is done before the values change.

The way around this:
obj vbar creation code
Code:
//scrollbar limits
topLimit = obj_vslider_parent.topLimit;
bottomLimit = obj_vslider_parent.bottomLimit;

//bar start position
y = topLimit;
scroll_power = 10; //scrollpower
grab = false;
slider_px_height = 0; //hoogte sprite obj_vslider_parent.sprite_height;
real_slider_height = 0; //echte waarde scaled met hoogte sprite obj_vslider_parent.slider_value;

real_surface_position = 0;
px_on_slider = 0;

alarm[0]=1
Then, in event Alarm 0:
Code:
scroll_scale = real_slider_height/slider_px_height;
This *should* solve the problem
 
Last edited:

TheouAegis

Member
ALL CODE inside a create event is run before the program will continue. There is no overlapping code execution in GM. You cannot run two scripts at once. You cannot run two events at once. You cannot run two objects' codes at once. Maybe there is a DLL or whatever that would let you, but generally speaking - no.

The instance_create() function interrupts the current code to run the Create Event of the new instance.
 

Tsa05

Member
Ah, you're right Theou, has to use alarm instead of user_defined. Updating my previous post, since I had suggested both user_defined and alarm would work.
 
R

RealsLife

Guest
Ah, you're right Theou, has to use alarm instead of user_defined. Updating my previous post, since I had suggested both user_defined and alarm would work.
Hey woops sorry I didn't reply directly, Tsa05 yea it was actually just that formula so what I did was just moving it to the step event so It could check for the new input from my other object and in my actual new slider I also moved some formula to the step event so the objects always check for eachothers new variables even if the variable is a static one :p but, yea it works now ^_^!

EDIT: Theou yea I should just have put it in the step event so it will always check for input from the other object, I think I will add a check and if the new static variable is passed It will no longer check for it every step ^_^!
 
Top