FOnline Development > Questions and Answers
StrToInt (and StrToFloat) - WTF
JovankaB:
OK I must be just tired and not see some obvious thing. Maybe someone else will.
Here is the file:
https://dl.dropboxusercontent.com/u/3880395/wtf.fos
It's very simple:
--- Code: ---void wtf()
{
int i = 0;
StrToInt("666", i);
Log("" + i);
}
--- End code ---
compile it with asscompiler:
--- Code: ---ascompiler wtf.fos -run wtf
--- End code ---
Log outputs 0. Why i doesn't become 666?
Berko:
Looks like we can't do everything with ASCompiler.
I test it with some old ascompiler with the same result, it's not a regression or something like that. And in game all works good.
To try to understand I tested another function from string :
"int toInt( int defaultValue ) const"
Works good with ascompiler directly.
StrToInt is a binded function :
"bool StrToInt(string@+ text, int& result)" is binded to FOServer::SScriptFunc::Global_StrToInt
Where string.toInt isn't :
"ScriptString::toInt" -> "StriptString" is directly added on script engine as "string" object
Maybe that's the difference? All binded elements don't throw errors on compilation but don't necessary do what they are supposed to do?
You can try some other binded function like "Random" or "GetTick", they return me totally wrong value too.
So is it normal or not?
In fonline help we have :
--- Quote ---compiler scripts
The program is designed to validate the written scripts.
Name of the executable file: "ASCompiler.exe".
It can be used from the command line or integrated into a variety of IDE (integration examples are described in the folder with the compiler).
Command line: <filename> <library AngelScript> <library gaming API> [-p <filename, where there will be code after preprocessing>] [-d <defined symbols>]
Example: C:\client_main.fos as.dll fo_client.dll -p prep.txt -d __CLIENT
--- End quote ---
So it look like that ASCompiler only test if code can compile, and all binded function isn't really working when code is evaluated.
JovankaB:
So that's because StrToFloat is part of FOnline, not AngelScript?
I guess I will write my own StrToFloat then, it shouldn't be too difficult.
Wipe:
--- Quote from: Berko on August 03, 2014, 02:02:56 pm ---Maybe that's the difference? All binded elements don't throw errors on compilation but don't necessary do what they are supposed to do?
--- End quote ---
Basically this.
Each FOnline function have its copy just for the ASCompiler; all of these functions are empty, so they have no chance to work there. They just have to pass AngelScript compiler, and nothing more. Oh, why Log() works then? If program is started with "-run" argument, ASCompiler registers __CompilerLog(string&) function (which is nothing else than single printf() call) and masks original function by adding "#define Log __CompilerLog" to preprocessor before executing your script.
Then, as Berko said, we have some types and functions which are provided either by standard AS addons (string, array<T>, findFirst(), strlwr(), etc) or written like one of them (reflection namespace) and should work with any application using AngelScript. And, of course, during 'ASCompiler -run'
If that helps, current naming convention is:
FunctionName - FOnline function, dummy in ASCompiler (not counting Log())
functionName - generic function, should work on any target
--- Quote from: JovankaB on August 03, 2014, 02:54:07 pm ---I guess I will write my own StrToFloat then, it shouldn't be too difficult.
--- End quote ---
There was something about floats in 2238 dlls (can't check now, so only 95% sure)
JovankaB:
I already wrote my own version - StrToDouble:
--- Code: ---double StrToDouble(string@ str)
{
int length = str.length();
int position = str.length() - 1;
int count = 0;
int symbol = 0;
int value = 0;
int exponent = 0;
while (position >= 0)
{
symbol = str[position]; // Comment for FOnline revision >= 400
//symbol = str.rawGet(position); // Uncomment for FOnline revision >= 400
switch (symbol)
{
case 43: // + sign
{
break;
}
case 45: // - sign
{
value = -value;
break;
}
case 46: // . decimal point
{
exponent += -count;
break;
}
case 48: // 0
case 49: // 1
case 50: // 2
case 51: // 3
case 52: // 4
case 53: // 5
case 54: // 6
case 55: // 7
case 56: // 8
case 57: // 9
{
value += (symbol - 48) * pow(10, count);
count += 1;
break;
}
case 69: // E
case 101: // e
{
exponent = value;
value = 0;
count = 0;
break;
}
default:
{
return 0;
}
}
position -= 1;
}
return double(value) * double(pow(10, exponent));
}
--- End code ---
There is no checking if the string is a valid decimal number, except that if an unknown character is met, 0 is returned. So if you have weird stuff in the string, but made from characters that could be in a decimal number like "2.-..2", you will get some weird number (in this case 0.018). But other than that, it seems to work fine. For my stuff I validate the numberish string in another place so this function is enough.
Navigation
[0] Message Index
[#] Next page
Go to full version