Thread: [Solved] Natives and Methodmaps
View Single Post
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