Zeex / sampgdk

Write SA-MP gamemodes in C/C++
http://zeex.github.io/sampgdk
Apache License 2.0
156 stars 83 forks source link

Suggestion: make SAMPGDK_CPP_WRAPPERS declare and define a sampgdk class. #178

Open grasmanek94 opened 7 years ago

grasmanek94 commented 7 years ago

This popped up into my head a long time ago and didn't come around to implementing this myself, so I will just leave it here as a suggestion for whomever would like to work on this.

The problem: Sometimes you want to override the behavior of sa-mp (or other SampGDK provided) functions. This can be cumbersome and result in functions with different prototypes, prefixes, suffixes, defines and sometimes a combination of the mentioned things. Most commonly this is with SendClientMessage* functions.

The suggestion: Encapsulate all SampGDK functions in a class and mark them all as virtual when SAMPGDK_CPP_WRAPPERS is defined. Users can then create their own class which inherits from the base SampGDK class and customize the behavior of all functions to their likeness, all in a standard manner. Optionally SampGDK should provide a global variable that can be used directly, and provide a function like sampgdk_class_init(BaseClass* ptr) which would set the global variable to an user instantiated class (probably a derived class).

Example:

class SampGDK
{
public:
    virtual int SendClientMessage(int playerid, int color, const char* message);
};

SampGDK* sampgdk_global; // maybe someone can come up with a more creative name

class MyGamemodeSDK : public SampGDK
{
    int SendClientMessage(int playerid, int color, const char* message) override;
};

int MyGamemodeSDK::SendClientMessage(int playerid, int color, const char* message)
{
    return SampGDK::SendClientMessage(playerid, color, TranslateMessage(message));
}

//somewhere in the code
sampgdk_class_init(new MyGamemodeSDK());

Plugins that extend PAWN functionality wouldn't need this, in either case, each plugin should have its own instance of the 'global function pointer', to guarantee consistency.

NegativeIQ commented 7 years ago

Yeah that would be awesome. And its not just client messages... Get/Set pos, instead of passing floats we could use custom Vector class...Many useful stuff is possible....