Getting argument type? - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Getting argument type? (
/showthread.php?tid=598970)
Getting argument type? -
Gammix - 19.01.2016
Is there any way you can possibly identify an argument's type. Currently i am only working on single index arguments, no arrays.
So if i have a function like this:
How can i do such thing:
pawn Код:
MyFunc({_, Float}:...)
{
if (its only a number)
{
}
else if (its only a float)
{
}
else //its a string
{
}
}
Re: Getting argument type? -
Jefff - 19.01.2016
https://sampforum.blast.hk/showthread.php?tid=574203
https://sampwiki.blast.hk/wiki/Keywords:Operators#tagof
Re: Getting argument type? -
SickAttack - 19.01.2016
pawn Код:
// ** INCLUDES
#include <a_samp>
// ** MAIN
main()
{
print("Loaded \"argument_type.amx\".");
Function(""); // "2" = integer, "2.0" = float, "test" = string
}
// ** CALLBACKS
public OnGameModeInit()
{
return 1;
}
public OnGameModeExit()
{
return 1;
}
// ** FUNCTIONS
stock Function({_, Float}:...)
{
new string[144], bool:character = false, bool:point = false, bool:number = false;
for(new i = 0, j = sizeof(string); i < j; i ++)
{
string[i] = getarg(0, i);
}
for(new i = 0, j = strlen(string); i < j; i ++)
{
if(!character)
{
switch(string[i])
{
case 'A'..'z':
{
character = true;
}
}
}
if(!point)
{
switch(string[i])
{
case '.':
{
point = true;
}
}
}
if(!number)
{
switch(string[i])
{
case '0'..'9':
{
number = true;
}
}
}
}
if(character)
{
print("string");
}
else if(number && point)
{
print("float");
}
else
{
print("integer");
}
return 1;
}
Remember to put the arguments in double quotes.
Re: Getting argument type? -
Gammix - 19.01.2016
Quote:
Originally Posted by Jefff
|
tagof only works for elements with TAGS, integer and strings doesn't have one.
So how can we detect those 2?
Quote:
Originally Posted by SickAttack
Remember to put the arguments in double quotes.
|
That's what i don't want. I need to input any type and then detect internally. (idk if its possible in PAWN)
Re: Getting argument type? -
Vince - 19.01.2016
Try:
PHP код:
if(tagof(var) == tagof(_:))
Re: Getting argument type? -
Gammix - 19.01.2016
Quote:
Originally Posted by Vince
Try:
PHP код:
if(tagof(var) == tagof(_:))
|
Doesn't work for numbers without (_:) tag.
Re: Getting argument type? -
BroZeus - 19.01.2016
There is really no way to detect it you need to pass the format of argument like in functions like mysql_tquery, where you pass the format of variable to be passed to callback as a string.
tagof doesn't works with getarg directly it gives error.
For example:
PHP код:
tagof(getarg(0)); // gives error won't compile
So best option is to make your function something like
PHP код:
//example call to function someFunction("dfd", 3, 45, 55.5, 8)
stock someFunction(args[], s = sizeof(args), ...)
{
for(new i = 0; i < s; i++)
{
switch(getarg(0, i))
{
case 'd':{ /* interger */ }
case 'f':{ /* float */ }
//so on
}
}
}