View Single Post
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 10-31-2017 , 14:48   Re: Dynamic Objects and Properties - v.0.0.30 - [2017.09.18]
Reply With Quote #249

Your best to use the class builder.

http://console.aus-tg.com/index.php?...tedynamicclass

Code:
#if defined _dynamic_class_test_
  #endinput
#endif
#define _dynamic_class_test_

methodmap test < Dynamic
{
	public test()
	{
		// First we make a new dymanic object
		Dynamic myclass = Dynamic(512, 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", "", 256);
		return view_as<test>(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);
	}
}
When dealing with strings, you need to realise they are a fixed length based on there size when first set. That length cant change. Notice the class builder sets the length within the methodmaps initialiser.
__________________
Neuro Toxin is offline