[2.0.0] A Basic Methodmap - ntoxin66/Dynamic GitHub Wiki
Dynamic has a large focus on bringing classes to SourcePawn. As such, inheriting Dynamic from a Methodmap gives you a way to define class structures which manage their own memory during run-time.
Lets start off by making an empty Methodmap named MyPluginPlayerInfo
which inherits Dynamic
.
methodmap MyPluginPlayerInfo < Dynamic
{
}
Because MyPluginPlayerInfo
inherits Dynamic
, it's initialiser needs to create an actual Dynamic
instance and return this as the pointer. This means the inherited properties and functions from Dynamic
are accessible via MyPluginPlayerInfo
instances.
public MyPluginPlayerInfo()
{
Dynamic playerinfo = Dynamic();
return view_as<MyPluginPlayerInfo>(playerinfo);
}
When creating properties we use the inherited Dynamic instance to access their values for the getters and setters.
property int Points
{
public get()
{
return this.GetInt("Points");
}
public set(int value)
{
this.SetInt("Points", value);
}
}
property float KDR
{
public get()
{
return this.GetFloat("KDR");
}
public set(float value)
{
this.SetFloat("KDR", value);
}
}
property int Rank
{
public get()
{
return this.GetInt("Rank");
}
public set(int value)
{
this.SetInt("Rank", value);
}
}
We now have a fully functional dynamic structure that can be used like so.
public void OnClientConnected(int client)
{
MyPluginPlayerInfo playerinfo = MyPluginPlayerInfo();
playerinfo.Points = 2217;
playerinfo.KDR = 2.69;
playerinfo.Rank = 1;
}