Raised This Month: $51 Target: $400
 12% 

[Tutorial] Communicating data across plugins (or how to use natives in general)


Post New Thread Reply   
 
Thread Tools Display Modes
Vergil333
Member
Join Date: Apr 2016
Old 06-04-2016 , 12:39   Re: [Tutorial] Communicating data across plugins (or how to use natives in general)
Reply With Quote #11

Wow, thanks for explaining that to me, WildCard65. I wouldn't find this anywhere else.

But what about getAnInt() function? It returns an int form first plugin without any natives, forwards or anything?
Vergil333 is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 06-04-2016 , 13:58   Re: [Tutorial] Communicating data across plugins (or how to use natives in general)
Reply With Quote #12

Your example code doesn't register getAnInt as a native, so the SP compiler expects a normal method, your example code does expose a native_getAnInt but it'll never be called.
WildCard65 is offline
Vergil333
Member
Join Date: Apr 2016
Old 06-05-2016 , 05:36   Re: [Tutorial] Communicating data across plugins (or how to use natives in general)
Reply With Quote #13

I just copied the code from EasSidezz... But you are right, I don't know how I couldn't see it before...

If I correct the example this way, will it work? :

first plugin:

PHP Code:
new anInt;

//We call this because OnPluginLoad() is apparently called too late. I  mean just look at the name of this, it honestly looks like it belongs in  here.
public APLRes:AskPluginLoad2(Handle:myselfbool:lateString:error[], err_max)
{
    
CreateNative("setAnInt"native_setAnInt);
    
CreateNative("getAnInt"native_getAnInt);

    
RegPluginLibrary("uselessnatives.inc");
    return 
APLRes_Success;
}


public 
native_getAnInt(Handle:pluginnumParams)
{
    return 
anInt;
}


public 
native_setAnInt(Handle:pluginnumParams)
{
    
//setAnInt(value);
    
new set GetNativeCell(2); //First param, the value we'll be setting int to.

    
anInt set;
    return;

include file:
PHP Code:
#if defined _uselessnatives_included
 #endinput
#endif
#define _uselessnatives_included

native native_setAnInt(intvalue);
native native_getAnInt(intvalue); 
second plugin:
PHP Code:
#include <uselessnatives>

public OnPluginStart()
{
    
RegConsoleCmd("sm_nativeprint"command_printNatives"Print what you set");
}

public 
Action:command_printNatives(clientargs)
{
    
setAnInt(50);

    
getAnInt();

    return 
Plugin_Handled;

Vergil333 is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 06-05-2016 , 06:45   Re: [Tutorial] Communicating data across plugins (or how to use natives in general)
Reply With Quote #14

You should name your natives so they they won't conflict with other plugins. For example:

PHP Code:
    CreateNative("uselessnatives_setAnInt"native_setAnInt);
    
CreateNative("uselessnatives_getAnInt"native_getAnInt); 
Then you need to use the same names in your include file. Also, setAnInt only uses one parameter, and getAnInt does not use any.

PHP Code:
native uselessnatives_setAnInt(value);
native uselessnatives_getAnInt(); 
Then of course use GetNativeCell(1) inside native_setAnInt.


Edit: Also, to actually print the int:

PHP Code:
public Action:command_printNatives(clientargs)
{
    
uselessnatives_setAnInt(50);

    
ReplyToCommand(client"Int: %i"uselessnatives_getAnInt());

    return 
Plugin_Handled;

__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.

Last edited by DJ Tsunami; 06-05-2016 at 06:48.
DJ Tsunami is offline
Phil25
AlliedModders Donor
Join Date: Feb 2015
Old 06-05-2016 , 09:34   Re: [Tutorial] Communicating data across plugins (or how to use natives in general)
Reply With Quote #15

uselessnatives = use-less-natives

You can pass a third parameter which will tell your plugin whether to return an already set value or set it to the new one.

This can be also achieved by having a certain value of the "value" parameter to do it. For example, if you can't have the int less than 0, you can pass -1 which would you could make it return the value instead of setting it, a non-negative value does the latter.

Don't forget that you could set default values of parameters in the .inc file.

Just my two cents...
Phil25 is offline
Vergil333
Member
Join Date: Apr 2016
Old 06-05-2016 , 14:53   Re: [Tutorial] Communicating data across plugins (or how to use natives in general)
Reply With Quote #16

Whohohooo, it works like a charm! Thanks DJ Tsunami for your correction. Now everything makes sense to me
Vergil333 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 06-06-2016 , 16:11   Re: [Tutorial] Communicating data across plugins (or how to use natives in general)
Reply With Quote #17

When naming natives, I've found it best to have the plugin name at the start.

So, if my plugin name is GreatestPluginEver, it'd be Greatest_DoGreatThing
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
sdz
Senior Member
Join Date: Feb 2012
Old 06-07-2016 , 08:37   Re: [Tutorial] Communicating data across plugins (or how to use natives in general)
Reply With Quote #18

Quote:
Originally Posted by Powerlord View Post
When naming natives, I've found it best to have the plugin name at the start.

So, if my plugin name is GreatestPluginEver, it'd be Greatest_DoGreatThing
That's how I go about it.
I figure that if people feel comfortable about implementing natives into their plugins, they have enough sense to go read documentation but of course I can see I'm proven wrong time and time again.
sdz is offline
zeroibis
Veteran Member
Join Date: Jun 2007
Old 06-07-2016 , 13:38   Re: [Tutorial] Communicating data across plugins (or how to use natives in general)
Reply With Quote #19

Also lets not forget the view_as<int>() function to help avoid a tag mismatch toss if your trying to return something other than int and your prototype is a float or bool for example.
__________________
zeroibis is offline
Reply



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 15:51.


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