Raised This Month: $32 Target: $400
 8% 

Return a Function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 01-08-2020 , 09:36   Return a Function
Reply With Quote #1

Is there any way to properly return a Function inside native callback?
I use converting method and it works fine:
Code:
public int NTV_SomeNative(Handle plugin, int numParams) {
    Function fktPtr = GetNativeFunction( 1 );
    return view_as< int >( fktPtr );
}
but compiler warns:
Quote:
warning 237: coercing functions to and from primitives is unsupported and will be removed in the future
So, is there another way to return Function pointer inside native?
__________________

Last edited by MAGNAT2645; 01-08-2020 at 09:38.
MAGNAT2645 is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 01-08-2020 , 16:03   Re: Return a Function
Reply With Quote #2

https://sm.alliedmods.net/new-api/functions/NativeCall

try:

PHP Code:
public any NTV_SomeNative(Handle pluginint numParams) {
    return 
GetNativeFunction);

__________________

Last edited by Ilusion9; 01-08-2020 at 16:04.
Ilusion9 is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 01-08-2020 , 18:47   Re: Return a Function
Reply With Quote #3

I have tried to use any instead of int but compiler throws an error:
Code:
error 130: cannot coerce functions to values
__________________
MAGNAT2645 is offline
Papero
Veteran Member
Join Date: Aug 2016
Location: Italy
Old 01-10-2020 , 16:24   Re: Return a Function
Reply With Quote #4

Instead of returning a function you could save it in a variable and pass the reference:
PHP Code:
    Function func;
    
GetFunction(func);
    
Call_StartFunction(INVALID_HANDLEfunc);
    
Call_Finish();


void GetFunction(Function& myFunc)
{
    
myFunc Hello;
}

void Hello()
{
    
PrintToServer("Hello");

Will print "Hello".
Tough there is no such things as `SetNativeFunction` so idk if this could help you
__________________
My Plugins
SPCode


Steam: hexer504
Telegram: Hexah
Discord: Hexah#6903

If you like my work you can donate here!

Last edited by Papero; 01-10-2020 at 16:26.
Papero is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 01-10-2020 , 16:44   Re: Return a Function
Reply With Quote #5

I just want to directly return Function pointer like some natives (GetNativeFunction, GetFunctionByName) do.

Actually, GetNativeFunction returns regular cell_t so i wonder if i can do same but without warning 237.
__________________
MAGNAT2645 is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 01-10-2020 , 20:10   Re: Return a Function
Reply With Quote #6

Why though? What's the use-case for returning a function from a native?
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 01-11-2020 , 03:49   Re: Return a Function
Reply With Quote #7

Why not? I have my own Store/Shop plugin coded from scratch and there's a thing:

I have 3 plugins for Store system:
1) Item Table - parses KV config (via SMCParser) and caches any items into "table".
2) Inventory and Equipment - as the name says, creates inventory and equipment systems
3) The Store itself - loads Shop Table into KeyValues handle (stores every shop and can delete shops disabled by specific map, shops can be global (on every map but only in respawnroom) or local (only in specific map zone))

So, i have native (from Item Table plugin) that returns "ItemBehavior" Function pointer (script/code that launches on item use/activating) that was registered via other native.
These pointers used by Inventory plugin (with Call_StartFunction call).

Functions work fine but this warning annoys me.

Also, i know that i can use forwards (instead of storing Function pointers and using Call_StartFunction) but i don't know how to share these private forwards or how to execute them from other plugins.
__________________

Last edited by MAGNAT2645; 01-11-2020 at 03:54.
MAGNAT2645 is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 01-11-2020 , 04:36   Re: Return a Function
Reply With Quote #8

Quote:
Originally Posted by MAGNAT2645 View Post
Also, i know that i can use forwards (instead of storing Function pointers and using Call_StartFunction) but i don't know how to share these private forwards or how to execute them from other plugins.
Yeah, you already know the right way of doing this. It's just a matter of learning it.


The warning hints to a more fundamental limitation that will need to be sorted out in the future. The only data type in pawn that can be returned from a native is a cell_t, which is literally just a uint32. Function pointers will eventually be too large to fit inside of that and by using forwards you future-proof your code from any possible breakage.
headline is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 01-11-2020 , 07:16   Re: Return a Function
Reply With Quote #9

Ah, I didn't think about it, but now i know. So, will you change that? (because SourcePawn doesn't support unsigned data type and there may be conflicts or simple integer overflow, i think)

Also, i know private forwards can have multiple functions but how to use them in my case:

Code:
native int Store_RegItemBehavior(const char[] token, Handle plugin = null, ItemBehaviorCB onUse = INVALID_FUNCTION, ItemBehaviorCB onEquip = INVALID_FUNCTION, ItemBehaviorCB onUnequip = INVALID_FUNCTION, ItemBehaviorCB onTrade = INVALID_FUNCTION, ItemBehaviorCB onSell = INVALID_FUNCTION, ItemBehaviorCB onDrop = INVALID_FUNCTION);
So, you can pass INVALID_FUNCTION if you don't want it to execute.
How to store all these functions into forward if PrivateForward.AddFunction doesn't allow INVALID_FUNCTION.
Or i need to use 1 forward per function (multiple forwards)? (and instead of checking INVALID_FUNCTION, check null)
__________________

Last edited by MAGNAT2645; 01-11-2020 at 07:30.
MAGNAT2645 is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 01-11-2020 , 10:44   Re: Return a Function
Reply With Quote #10

You would have a private forward for each behavior (onUse, onEquip, etc.). Then add a native for each forward so you can call it from other plugins.
Quote:
How to store all these functions into forward if PrivateForward.AddFunction doesn't allow INVALID_FUNCTION.
Simply don't call PrivateForward.AddFunction if you're passing INVALID_FUNCTION.

See https://wiki.alliedmods.net/Function...ivate_Forwards for the rest.
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:37.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode