I need IsFloat (like a IsNum)

Started by Banaqs, Dec 02, 2014, 05:50 PM

Previous topic - Next topic

Banaqs


.

This functionality is built into the Squirrel language

local var1 = 5632;
local var2 = 3.5434;

if (typeof var1 == "float") print("var1 is float");
else print("var1 is not float");

if (typeof var2 == "float") print("var2 is float");
else print("var2 is not float");
.

Banaqs


Banaqs

I have a problem....

else if (cmd == "setspeed")
{
if (!text) MessagePlayer("Type: /"+cmd+" [float]",player);
else if (typeof text != "float") MessagePlayer("Type: /"+cmd+" [float]",player);
else
{
Message(player.Name+" change game speed to ( "+text.tofloat()+" )");
SetGamespeed(text.tofloat());
}
}

Every time (when i wrote "/setspeed 1.2") return message from this line

else if (typeof text != "float") MessagePlayer("Type: /"+cmd+" [float]",player);

.

I'm assuming this is a command for admins/moderators/scripters etc. ?

else if (cmd == "setspeed")
{
// See if the command is populated
if (!text){
// Inform the invoker to correct his command
MessagePlayer(format(@"Syntax: %s <float>", cmd), player);
} else {
// Allocate temporary storage
local speed = null;
// Attempt to convert the text to a float
try {
speed = text.tofloat();
} catch (error) {
// Inform the invoker
MessagePlayer(format(@"Error: %s (%s)", error, text), player);
}
if (speed != null)
{
// Apply the extracted value
SetGamespeed(speed);
// Inform the invoker about the result
MessagePlayer(format(@"Result: speed changed to %f", speed), player);
}
else
{
// Inform the invoker to correct his command
MessagePlayer(format(@"Syntax: %s <float>", cmd), player);
}
}
 }
.

Banaqs

When i use

/setspeed f

in console i have a error

CALLSTACK
AN ERROR HAS OCCURED [cannot convert the string]

in line 2597:

2596:  try {
2597:   speed = t.tofloat();
2598:  } catch (error) {
2599:   // Inform the invoker
2600:   MessagePlayer(format(@"Error: %s (%s)", error, t), p);
2601:  }

.

Quote from: Banaqs on Dec 03, 2014, 07:08 PMWhen i use

/setspeed f

in console i have a error

CALLSTACK
AN ERROR HAS OCCURED [cannot convert the string]

in line 2597:

2596:  try {
2597:   speed = t.tofloat();
2598:  } catch (error) {
2599:   // Inform the invoker
2600:   MessagePlayer(format(@"Error: %s (%s)", error, t), p);
2601:  }

How can you get that when the expression is in a try-catch block? You should receive the error as a message in the game but other than that nothing should happen in the script and the error should be handled automatically. Are you sure you got the code right?

And how do you expect to convert "f" to a floating point number? The command is /setspeed #.##.... where # are decimal numbers.
.