AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved Natives and Methodmaps (https://forums.alliedmods.net/showthread.php?t=308047)

iGANGNAM 06-04-2018 16:28

Natives and Methodmaps
 
I know there is plenty tuts and stuff, but want to ask straight question.

How I can make methodmap with natives? So I could use methodmap in other plugin...


Let's say I'm interested in some client related natives, which would change some for that methodmap and executing some functions :)

If anyone has clear example of core plugin and plugin who uses those methodmaps natives please share

ddhoward 06-04-2018 16:32

Re: Natives and Methodmaps
 
Create the natives as normal, and then, in the include that the other plugin would use, insert the methodmap.

nosoop 06-04-2018 23:08

Re: Natives and Methodmaps
 
Let's say you had a methodmap like this:

Code:

methodmap YourThing {
        public native YourThing(int param); // "constructor" call, returns a value tagged as type YourThing
        public native void DoStuff(int a, int b); // "method" call on YourThing
}

You would declare natives like this:
Code:

public APLRes AskPluginLoad2(Handle hPlugin, bool late, char[] error, int err_max) {
        RegPluginLibrary("your-thing-lib");
       
        // use MethodMapName.FunctionName format
        CreateNative("YourThing.YourThing", Native_CreateYourThing);
        CreateNative("YourThing.DoStuff", Native_YourThingDoStuff);
       
        return APLRes_Success;
}

public int Native_CreateYourThing(Handle hPlugin, int argc) {
        int someValue;
        int param = GetNativeCell(1);
        // do stuff with param
       
        return someValue; // value that will be used as your methodmap's "this"
}

public int Native_YourThingDoStuff(Handle hPlugin, int argc) {
        int yourThingThis = GetNativeCell(1); // methodmap calls have an implicit "this"
        int a = GetNativeCell(2);
        int b = GetNativeCell(3);

        // do more stuff
}

And write code like this:
Code:

// include is implied
YourThing thing = YourThing(5);
thing.DoStuff(1, 8);

Though in my personal experience, I'd say that if you're working with existing entities, just write normal native functions. Wrapping entity indices with your methodmap takes additional steps and doesn't really add much other than "it looks object-oriented".

iGANGNAM 06-05-2018 03:11

Re: Natives and Methodmaps
 
Quote:

Originally Posted by nosoop (Post 2595342)
Let's say you had a methodmap like this:

Though in my personal experience, I'd say that if you're working with existing entities, just write normal native functions. Wrapping entity indices with your methodmap takes additional steps and doesn't really add much other than "it looks object-oriented".

Well with methodmaps code looks alot cleaner thats what I like about it. Thanks for example it's clear to me, really thanks alot! :)

also a question is it possible to extend my methodmap by parent/children stuff between plugins?

hmmmmm 06-05-2018 04:35

Re: Natives and Methodmaps
 
If you mean inheritance type stuff, you can sort of "inherit" from other methodmaps. See https://wiki.alliedmods.net/SourcePa...ax#Inheritance for how that works.
With that said, you shouldn't mistake methodmaps for objects and try to write OOP code. I'd only use methodmaps when extending/implementing Handle types.

iGANGNAM 06-07-2018 07:26

Re: Natives and Methodmaps
 
I trying to make Native methodmap for client and I get these warnings:
Code:

....
include file...

methodmap ZMPlayer {
  // Constructor
  public native ZMPlayer(int client);
  // Methods
  // XP related
  property int Client {
        public native get();
  }
  property int Level {
        public native get();
  }
  property int XP {
        public native get();
        public native set(const int val);
  }
... somewhere in main code ....
//        CreateNative("ZMPlayer.ZMPlayer", Native_ZMPlayer_Constructor);
public int Native_ZMPlayer_Constructor(Handle plugin, int numParams)
{
        int client = view_as<int>(GetNativeCell(2));
        if ( IsValidClient( client ) ) {
                return view_as< ZMPlayer >( GetClientUserId( client ) );
        }
        return view_as< ZMPlayer >(-1);
}

public int Native_ZMPlayer_ClientGet(Handle plugin, int numParams)
{
        ZMPlayer player = GetNativeCell(1);
        return GetClientOfUserId( int(player) );
}

public int Native_ZMPlayer_LevelGet(Handle plugin, int numParams)
{
        ZMPlayer player = GetNativeCell(1);
        return getPlayerLevel(player.Client);
}

public int Native_ZMPlayer_XPGet(Handle plugin, int numParams)
{
        ZMPlayer player = GetNativeCell(1);
        return getPlayerUnlocks(player.Client);
}

public int Native_ZMPlayer_XPSet(Handle plugin, int numParams)
{
        ZMPlayer player = GetNativeCell(1);
        setPlayerUnlocks( player.Client, GetNativeCell(2));
}

And I get these 2
Code:

// C:\zombies\addons\sourcemod\scripting\zombie_mod.sp(1454) : warning 213: tag mismatch
// C:\zombies\addons\sourcemod\scripting\zombie_mod.sp(1456) : warning 213: tag mismatch

Which are returning values for constructor....
Code:

return view_as< ZMPlayer >( GetClientUserId( client ) );
and
Code:

return view_as< ZMPlayer >(-1);
Why I get this warning?

nosoop 06-07-2018 08:45

Re: Natives and Methodmaps
 
You need to return the value as an integer in your native function callback.

iGANGNAM 06-07-2018 08:50

Re: Natives and Methodmaps
 
Quote:

Originally Posted by nosoop (Post 2595678)
You need to return the value as an integer in your native function callback.

but it's constructor native... what I should return? A value (client id) or what?

hmmmmm 06-07-2018 09:15

Re: Natives and Methodmaps
 
You return exactly what you would usually return, except as an integer. If necessary, do a view_as<int> on it to get rid of the tag mismatch warning. You have to return an int for all native implementations. If the actual native has a different return type that will be handled by SM and it'll "cast" it from an int to the correct type.

For example if you have a native like this:
PHP Code:

native float GetSomeFloat(); 

the implementation would look like this:
PHP Code:

public int Native_GetSomeFloatHandle pluginint numParams )
{
    return 
view_as<int>( 5.0 ); // do the view_as to get rid of the tag mismatch warning



iGANGNAM 06-07-2018 10:56

Re: Natives and Methodmaps
 
well this as constructor works fine thanks
Code:

public int Native_ZMPlayer_Constructor(Handle plugin, int numParams)
{
        int client = view_as<int>(GetNativeCell(1));
        if ( IsValidClient( client ) ) {
                return view_as< int >( GetClientUserId( client ) );
        }
        return view_as< int >(-1);
}



All times are GMT -4. The time now is 03:45.

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