AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   Workaround for cross calling methods from methodmaps (https://forums.alliedmods.net/showthread.php?t=300065)

fakuivan 08-04-2017 10:07

Workaround for cross calling methods from methodmaps
 
Calling a method of methodmap1 from methodmap2, while still being able to call a method of methodmap2 from methodmap1 is not something that you can do on sourcepawn. But if you are desperate to do so, I found a workaround (that does not break the compiler :fox:).

Code:

#include <sourcemod>
#pragma semicolon 1
#pragma newdecls required

public Plugin myinfo =  {
        name = "",
        author = "",
        description = "",
        version = "0.0.0",
        url = ""
};

//we'll define MM2 as a enum, since we won't be calling its functions directly,
//and it can be overwritten by the methodmap later
enum MM2 {  }

methodmap MM1 {
        public MM1()
        {
                //because the methodmap MM2 does not exist yet for sourcepawn, we need to call a function that calls the method
                //functions can be called first and defined later, methods can't
                InstatiateMM2();
                return view_as<MM1>(0);
        }
}

methodmap MM2 {
        public MM2()
        {
                //since MM1 was already defined, we can call it without problems
                MM1 mm1 = MM1();
                return view_as<MM2>(0);
        }
}

//this wraps the MM2 constructor allowing us to call it before it being defined
//this needs to be defined AFTER the method we are trying to call
MM2 InstatiateMM2()
{
        return MM2();
}

public void OnPluginStart()
{
        //infinite loop
        MM1 mm1 = MM1();
}

Note: You'll get a missing tag waring on places where the compiler expected a the methodmap to be returned but the enum was given instead (and vice versa).

Neuro Toxin 08-06-2017 17:48

Re: Workaround for cross calling methods from methodmaps
 
Can you access properties from MM2 via MM1?

fakuivan 08-07-2017 22:53

Re: Workaround for cross calling methods from methodmaps
 
Quote:

Originally Posted by Neuro Toxin (Post 2540008)
Can you access properties from MM2 via MM1?

No you can't, unless you make a wrapper for it, like how I did with the MM2 constructor on InstatiateMM2.

fakuivan 08-08-2017 09:53

Re: Workaround for cross calling methods from methodmaps
 
VoiDeD already filed the bug like 3 years ago. Hopefully someone will patch it.


All times are GMT -4. The time now is 18:54.

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