07.01.2015, 19:05
Is there anyway to add a function that determines if the given variable is a float/string/int?
There is one for PlayerVariables: GetPVarType(playerid, varname[]);
I couldn't find any function for non-player related variables. But I do think it's possible to create such function... |
stock GetVarType(variable[])
{
new length = strlen(variable), dots, letters, bool:negative = false;
if(variable[0] == '-') negative = true;
for(new i; i<length; i++)
{
if(negative == false) {
if((variable[i] == '.') && (i>0) && i<(length-1)) dots++;
else if((variable[i] > '9') || (variable[i] < '0')) letters++;
}
else {
if((variable[i] == '.') && (i>1) && i<(length-1)) dots++;
else if(((variable[i] > '9') || (variable[i] < '0')) && (i != 0)) letters++;
}
}
if(dots == 1 && letters == 0) return 1; // float
else if(letters > 0 ) return 2; //string
return 0; // integer
}
I've been playing around a bit, and came up with a function that checks if a string has a integer, float or string format, but you still have to convert the actual variable to a string first, but before you can do that you have to know if the variable is an integer, float or string, making this whole function useless for your needs.
I've searched ****** and this forum for a long time, but couldn't find a solution how to pass a variable, ignoring the fact if it's a integer, string or floating point number. I hope somebody else here knows a solution for this problem! I'm not sure if it's 100% foolproof though.. It will return 0 if the entered string is an integer i.e.: "43941", "-3139". It will return 1 if the entered string is a float: i.e.: "3109.310", "-1.31". It will return 2 if the entered string contains any other characters: ".3123", "ajd913", "-39Fdn2". pawn Код:
|
new Integer = 540;
new Float:FloatingPoint = 3.14;
new StringString[12] = "Hello";
new StringFloat[12] = "30.1391";
new Type0 = GetVarType(Integer); // this will result in a tag-mismatch error
new Type1 = GetVarType(FloatingPoint); // this will result in a tag-mismatch error
new Type2 = GetVarType(StringString); // This will work and return '2'.
new Type3 = GetVarType(StringFloat); // This will work too and returns '1'.