ISXDK:Adding a Data Type

From Lavish Software Wiki
Revision as of 17:07, 7 September 2005 by Beefalo (talk | contribs)
(diff) ←Older revision | view current revision (diff) | Newer revision→ (diff)
Jump to navigation Jump to search

Summary

This tutorial is to explain the basics of adding a Data Type into a custom extension.

Adding a Data Type

When an extension is loaded Inner Space calls the Initialize function inside of it. In our test extension we have ISXTest::Initialize call the function RegisterDataTypes() to add the data types into LavishScript. Inside RegisterDataTypes() we must first create a pointer to the data type definition class then we use pISInterface->AddLSType(class LSType &Type) to add each data type.

Example Code
void ISXTest::RegisterDataTypes()
{
	 pTestType = new IS_TestType;
	 pISInterface->AddLSType(*pTestType);
}

Removing a Data Type

Removing a data type is basically just like adding a data type. When an extension gets unloaded Inner Space calls the function Shutdown that is a required part of each extension. In our test extension we call the function UnRegisterDataTypes() inside of Shutdown. The function to remove a Data Type is pISInterface->RemoveLSType(class LSType &Type). We also use this function to delete the pointer to the data type definition class we created in RegisterDataTypes().

Example code
void ISXTest::UnRegisterDataTypes()
{
       pISInterface->RemoveLSType(*pTestType);
       delete pTestType;
}

Creating a Data Type

Create our variables to be used for our data type

Example code from ISXTest.cpp
Person MainPerson;
IS_TestType *pTestType=0;
MainPerson.Age=(rand()%99)+1;
MainPerson.Height=(rand()%50)+100;
MainPerson.Weight=(rand()%120)+100;
MainPerson.Sex=(rand()%2);


Create our data type definition class GetMember functions

Example code from ISXTest.cpp
bool IS_TestType::GetMember(LSVARPTR VarPtr, PCHAR Member, int argc, char *argv[], LSTYPEVAR &Dest)

{

  1. define pPerson ((Person*)VarPtr.Ptr)

if (!VarPtr.Ptr) return false; PLSTYPEMEMBER pMember=FindMember(Member); if (!pMember) return false; switch((TestMembers)pMember->ID) { case Age: Dest.Int=pPerson->Age; Dest.Type=pIntType; // ${Person.Age} return true; case Height: Dest.Int=pPerson->Height; Dest.Type=pIntType; return true; case Weight: Dest.Int=pPerson->Weight; Dest.Type=pIntType; return true; case Sex: Dest.Int=pPerson->Sex; Dest.Type=pIntType; return true; }

  1. undef pPerson

return false; }

Example code
bool IS_TestType::GetMethod(LSVARPTR &VarPtr, PCHAR Method, int argc, char *argv[])
{
	#define pPerson ((Person*)VarPtr.Ptr)
	if (!VarPtr.DWord)
		return false;
	PLSTYPEMETHOD pMethod=IS_TestType::FindMethod(Method);
	if (!pMethod)
		return false; 
	switch((TestMethods)pMethod->ID)
	{
	case Random:
	 pPerson->Age=(rand()%99)+1;
	 pPerson->Height=(rand()%50)+100;
	 pPerson->Weight=(rand()%120)+100;
	 pPerson->Sex=(rand()%2);
		return true;
	}
	return false;
#undef pPerson
}
Example code from ISXTest.h
extern class IS_TestType *pTestType;

Creating a structure to hold our data

Here we create our structure in ISXTest.h that is going to be used to hold the information we need for our data type.

Example code from ISXTest.h
struct Person
{
	unsigned long Age;
	unsigned long Height;
	unsigned long Weight;
	unsigned long Sex;
};

Create our data type definition class

In this example code we create our data type definition class. We define the name of our class as IS_TestType and make it a public member of LSType. Inside we enum all the members our function is going to have, in this class it is Age, Height, Weight, Sex. Next we enum all the methods this class will have, in our class we have a method called Random.

Example code from ISXTest.h
class IS_TestType : public LSType
{
public:
   static enum TestMembers
   {
	   Age=1,
	   Height=2,
	   Weight=3,
	   Sex=4,
   };

   static enum TestMethods
   {
	   Random=1,
   };
   
	IS_TestType():LSType("test")
 	{
		TypeMember(Age);
		TypeMember(Height);
		TypeMember(Weight);
		TypeMember(Sex);

	};

   ~IS_TestType() {}
   	bool GetMember(LSVARPTR VarPtr, PCHAR Member, int argc, char *argv[], LSTYPEVAR &Dest);

	bool ToString(LSVARPTR VarPtr, PCHAR Destination) {return false;}
};


See Also

ISXDK Data_Types