Skip to content

MT2D_VAR

ibm5155 edited this page Sep 4, 2018 · 1 revision

MT2D_VAR is a way of storing multiple (but fixed) data types under a single struct Its basic struct is defined below:

struct MT2D_VAR {
	void *Data;
	MT2D_VAR_TYPE Type;
	char *Name;
};

an MT2D_VAR has a pointer to a Data that is cloned when you create a new var, a Type marker and also the name of this Var.

The supported data types are the following:

  1. VAR_BOOL
  2. VAR_CHAR
  3. VAR_INT
  4. VAR_FLOAT
  5. VAR_UNSIGNED_CHAR
  6. VAR_UNSIGNED_INT
  7. VAR_UNSIGNED_FLOAT
  8. VAR_POINTER
  9. VAR_STRING

By those names, I'll describe the last two special cases and those are the pointers and string:

VAR_POINTER

This VAR type is used as an MT2D_VAR pointer of another MT2D_VAR, it's not a pointer referenced by memory but by it's name, so it can be used in cases where it's creation doesn't really know where the pointed data is, as an example, you have a pointer to the Health VAR from an object, since you can have lots of objects with the health bars, you can just create a single Health pointer on the memory and link it to all those objects, thus you create a single pointer in memory that will work with every object.

VAR_STRING

This VAR stores a string as it's data, it cannot suffer math operations on it either any other type of logic test.

How to retreive the value inside the MT2D_VAR?

for that you should use the function MT2D_Object_Get_Var_Data, and also use a special cast to tell the C/C++ code to know that in the given memory address there's a known data type, check the example below:

MT2D_VAR *myVar = MT2D_Object_Create_Var_Int("Test",6);
int X = *(int*)MT2D_Object_Get_Var_Data(myVar );
printf("%d",X); // output is 6

For that you should really know the type of the var that you're dealing, else you'll get in trouble :)

Functions

/*Gets an MT2D_VAR and change its data to a new data type*/
void MT2D_Object_Set_Var(MT2D_VAR *Var, bool Data);
void MT2D_Object_Set_Var(MT2D_VAR *Var, int Data);
void MT2D_Object_Set_Var(MT2D_VAR *Var, unsigned int Data);
void MT2D_Object_Set_Var(MT2D_VAR *Var, char *Data);
void MT2D_Object_Set_Var(MT2D_VAR *Var, float Data);
void MT2D_Object_Set_Var(MT2D_VAR *Var, float Data);
void MT2D_Object_Set_Var(MT2D_VAR *Var, void *Data, MT2D_VAR_TYPE Type);
void MT2D_Object_VAR_SetVar(MT2D_VAR *Var, MT2D_VAR *New);

/*Gets the pointer of the inside Data*/
void *MT2D_Object_Get_Var_Data(MT2D_VAR *Var);


MT2D_VAR *MT2D_Object_Get_Var_Pointer(MT2D_VAR *ArrayVARS, char *Name);

/*Gets the last error that happened dealing with MT2D_VARS*/
MT2D_VAR_ERROR MT2D_Object_Get_Last_Var_Error();

/*Create Vars*/
MT2D_VAR *MT2D_Object_Create_Var_Int(char *Name, int InitialData);
MT2D_VAR *MT2D_Object_Create_Var_UInt(char *Name, unsigned int InitialData);
MT2D_VAR * MT2D_Object_Create_Var_Char(char * Name, char InitialData);
MT2D_VAR * MT2D_Object_Create_Var_UChar(char * Name, unsigned char InitialData);
MT2D_VAR * MT2D_Object_Create_Var_Pointer(char * Name);

/*Tries to get the internal value of a Var as integer*/
int MT2D_Object_VAR_GetInt(MT2D_VAR *Var);
/*Create a new MT2D_Var based in the internal stored information from the given var*/
MT2D_VAR *MT2D_VAR_CLONE(MT2D_VAR *VAR);

/*tries to apply basic math operations*/
void  MT2D_Object_SUB(MT2D_VAR *Store, MT2D_VAR *ToAdd);
void  MT2D_Object_ADD(MT2D_VAR *Store, MT2D_VAR *ToAdd);

/*Creates a matrix of MT2D_VARS, userfull for CScript*/
MT2D_VAR **MT2D_VAR_Create_Matrix1(MT2D_VAR *First);
MT2D_VAR **MT2D_VAR_Create_Matrix2(MT2D_VAR *First,MT2D_VAR *Second);
MT2D_VAR **MT2D_VAR_Create_Matrix3(MT2D_VAR *First, MT2D_VAR *Second,MT2D_VAR *Third);
MT2D_VAR **MT2D_VAR_Create_Matrix3(MT2D_VAR *First, MT2D_VAR *Second, MT2D_VAR *Third);
MT2D_VAR **MT2D_VAR_Create_Matrix4(MT2D_VAR *First, MT2D_VAR *Second, MT2D_VAR *Third,MT2D_VAR *Fourth);

/*Releases the internal Data from memory*/
void MT2D_VAR_Free(MT2D_VAR *var, int constName);

EXAMPLES

  • Create Two vars and subtract one from another
MT2D_VAR *var1 = MT2D_Object_Create_Var_Int("One", 1);
MT2D_VAR *var2 = MT2D_Object_Create_Var_Char("Two", 2);
MT2D_Object_ADD(var1,var2);//var1 Data is edited
printf("%d",*(int*)MT2D_Object_Get_Var_Data(var1)); // output is 3
MT2D_VAR_Free(var1);
MT2D_VAR_Free(var2);
Clone this wiki locally