Difference between revisions of "ISXDK:Adding a Data Type"

From Lavish Software Wiki
Jump to: navigation, search
(Create our data type definition class)
 
Line 137: Line 137:
 
   
 
   
 
  };
 
  };
 +
 +
===Returning an iterator===
 +
case GetIterator:
 +
    if (argc)
 +
    {
 +
        LSOBJECT Object;
 +
        if (g_pScriptInterface->DataParse(argv[0],Object))
 +
        {
 +
            return InitializeIterator(pSet,(void*)LSN_ANY,Object);
 +
        }
 +
    }
 +
    return false;
  
 
==See Also==
 
==See Also==

Latest revision as of 13:05, 12 July 2012

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->AddLSTypeDefinition(class LSTypeDefinition &Type) to add each data type.

Example Code
void ISXTest::RegisterDataTypes()
{
	 pTestType = new IS_TestType;
	 pISInterface->AddLSTypeDefinition(*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->RemoveLSTypeDefinition(class LSTypeDefinition &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->RemoveLSTypeDefinition(*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;
srand(time(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(LSOBJECTDATA ObjectData, PCHAR Member, int argc, char *argv[], LSOBJECT &Dest)
{
#define pPerson ((Person*)ObjectData.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;
	}
#undef pPerson
	return false;
}


Create our data type definition class GetMethod functions

Example code
bool IS_TestType::GetMethod(LSOBJECTDATA &ObjectData, PCHAR Method, int argc, char *argv[])
{
	#define pPerson ((Person*)ObjectData.Ptr)
	if (!VarPtr.DWord)
		return false;
	PLSTYPEMETHOD pMethod=IS_TestType::FindMethod(Method);
	if (!pMethod)
		return false; 
	switch((TestMethods)pMethod->ID)
	{
	case Random:
        srand(time(0));
	 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 LSTypeDefinition. 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 LSTypeDefinition
{
public:
   static enum TestMembers
   {
	   Age=1,
	   Height=2,
	   Weight=3,
	   Sex=4,
   };

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

	};

   ~IS_TestType() {}
   	bool GetMember(LSOBJECTDATA ObjectData, PCHAR Member, int argc, char *argv[], LSOBJECT &Dest);
 	bool GetMethod(LSOBJECTDATA &ObjectData , PCHAR Method, int argc, char *argv[]);

};

Returning an iterator

case GetIterator:
   if (argc)
   {
       LSOBJECT Object;
       if (g_pScriptInterface->DataParse(argv[0],Object))
       {
           return InitializeIterator(pSet,(void*)LSN_ANY,Object);
       }
   }
   return false;

See Also

ISXDK Data_Types