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

[Pawn Question] How to set a function as a parameter for a function?


Post New Thread Reply   
 
Thread Tools Display Modes
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 09-16-2014 , 10:49   Re: [Pawn Question] How to set a function as a parameter for a function?
Reply With Quote #11

Quote:
Originally Posted by Powerlord View Post
Be aware that functag and funcenum don't exist in the new syntax... you need to look up what replaced them and use that instead.
Edit: typedef replaced functag and union replaced funcenum.

Speaking of which, I was going to work on passing a data arg to menus in the 1.7 core... I wonder if the AM team moved the menu code yet so I can start modifying them.
this? https://github.com/alliedmodders/sourcemod/pull/151
__________________
WildCard65 is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 09-16-2014 , 12:32   Re: [Pawn Question] How to set a function as a parameter for a function?
Reply With Quote #12

I mentioned this in the other post.

I've only had to do this once... basically what I did was use adt arrays, each cell having a handle to the forward.. function search name, an cipher (string), and then another arg as a datapack. It wasn't pretty but it got the job done. This was mainly because I wouldn't know what the thing would be doing until later, and if the other plugin would have the think functions loaded for that module (there were different think modules for different plugins). I guess you could use a string in stead or an any, and do away with the cipher, but you'll need to know what your forward is expecting, and it gets very messy very quickly.

Was like this

PHP Code:
new pack CreateDataPack();
WritePackCell(entity);
WritePackCell(target);
WritePackFloat(velocity);

Function_Think("Surface to air homing"pack);

Function_Think(const String:method[], Handle:data)
{
  new 
index FindStringInArray(gh_methodsmethod);
  if(
method != -1)
  {
    
decl String:cipher[64];
    
GetArrayString(gh_cipherindexcipher64);
    
Call_StartForward(GetArrayCell(gh_forwardindex))
    
    
ResetPack(pack);
    for(new 
icipher[i] != '\0'i++)
    {
       switch(
cipher[i])
       {
            case 
'i'Call_PushCell(ReadPackCell(pack));
            case 
'f'Call_PushFloat(ReadPackFloat(pack));
            case 
's':
            {
                
decl String:temp[256];
                
ReadPackString(packtemp256);
                
Call_PushString(temp);
            }
       }
    }
    
Call_Finish();
  }
  else
  {
     
// method wasn't found, do something...
  
}
  
CloseHandle(pack);


As you can see, it's not pretty. And there are some assumptions:
You always know what you are going to do. If you call the function with the wrong cipher, you're screwed. I got around this by using the preproccessor and putting them in an include file, so they would always be the same.. but you still have to be sure that you that you pass all of the parameters or the forward will not get the data. This is okay if you don't know what modules you will be loading/unloading, and you can have the same "function name" do different things in different situations. However, if you start having multiple methods with the same name, you'll have to do something.. Perhaps store the plugin's handle AND library name that registered the forward if it's a global forward, and when that plugin's library is unloaded, remove it's data from your arrays. like deal with which one was loaded first, or figure out some kind of calling convention. This is a hacky solution, and one that I would avoid unless you really really need it.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.

Last edited by friagram; 09-16-2014 at 12:36.
friagram is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 09-16-2014 , 12:51   Re: [Pawn Question] How to set a function as a parameter for a function?
Reply With Quote #13

Take a look at sourceirc.
__________________
Santa or Satan?

Watch out when you're paying people for private requests! Most stuff already exists and you can hardly assess the quality of what you'll get, and if it's worth the money.
Dr. Greg House is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 09-16-2014 , 13:03   Re: [Pawn Question] How to set a function as a parameter for a function?
Reply With Quote #14

Quote:
Originally Posted by WildCard65 View Post
Looks like it. I didn't think to check the pull requests to see if it was finished. Granted, I had just assumed it was when I merged master into my fork's master (which I subsequently merged into the branch I use for this).
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 09-16-2014 at 13:04.
Powerlord is offline
BAILOPAN
Join Date: Jan 2004
Old 09-16-2014 , 16:24   Re: [Pawn Question] How to set a function as a parameter for a function?
Reply With Quote #15

You don't need any directives to use the new syntax - just the 1.7 compiler. You can still use functag/funcenum though we have introduced a new syntax and deprecated the old one:

Code:
typedef AbilityFunction function void();

The old syntax isn't "hard deprecated"... we just don't use it upstream anymore. It'll probably get deprecated in 1.8 and removed in 1.9, or something.
__________________
egg
BAILOPAN is offline
nergal
Veteran Member
Join Date: Apr 2012
Old 04-11-2015 , 01:00   Re: [Pawn Question] How to set a function as a parameter for a function?
Reply With Quote #16

just an expansion on this thread

I tried to do this

PHP Code:
Function func pack.ReadFunction();

int param1 pack.ReadCell();
if ( 
param1 func(param1); 
and I get an error saying "invalid function call, not a valid address."

Is there seriously no other way to call a function without using call_startfunction?
__________________
nergal is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 04-11-2015 , 11:41   Re: [Pawn Question] How to set a function as a parameter for a function?
Reply With Quote #17

Quote:
Originally Posted by nergal View Post
just an expansion on this thread

I tried to do this

PHP Code:
Function func pack.ReadFunction();

int param1 pack.ReadCell();
if ( 
param1 func(param1); 
and I get an error saying "invalid function call, not a valid address."

Is there seriously no other way to call a function without using call_startfunction?
iirc Function variables aren't closures. You have to use Call_StartFunction or put them into a Forward to call them.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
nergal
Veteran Member
Join Date: Apr 2012
Old 04-14-2015 , 23:07   Re: [Pawn Question] How to set a function as a parameter for a function?
Reply With Quote #18

Quote:
Originally Posted by Powerlord View Post
iirc Function variables aren't closures. You have to use Call_StartFunction or put them into a Forward to call them.
would be interesting if in the future, there'll be a function class under Handles that can operate that way
__________________
nergal is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 04-15-2015 , 16:29   Re: [Pawn Question] How to set a function as a parameter for a function?
Reply With Quote #19

Quote:
Originally Posted by nergal View Post
would be interesting if in the future, there'll be a function class under Handles that can operate that way
Some of the breaking changes made in the transitional syntax are for supporting that in the future (and suprise, suprise, people complain about the changes).
__________________
asherkin is offline
nergal
Veteran Member
Join Date: Apr 2012
Old 04-15-2015 , 17:03   Re: [Pawn Question] How to set a function as a parameter for a function?
Reply With Quote #20

Quote:
Originally Posted by asherkin View Post
Some of the breaking changes made in the transitional syntax are for supporting that in the future (and suprise, suprise, people complain about the changes).
If I can drag and shoehorn Ethics into this, wouldn't anything that makes it easier and enables further flexibility with sourcepawn better and worth it more than the people who complain about the changes?

Currently, I love the changes. Methodmaps do make it easier to code and it's alot cleaner than before. I don't have to write 'new' every damn time I want to make a simple variable.

Also, I'd really love to know who's complaining because all of these changes are for the better and make Pawn alot more flexible and expressive than before.

My only request in sourcepawn is having structs/classes and pointers but I understand that it'll be some time before they're introduced but they're worth the wait.

I'm assuming that there won't be classes/pointers until the old syntax is removed in order to maintain sourcepawn's speed?

Seriously, I would like to have a long chat with whoever complained and I'd love to know their complaints.
__________________

Last edited by nergal; 04-15-2015 at 17:11.
nergal 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 08:29.


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