Quote:
Originally Posted by oeN.
Hey,
Basically I'm working on Clan System. And Dynamic looks promising.
I need to store Clans names and array of members (strings).
Let's say there are 5 Clans. Indexed 1, 2, 3, 4. Each Clan has unique name and array of members that are in that clan. Is it possible to store these data using Dynamic?
In AmxX I would just do that:
Code:
enum _:ClanData {
CLAN_ID,
CLAN_NAME[32],
CLAN_MEMBERS[MAX_MEMBERS][64]
}
new g_aClans = CreateArray(ClanData);
new clan[ClanData];
clan[CLAN_ID] = 1;
clan[CLAN_NAME] = "My Clan";
clan[CLAN_MEMBERS] = { "Player 1", "Player 2", "Player 3" }
ArrayPushArray(g_aClans, clan);
And done. So easy and so readable. Without dozens of global variables.
Am I doing bad trying to use Dynamic for that?
|
I would start of by using the
Class Builder to build your base class ClanData.
clandata.inc
Code:
#if defined _dynamic_class_clandata_
#endinput
#endif
#define _dynamic_class_clandata_
methodmap ClanData < Dynamic
{
public ClanData()
{
// First we make a new dymanic object
Dynamic myclass = Dynamic(64, 0);
// Next we will define all the members
// -> We do this to force the offsets to always be in the same location
// over multiple instances of the same class.
myclass.SetString("Name", "", 32);
myclass.SetDynamic("Members", Dynamic());
return view_as<ClanData>(myclass);
}
// Note that I use static offsets to access members.
// -> This improves performance by caching member offsets
// -> This is why we force the members in during the contructor
// -> Failure to force members in the constructor will cause corruption
public bool GetName(char[] buffer, int length)
{
static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
if (offset == INVALID_DYNAMIC_OFFSET)
{
offset = this.GetMemberOffset("Name");
if (offset == INVALID_DYNAMIC_OFFSET)
SetFailState("A serious error occured in Dynamic!");
}
this.GetStringByOffset(offset, buffer, length);
return true;
}
public void SetName(const char[] buffer)
{
static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
if (offset == INVALID_DYNAMIC_OFFSET)
{
offset = this.GetMemberOffset("Name");
if (offset == INVALID_DYNAMIC_OFFSET)
{
offset = this.SetString("Name", buffer);
return;
}
}
this.SetStringByOffset(offset, buffer);
}
property Dynamic Members
{
public get()
{
static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
if (offset == INVALID_DYNAMIC_OFFSET)
{
offset = this.GetMemberOffset("Members");
if (offset == INVALID_DYNAMIC_OFFSET)
SetFailState("A serious error occured in Dynamic!");
}
return this.GetObjectByOffset(offset);
}
public set(Dynamic value)
{
static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
if (offset == INVALID_DYNAMIC_OFFSET)
{
offset = this.GetMemberOffset("Members");
if (offset == INVALID_DYNAMIC_OFFSET)
{
offset = this.SetObject("Members", value);
return;
}
}
this.SetObjectByOffset(offset, value);
}
}
}
As per your code example:
Code:
Dynamic Clans = Dynamic();
ClanData Clan = ClanData();
Clan.SetName("My Clan");
Dynamic ClanMembers = ClanData.Members;
ClanMembers.PushString("Player 1");
ClanMembers.PushString("Player 2");
ClanMembers.PushString("Player 3");
Clans.PushDynamic(Clan);
You could then access a ClanData methodmap like so:
Code:
ClanData Clan = Clans.GetDynamicByIndex(0);
__________________