Raised This Month: $ Target: $400
 0% 

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


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

Ok, I think I finally get it!

So we have this 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);

    
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;

then we have this strange include file:
PHP Code:
#if defined _uselessnatives_included
 #endinput
#endif
#define _uselessnatives_included

native native_setAnInt(intvalue); 
and finally 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;

Sooooooooooooooo... What is happening here?
1) setAnInt(50) from second plugin is actually a function. Function from first plugin called native_setAnInt.
2) it pass the value 50 via uselessnatives.inc
3) uselessnatives.inc communicate with CreateNative("setAnInt", native_setAnInt) from first plugin
4) and by this magic, 50 is set in native_setAnInt function in first plugin. This function than changes value of anInt = 50.

That's setAnInt. Passing data from second plugin to the first plugin.
After it is set (passed) it is called by a function getAnInt. And there is no need for magic? It is just called like this? Directly?

I need to pass an array from first plugin to second. Only one-way. What do I need???

Last edited by Vergil333; 06-03-2016 at 13:52. Reason: I always find some typing errors after I post a post
Vergil333 is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 06-03-2016 , 14:11   Re: [Tutorial] Communicating data across plugins (or how to use natives in general)
Reply With Quote #2

Quote:
Originally Posted by Vergil333 View Post
Sooooooooooooooo... What is happening here?
1) setAnInt(50) from second plugin is actually a function. Function from first plugin called native_setAnInt.
2) it pass the value 50 via uselessnatives.inc
3) uselessnatives.inc communicate with CreateNative("setAnInt", native_setAnInt) from first plugin
4) and by this magic, 50 is set in native_setAnInt function in first plugin. This function than changes value of anInt = 50.

That's setAnInt. Passing data from second plugin to the first plugin.
After it is set (passed) it is called by a function getAnInt. And there is no need for magic? It is just called like this? Directly?

I need to pass an array from first plugin to second. Only one-way. What do I need???
Here is what actually happens:
A) CreateNative tells SourceMod to bind setAnInt native to plugin function native_setAnInt through FakeNatives.
B) 2nd Plugin calls setAnInt and pushes 50 to param stack.
C) Sourcepawn lookups the native binding of setAnInt, discovers it as a fake native and calls the fake native router associated with the native.
D) SourceMod sets up internal variables related to the fake native it's currently executing (in this case, setAnInt).
E) SourceMod calls the bound plugin function.
F) First plugin does logic for native and returns 0 (because SP compiler code injection).
G) SourceMod re-returns 0 back to SP.
H) SP re-returns 0 back to the 2nd plugin.
I) Second plugin discards the returned value after current function is done executing.

Last edited by WildCard65; 06-03-2016 at 14:12.
WildCard65 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 18:55.


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