[SOLVED] is_real not returning false for a non-number

D

Darth Binary 1010

Guest
Code:
var test = noone

if (is_real(test)) {
  show_debug_message("rawr")
}
Why does this if statement return true? Should I be using "noone" for blank variables and resetting a variable to nothing? Also, should I be using is_real to check if a variable is a number or not?
 
D

dugtrioramen

Guest
Code:
var test = noone

if (is_real(test)) {
  show_debug_message("rawr")
}
Why does this if statement return true? Should I be using "noone" for blank variables and resetting a variable to nothing? Also, should I be using is_real to check if a variable is a number or not?
Probably not, you usually use noone when you know that the variable will hold an instance id at one point
Just use:

var test = ""

And then your if statement will work.
 

Pfap

Member
Code:
var test = noone

if (is_real(test)) {
  show_debug_message("rawr")
}
Why does this if statement return true? Should I be using "noone" for blank variables and resetting a variable to nothing? Also, should I be using is_real to check if a variable is a number or not?


I'm not sure what you mean by blank variables, but when first typing a variable that will eventually hold an instance id I would set it to noone.

In the create event
Code:
//In the create event 


//when I tap this instance it will hold the id of the tapped instance
global.id_store = noone;


//in the tap event

global.id_store = id;

If I was using any other data I normally start my variables that currently don't have an explicit value to 0 or -1.

Also, using is real just checks the data type. For instance, this is a string: "1" one, while this is a real: 1 one.
A practical example would be that you have a players score which is the real 4550, but you want to display it on a leaderboard with their name which is "John".

Code:
//this is a string
player = "John"; 


//this is a real
player_score = 4550;



//now in order to get a string that reads "John: 4550" we need to change player score into a string. 
string_score = string(4550);

//now we can make a string for displaying
display_string = player + ":\n"+string score;

//the \n adds a line break so you would get John: and the score would be drawn on the next line.



//maybe you save there string on a database and need to turn it back into a real then check to make sure it's a real so you don't get a crashing error because you can't add a string to a real


/*
the below would be set to some data received from the database and not typed out more like:
incoming_string = path;
maybe taken from the http async event and as such we wouldn't be entirely sure what the variable path held until we test it

but for an example
*/
incoming_string = "4550";

if !is_real(incoming_string){ 

//if it is not real we turn it into a real here

real_score = real(incoming_string);

}

If you're trying to get just numbers from a string lets say "abc123" you could use the function string_digits("abc123"); and it would return the string "123" and then you could change that into the real 123 with real("123");.
 
Top