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

From Lavish Software Wiki
Jump to navigation Jump to search
 
Line 3: Line 3:
  
 
==Adding a Data Type==
 
==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 [[ISInterface:AddLSType|pISInterface->AddLSType(class LSType &Type)]] to add each 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 [[ISInterface:AddLSTypeDefinition|pISInterface->AddLSTypeDefinition(class LSTypeDefinition &Type)]] to add each data type.   
 
;Example Code
 
;Example Code
 
  void ISXTest::RegisterDataTypes()
 
  void ISXTest::RegisterDataTypes()
 
  {
 
  {
 
  pTestType = new IS_TestType;
 
  pTestType = new IS_TestType;
  pISInterface->AddLSType(*pTestType);
+
  pISInterface->AddLSTypeDefinition(*pTestType);
 
  }
 
  }
  
 
==Removing a Data Type==
 
==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 [[ISInterface:RemoveLSType|pISInterface->RemoveLSType(class LSType &Type)]]. We also use this function to delete the pointer to the data type definition class we created in '''RegisterDataTypes()'''.
+
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 [[ISInterface:RemoveLSTypeDefinition|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
 
;Example code
 
  void ISXTest::UnRegisterDataTypes()
 
  void ISXTest::UnRegisterDataTypes()
 
  {
 
  {
         pISInterface->RemoveLSType(*pTestType);
+
         pISInterface->RemoveLSTypeDefinition(*pTestType);
 
         delete pTestType;
 
         delete pTestType;
 
  }
 
  }
Line 26: Line 26:
 
  Person MainPerson;
 
  Person MainPerson;
 
  IS_TestType *pTestType=0;
 
  IS_TestType *pTestType=0;
 +
srand(time(0));
 
  MainPerson.Age=(rand()%99)+1;
 
  MainPerson.Age=(rand()%99)+1;
 
  MainPerson.Height=(rand()%50)+100;
 
  MainPerson.Height=(rand()%50)+100;
Line 34: Line 35:
 
===Create our data type definition class GetMember functions===
 
===Create our data type definition class GetMember functions===
 
;Example code from ISXTest.cpp
 
;Example code from ISXTest.cpp
  bool IS_TestType::GetMember(LSVARPTR VarPtr, PCHAR Member, int argc, char *argv[], LSTYPEVAR &Dest)
+
  bool IS_TestType::GetMember(LSOBJECTDATA ObjectData, PCHAR Member, int argc, char *argv[], LSOBJECT &Dest)
 
{
 
{
#define pPerson ((Person*)VarPtr.Ptr)
+
#define pPerson ((Person*)ObjectData.Ptr)
 
if (!VarPtr.Ptr)
 
if (!VarPtr.Ptr)
 
return false;
 
return false;
Line 64: Line 65:
 
return false;
 
return false;
 
}
 
}
 +
 +
 +
===Create our data type definition class GetMethod functions===
 
;Example code
 
;Example code
  bool IS_TestType::GetMethod(LSVARPTR &VarPtr, PCHAR Method, int argc, char *argv[])
+
  bool IS_TestType::GetMethod(LSOBJECTDATA &ObjectData, PCHAR Method, int argc, char *argv[])
 
  {
 
  {
  #define pPerson ((Person*)VarPtr.Ptr)
+
  #define pPerson ((Person*)ObjectData.Ptr)
 
  if (!VarPtr.DWord)
 
  if (!VarPtr.DWord)
 
  return false;
 
  return false;
Line 76: Line 80:
 
  {
 
  {
 
  case Random:
 
  case Random:
 +
        srand(time(0));
 
  pPerson->Age=(rand()%99)+1;
 
  pPerson->Age=(rand()%99)+1;
 
  pPerson->Height=(rand()%50)+100;
 
  pPerson->Height=(rand()%50)+100;
Line 100: Line 105:
  
 
===Create our data type definition class===
 
===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.  
+
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
 
;Example code from ISXTest.h
  class IS_TestType : public LSType
+
  class IS_TestType : public LSTypeDefinition
 
  {
 
  {
 
  public:
 
  public:
Line 118: Line 123:
 
     };
 
     };
 
      
 
      
  IS_TestType():LSType("test")
+
  IS_TestType():LSTypeDefinition("test")
 
   {
 
   {
 
  TypeMember(Age);
 
  TypeMember(Age);
Line 128: Line 133:
 
   
 
   
 
     ~IS_TestType() {}
 
     ~IS_TestType() {}
     bool GetMember(LSVARPTR VarPtr, PCHAR Member, int argc, char *argv[], LSTYPEVAR &Dest);
+
     bool GetMember(LSOBJECTDATA ObjectData, PCHAR Member, int argc, char *argv[], LSOBJECT &Dest);
+
  bool GetMethod(LSOBJECTDATA &ObjectData , PCHAR Method, int argc, char *argv[]);
bool ToString(LSVARPTR VarPtr, PCHAR Destination) {return false;}
+
 
 
  };
 
  };
  

Revision as of 23:27, 7 September 2005

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)

{

  1. 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; }

  1. 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[]);
};


See Also

ISXDK Data_Types