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

Solved Natives and Methodmaps


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
iGANGNAM
AlliedModders Donor
Join Date: Sep 2012
Location: Lithuania
Old 06-04-2018 , 16:28   Natives and Methodmaps
Reply With Quote #1

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
__________________

Last edited by iGANGNAM; 06-07-2018 at 12:06.
iGANGNAM is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 06-04-2018 , 16:32   Re: Natives and Methodmaps
Reply With Quote #2

Create the natives as normal, and then, in the include that the other plugin would use, insert the methodmap.
__________________
ddhoward is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 06-04-2018 , 23:08   Re: Natives and Methodmaps
Reply With Quote #3

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".
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)
nosoop is offline
iGANGNAM
AlliedModders Donor
Join Date: Sep 2012
Location: Lithuania
Old 06-05-2018 , 03:11   Re: Natives and Methodmaps
Reply With Quote #4

Quote:
Originally Posted by nosoop View Post
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?
__________________

Last edited by iGANGNAM; 06-05-2018 at 03:14.
iGANGNAM is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 06-05-2018 , 04:35   Re: Natives and Methodmaps
Reply With Quote #5

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.
hmmmmm is offline
iGANGNAM
AlliedModders Donor
Join Date: Sep 2012
Location: Lithuania
Old 06-07-2018 , 07:26   Re: Natives and Methodmaps
Reply With Quote #6

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?
__________________
iGANGNAM is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 06-07-2018 , 08:45   Re: Natives and Methodmaps
Reply With Quote #7

You need to return the value as an integer in your native function callback.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)
nosoop is offline
iGANGNAM
AlliedModders Donor
Join Date: Sep 2012
Location: Lithuania
Old 06-07-2018 , 08:50   Re: Natives and Methodmaps
Reply With Quote #8

Quote:
Originally Posted by nosoop View Post
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?
__________________
iGANGNAM is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 06-07-2018 , 09:15   Re: Natives and Methodmaps
Reply With Quote #9

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

hmmmmm is offline
iGANGNAM
AlliedModders Donor
Join Date: Sep 2012
Location: Lithuania
Old 06-07-2018 , 10:56   Re: Natives and Methodmaps
Reply With Quote #10

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);
}
__________________
iGANGNAM 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 07:52.


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