Game Development Community

Define an array in DefineEngineFunction SOLVED !!!!

by Robert Pinter · in Torque 3D Professional · 06/05/2012 (9:27 pm) · 3 replies

Hi All,

I'm working on my own C++ class in the torque engine and I have a following function. It is just for a test purpose to find out how I can achieve what I want.

in the header.

void example(ShapeBase* models);

In the program body

void example(ShapeBase* models)
{
Con::printf(models->getname());
}

DefineEngineFunction(example,void,(ShapeBase* Model),,"")
{

class_name::get()->example(Model);

}

The above code is working fine when I call it from the script, so it writes the given name of the object to the console

What I would like to do is to pass multiple Shape object to my C++ function in array.



in the torque script.
function LoadModels($numShape)
{

if ($numShape == 0) 
{
echo ("The given value should be greater than 0");
}
else
{
$shapeModels[$numShape] = 0;
	
	for (%objIndex = 0; %objIndex < $numShape; %objIndex++)
		{
			
			$shapeModels[%objIndex] = new StaticShape(Knight@%objIndex)  
				{  
				dataBlock = AnimatedKnight;
				position = ""@%objIndex + (%objIndex + 0.5) @ " 0.0 0.0";
				};    
				
			MissionCleanup.add($shapeModels[%objIndex]);
			
		}
		
		%client = LocalClientConnection;
		%client.camera.setOrbitObject((Knight@(%middleShape = mCeil($numShape/2))).getID() - 1,"45 45 0 ", 2, 15, 5);

So the Shapes are stored in the array and I would like to pass this array from torque script to my C++ function so I tried do the following.

in the header.

void example(ShapeBase* models[]);

In the program body

void example(ShapeBase* models[])
{
int msize = sizeof(models[]);

for (index =0; index < msize; index++)
{
Con::printf(models[index]->getname());
}
}


DefineEngineFunction(example,void,(ShapeBase* Model[]),,"")
{

class_name::get()->;example(Model);

}

Unfortunately during the compile I get this error which is related to the "DefineEngineFunction" I guess this is not a way to create array in this function.

c:own_utilsdevelopmenttorque 3d 1.2enginesourceconsoleengineapi.h(987): error C2664: 'ShapeBase *(ShapeBase **)' : cannot convert parameter 1 from 'ShapeBase *' to 'ShapeBase **'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>          c:own_utilsdevelopmenttorque 3d 1.2enginesourceconsoleengineapi.h(986) : while compiling class template member function 'ShapeBase _EngineFunctionTrampoline<T>::jmp(R (__cdecl *)(A),const _EngineTrampoline<T>::Args &)'
1>          with
1>          [
1>              T=ShapeBase *(ShapeBase *[]),
1>              R=ShapeBase *,
1>              A=ShapeBase *[]
1>          ]


Do you have any idea how I can set the DefineEngineFunction argument as an array?


Many thanks

Rob

#1
06/08/2012 (5:40 am)
I'm unable to reproduce this, mainly because of this line:

class_name::get()->;example(Model);

being invalid C++ syntax. I'm also not sure what class_name is, my compiler is complaining that it doesn't exist.

Keep in mind that when you define an array variable, it's just as simply defining it as another pointer, so

ShapeBase *model[];

I translate as this:

ShapeBase **model;
#2
06/08/2012 (7:10 am)
Maybe try passing an instance of the ArrayObject class. TorqueScript doesn't do array primitives. And like Robert says, there seems to be a dodgy line in there ;P.
#3
06/09/2012 (3:21 am)
Thanks for your comment and suggestions.

Yeah, sorry, I wrote the code from my head and I didn't remember to all of the codes, so I correct it now.


In the ModelControl.h, just the class name and the header and the void.

#ifndef _MODELCONTROL_H_
#define _MODELCONTROL_H_

#ifndef _SHAPEBASE_H_
#include "T3D/shapeBase.h"
#endif _SHAPEBASE_H_

class ModelControl: public SimObject
{
	typedef SimObject Parent;
	
public: 
	ModelControl();
	~ModelControl();

	static ModelControl* get();
	static void init();

        void Model_Handle(ShapeBase *Models)
#endif
}


In the ModelControl.cpp

void ModelControl::Model_Handle(ShapeBase *Models)
{
  Con::printf(Models->getname());
}

DefinreEngineFunction(Model_Handle,void,(ShapeBase *Models)
{

ModelControl::get()->Model_Handle(Models);

}


So the above's were the originals, but I have found the solution to pass the array from torque script to my C++ function.

So here is the solution.

What I did I have added the following line to the ModelControl.h header.

#ifndef _SIMSET_H_
#include "console/simSet.h"
#endif _SIMSET_H_

and I have changed the void 

from 

void Model_Handle(ShapeBase *Models)

to

void Model_Handle(SimSet *ModelArray)
[code]

In the ModelControl.cpp

[code]
//funtion to handle the array members and do your stuff
void ModelControl::Model_Handle(SimSet *ModelArray)
{

	SimSet::iterator i;
    
         for(i = ModelArray->begin(); i != ModelArray->end(); i++)
         {
             ShapeBase *ShapeObj = (ShapeBase *)(*i);

	     Con::printf(ShapeObj->getName());
            
            //do your stuff whatever you want.

	 }			

}

//Populate the C++ function to the Torque script.

DefineEngineFunction(Model_Handle,void,(SimSet *ModelArray),,"")
{
	 		
	ModelControl::get()->Model_Handle(ModelArray);
		
}

In the Torque script I did the following

//Load models and put them into the SimSet.

function LoadModels($numShape)
{

if ($numShape == 0) 
{
echo ("You have to specify at least 1 number of model");
}
else
{
	$set = new SimSet(Models); //Create new SimSet
	
	for (%objIndex = 0; %objIndex < $numShape; %objIndex++)
		{
			
			%object = new StaticShape(Knight@%objIndex)  
				{  
				dataBlock = AnimatedKnight;
				position = ""@%objIndex + (%objIndex + 0.5) @ " 0.0 0.0";
				};    
				
			MissionCleanup.add(%object);

			$set.add(%object); //Add the object to the SimSet
		}
		
		%client = LocalClientConnection;
		%client.camera.setOrbitObject((Knight@(%middleShape = mCeil($numShape/2))).getID() - 1,"45 45 0 ", 2, 15, 5);	
		
		
}
   
  
}

// Pass the Simset to the populated C++ function.

function HandleModels()
{
Model_Handle($set);
}


Finally I figured out what is the best way to use the array between C++ and Torque script.

If you have any additional suggestion don't hesitate to let me know.

Thanks again for your help. :)