SA-MP Forums Archive
Combining int function with float function using tagof... - 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: Combining int function with float function using tagof... (/showthread.php?tid=602091)



Combining int function with float function using tagof... - Crayder - 02.03.2016

Suppose I have these:
pawn Код:
stock range(...)
{
    new mini = getarg(0), maxi = mini;
    for(new i = numargs() - 1; i != 1; i--) {
        if(getarg(i) > maxi)
            maxi = getarg(i);
        if(getarg(i) < mini)
            mini = getarg(i);
    }
   
    return maxi - mini;
}

stock Float:frange(Float:...)
{
    new Float:mini = Float:getarg(0), Float:maxi = mini;
    for(new i = numargs() - 1; i != 1; i--) {
        if(Float:getarg(i) > maxi)
            maxi = Float:getarg(i);
        if(Float:getarg(i) < mini)
            mini = Float:getarg(i);
    }
   
    return maxi - mini;
}
I want to make them a single function that accepts both integer arguments and float arguments. I started with this, but it crashes the compiler:
pawn Код:
stock Float:xrange({Float, _}:...)
{
    new Float:mini, Float:maxi, tag = tagof(getarg(0));
    switch(tag) {
        case ((tagof(Float:))): {
            mini = Float:getarg(0);
            maxi = mini;
        }
        default: {
            mini = getarg(0);
            maxi = mini;
        }
    }
    // That's how far I got...
}
I already know about the compiler bug that causes "case (tagof(Float)" to crash, which is why I used "case ((tagof(Float))" (as ****** suggested somewhere a long time ago).

1. What am I doing wrong?
2. How do I fix it?

Never really used tagof before in something like this.


Re: Combining int function with float function using tagof... - BiosMarcel - 02.03.2016

Can u overload functions? I never tried, but if it works just do that. And shouldnt the float one accept int anyways?

Float:float = 1 (float is now 1.0) thought it would work like that, maybe i just used a bit too much java recently :P


Re: Combining int function with float function using tagof... - NaS - 02.03.2016

First of all you don't need to have unlimited arguments, just one like you said, so
Код:
stock Float:xrange({Float, _}:...)
becomes
Код:
stock Float:xrange({Float,_}:value, tagid = tagof(value))
since you only want Float and int to be accepted by one argument.

Look at this:
https://sampforum.blast.hk/showthread.php?pid=3459023#pid3459023


Re: Combining int function with float function using tagof... - Crayder - 02.03.2016

@Marcel: No you can't really overload functions in PAWN. What I'm doing is as close as you can get to overloading.

@NaS: No, the entire point of the function is finding the range of the unlimited arguments.


Re: Combining int function with float function using tagof... - Crayder - 03.03.2016

Nobody ever wants to help me... D:


Re: Combining int function with float function using tagof... - NaS - 03.03.2016

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Nobody ever wants to help me... D:
I wanted to, but I was in a hurry and didn't notice that you required unlimited arguments

I only know you have to do it like in this include (using #emit):

https://sampforum.blast.hk/showthread.php?tid=313488

Or like a SendClientMessageFormat function found several times in the Useful Functions thread.

Just instead calling format you have to do your calculations.
But I guess you already looked at that.


Re: Combining int function with float function using tagof... - Crayder - 03.03.2016

Quote:
Originally Posted by NaS
Посмотреть сообщение
I wanted to, but I was in a hurry and didn't notice that you required unlimited arguments

I only know you have to do it like in this include (using #emit):

https://sampforum.blast.hk/showthread.php?tid=313488

Or like a SendClientMessageFormat function found several times in the Useful Functions thread.

Just instead calling format you have to do your calculations.
But I guess you already looked at that.
I'm guessing you mean to use a format string, like "ddddfdfdfffdf", but that can't be the best solution. That could be a bit out of hand with enough values. It could work, even without using emit that would work. I just don't think that would be very pretty.


Re: Combining int function with float function using tagof... - NaS - 03.03.2016

Quote:
Originally Posted by Crayder
Посмотреть сообщение
I'm guessing you mean to use a format string, like "ddddfdfdfffdf", but that can't be the best solution. That could be a bit out of hand with enough values. It could work, even without using emit that would work. I just don't think that would be very pretty.
Thats not the part that I mean, with that technique (inside formatex) you can get all arguments you passed and process them (either float or int), just I can't tell you how to rewrite the code to not call format, but to do the stuff you want to do (calculate the range throughout all arguments).
That's all I can help you with.

I only know tagof does only work for known argument count since getarg will always return an integer (also it crashes..), the only way to know the tag is by specifying it in the function header (impossible for unknown argument count) like seen in the function overloading tutorial.

So you have 3 possible ways:
- format method like you said above
- pure float function with float() for every integer input
- #emit for which I cannot tell you how it works


Re: Combining int function with float function using tagof... - Crayder - 03.03.2016

Quote:
Originally Posted by NaS
Посмотреть сообщение
Thats not the part that I mean, with that technique (inside formatex) you can get all arguments you passed and process them (either float or int), just I can't tell you how to rewrite the code to not call format, but to do the stuff you want to do (calculate the range throughout all arguments).
That's all I can help you with.

I only know tagof does only work for known argument count since getarg will always return an integer (also it crashes..), the only way to know the tag is by specifying it in the function header (impossible for unknown argument count) like seen in the function overloading tutorial.

So you have 3 possible ways:
- format method like you said above
- pure float function with float() for every integer input
- #emit for which I cannot tell you how it works
The assembly code there is mostly just to pass the parameters to format. I don't think that is at all related.

tagof is the only thing I know of that can do this.


Re: Combining int function with float function using tagof... - Slice - 03.03.2016

It can't be done in such a simple manner. Tags during runtime are only saved in the debug information.

Look at how I did it in SQLite (sort of here).

Other things you could do if you really wish to cut down on manual meta-data:
* Write a recursive macro that saves the type. Might work OK, but probably not.
* Write a recursive macro that wraps all arguments in a wrapper that returns an array with 2 cells - tag info and the value.
* Create a custom type that works in a similar manner to my previous suggestion. You could make custom conversion functions for T->_ and T->Float.