Interfacing scripts with C++ Code
by City University (#0001) · in Torque Game Engine · 12/11/2005 (6:08 am) · 13 replies
I am writing a fuzzy logic module (which I hope to release as a module) to add to the C++ part of the game engine. At the moment Im just trying to figure out how to connect the C++ code with the scripts. By reading around, this is what ive come up with:
These two classes are in C:\Torque\SDK\engine\ai
fuzzyLogic.h
fuzzyLogic.cc
In my script Ive got:
%fuzzy = new fuzzyLogic();
error("TEST" SPC %fuzzy);
I get an error:
demo/server/scripts/aiNPC.cs (244): Unable to instantiate non-conobject class fuzzyLogic.
demo/server/scripts/aiNPC.cs (245): Unable to find object: '0' attempting to call function 'getName'
For some reason the script code cant instantiate the fuzzyLogic object. Thanks in advance, your help is MUCH appreciated.
These two classes are in C:\Torque\SDK\engine\ai
fuzzyLogic.h
#include "console/simBase.h"
//----------------------------------------------------------------------------
class fuzzyLogic : public SimObject
{
typedef SimObject Parent;
public:
DECLARE_CONOBJECT(fuzzyLogic);
fuzzyLogic();
~fuzzyLogic();
};
IMPLEMENT_CONOBJECT(fuzzyLogic);fuzzyLogic.cc
#include "ai/fuzzyLogic.h"
#include "console/console.h"
#include "console/consoleTypes.h"
#include "console/consoleInternal.h"
//----------------------------------------------------------------------------
fuzzyLogic::fuzzyLogic()
{
}
fuzzyLogic::~fuzzyLogic()
{
}
ConsoleStaticMethod(fuzzyLogic,fuzzify,bool,0,0,"usage")
{
bool a = true;
return a;
}In my script Ive got:
%fuzzy = new fuzzyLogic();
error("TEST" SPC %fuzzy);
I get an error:
demo/server/scripts/aiNPC.cs (244): Unable to instantiate non-conobject class fuzzyLogic.
demo/server/scripts/aiNPC.cs (245): Unable to find object: '0' attempting to call function 'getName'
For some reason the script code cant instantiate the fuzzyLogic object. Thanks in advance, your help is MUCH appreciated.
About the author
#2
engine.overview.txt.
in
C:\Torque\SDK\engine
Here is the relevant extract that I got my code from:
Objects in the scripting language are simply instances of C++ classes deriving from SimObject, declared in the game engine and processed with a special set of macros (declared in engine/console/consoleObject.h). The DECLARE_CONOBJECT(class_name) macro is placed inside the class definition and the IMPLEMENT_CONOBJECT (class_name) macro is placed in a linked source file. IMPLEMENT_CONOBJECT has several versions, depending on the type of class:
- IMPLEMENT_CONOBJECT() A simple console object - no special network attributes.
- IMPLEMENT_CO_NETOBJECT_V1() A ghostable network object class. Any object that will be ghosted from server to client needs to be declared as a NETOBJECT. See the section on the network layer for more details about NetObjects.
- IMPLEMENT_CO_DATABLOCK_V1() A datablock class. Datablocks have special properties for being transmitted over the network.
@note There are 3 more IMPLEMENT_CONOBJECTs that deal specifically with network events. They are not covered here.
Every class declared as a Console class MUST declare a Parent typedef in its private member section referencing its parent class. This allows the console to properly determine the console object class hierarchy for method dispatch in the console.
You can instantiate and manipulate this object in script like this:
12/11/2005 (8:26 am)
Tried it but I get a whole load of errors. I read:engine.overview.txt.
in
C:\Torque\SDK\engine
Here is the relevant extract that I got my code from:
Objects in the scripting language are simply instances of C++ classes deriving from SimObject, declared in the game engine and processed with a special set of macros (declared in engine/console/consoleObject.h). The DECLARE_CONOBJECT(class_name) macro is placed inside the class definition and the IMPLEMENT_CONOBJECT (class_name) macro is placed in a linked source file. IMPLEMENT_CONOBJECT has several versions, depending on the type of class:
- IMPLEMENT_CONOBJECT() A simple console object - no special network attributes.
- IMPLEMENT_CO_NETOBJECT_V1() A ghostable network object class. Any object that will be ghosted from server to client needs to be declared as a NETOBJECT. See the section on the network layer for more details about NetObjects.
- IMPLEMENT_CO_DATABLOCK_V1() A datablock class. Datablocks have special properties for being transmitted over the network.
@note There are 3 more IMPLEMENT_CONOBJECTs that deal specifically with network events. They are not covered here.
Every class declared as a Console class MUST declare a Parent typedef in its private member section referencing its parent class. This allows the console to properly determine the console object class hierarchy for method dispatch in the console.
// A very simple SimObject sample.
class SampleObject : public SimObject
{
typedef SimObject Parent;
public:
DECLARE_CONOBJECT(SampleObject);
S32 someVariable; // signed integer variable
};
IMPLEMENT_CONOBJECT(SampleObject);You can instantiate and manipulate this object in script like this:
function foo()
{
%obj = new SampleObject(MySampleObject);
echo(%obj.getName());
}
#3
which suggest putting
in your fuzzyLogic.cc.
Problem is their sample doesnt do that, it it shouldnt make s difference anyway. Apart from that, I dont know.. sorry.
12/11/2005 (10:16 am)
Ok, strange, then the only thing it says (which the sample goes against) is:Quote:"and the IMPLEMENT_CONOBJECT (class_name) macro is placed in a linked source file"
which suggest putting
IMPLEMENT_CONOBJECT(fuzzyLogic);
in your fuzzyLogic.cc.
Problem is their sample doesnt do that, it it shouldnt make s difference anyway. Apart from that, I dont know.. sorry.
#4
12/11/2005 (2:00 pm)
Done a clean rebuild? That usually fixes these sorts of situations.
#5
fuzzyLogic.h
fuzzyLogic.cc
This is how my gui object is structured.
Also, doesn't the minimum arguments need to be 2 even when there are no arguments?:
12/11/2005 (7:49 pm)
Try:fuzzyLogic.h
#include "console/simBase.h"
//----------------------------------------------------------------------------
class fuzzyLogic : public SimObject
{
typedef SimObject Parent;
public:
fuzzyLogic();
~fuzzyLogic();
DECLARE_CONOBJECT(fuzzyLogic);
};fuzzyLogic.cc
#include "ai/fuzzyLogic.h"
#include "console/console.h"
#include "console/consoleTypes.h"
#include "console/consoleInternal.h"
IMPLEMENT_CONOBJECT(fuzzyLogic);
//----------------------------------------------------------------------------
fuzzyLogic::fuzzyLogic()
{
}
fuzzyLogic::~fuzzyLogic()
{
}
ConsoleStaticMethod(fuzzyLogic,fuzzify,bool,0,0,"usage")
{
bool a = true;
return a;
}This is how my gui object is structured.
Also, doesn't the minimum arguments need to be 2 even when there are no arguments?:
ConsoleStaticMethod(fuzzyLogic,fuzzify,bool,2,2,"usage")
#6
Appreciate the help guys!
12/12/2005 (4:13 am)
Tried the changes and did a new clean rebuild. Same error though. Put in 2 for the number of arguments too. Anyone got any other ideas.Appreciate the help guys!
#7
1) that you've added both the header and cc file to your project
2) That you are building a debug build and running the debug exe or building/running the release?
I know they're obvious but occasionally they can be missed :P
12/12/2005 (7:31 am)
Have you checked the following1) that you've added both the header and cc file to your project
2) That you are building a debug build and running the debug exe or building/running the release?
I know they're obvious but occasionally they can be missed :P
#8
It works when I run torqueDemo_DEBUG.exe. Why is that, can someone explain how I can get it to run on your standard torqueDemo.exe.
also, when I try to change the return type to anything but 'bool' Visual Studio wont compile
to
I get these errors from Visual Studio .net:
c:\Torque\SDK\engine\ai\fuzzyLogic.cc(22): error C2059: syntax error : ')'
c:\Torque\SDK\engine\ai\fuzzyLogic.cc(22): error C2065: 'conmethod_return_int' : undeclared identifier
c:\Torque\SDK\engine\ai\fuzzyLogic.cc(22): error C2146: syntax error : missing ';' before identifier 'cfuzzyLogicfuzzify'
Once again Thanks guys, Im new to Torque and over the last couple of months I really have learned a lot. Hopefully I'll get this resource finished soon so everyone can get make better ai for their games and I'll get a 1st from university.
12/12/2005 (8:16 am)
Thanks Gary :) , but Ive got new questions now.It works when I run torqueDemo_DEBUG.exe. Why is that, can someone explain how I can get it to run on your standard torqueDemo.exe.
also, when I try to change the return type to anything but 'bool' Visual Studio wont compile
ConsoleStaticMethod(fuzzyLogic,fuzzify,bool,2,3,"fuzzify")
{
bool a = true;
return a;
}to
ConsoleStaticMethod(fuzzyLogic,fuzzify,int,2,3,"fuzzify")
{
int a = 926;
return a;
}I get these errors from Visual Studio .net:
c:\Torque\SDK\engine\ai\fuzzyLogic.cc(22): error C2059: syntax error : ')'
c:\Torque\SDK\engine\ai\fuzzyLogic.cc(22): error C2065: 'conmethod_return_int' : undeclared identifier
c:\Torque\SDK\engine\ai\fuzzyLogic.cc(22): error C2146: syntax error : missing ';' before identifier 'cfuzzyLogicfuzzify'
Once again Thanks guys, Im new to Torque and over the last couple of months I really have learned a lot. Hopefully I'll get this resource finished soon so everyone can get make better ai for their games and I'll get a 1st from university.
#9
12/16/2005 (7:36 am)
Well, can somebody help me please.
#10
Also, if you are using ConsoleStaticMethod, minimum arguments is 1 not 2 as with consolemethod.
The following compiles fine for me :-
12/16/2005 (8:46 am)
I have a feeling that the return methods dont know anything about an int type but do know about S32 types.Also, if you are using ConsoleStaticMethod, minimum arguments is 1 not 2 as with consolemethod.
The following compiles fine for me :-
ConsoleStaticMethod(fuzzyLogic,fuzzify,[b]S32,1,1[/b],"fuzzify")
{
[b]S32[/b] a = 926;
return a;
}
#11
It works now but only in torqueDemo_DEBUG.exe
but in torqueDemo.exe I get the following errors in the console:
demo/server/scripts/aiNPC.cs (244): Unable to instantiate non-conobject class fuzzyLogic.
demo/server/scripts/aiNPC.cs (245): Unable to find object: '0' attempting to call function 'fuzzify'
How do I get it to work in torqueDemo.exe
Thanks
12/17/2005 (1:35 am)
Thanks David :)It works now but only in torqueDemo_DEBUG.exe
but in torqueDemo.exe I get the following errors in the console:
demo/server/scripts/aiNPC.cs (244): Unable to instantiate non-conobject class fuzzyLogic.
demo/server/scripts/aiNPC.cs (245): Unable to find object: '0' attempting to call function 'fuzzify'
How do I get it to work in torqueDemo.exe
Thanks
#12
select 'Torque Demo' in the solution explorer (usually on the right unless you moved it or got rid of it).
On the taskbar at the top the is a drop down box with debug written in it. Just change it to release and Torque will now compile to torqueDemo.exe instead of torqueDemo_DEBUG.exe
Hope that helps, its often the simplest problems that are the hardest to find an answer to!
12/17/2005 (11:56 am)
Sorted, if anyone else has this problem open up C:\Torque\SDK\vc7\Torque SDK.sln in Visual Studio .NETselect 'Torque Demo' in the solution explorer (usually on the right unless you moved it or got rid of it).
On the taskbar at the top the is a drop down box with debug written in it. Just change it to release and Torque will now compile to torqueDemo.exe instead of torqueDemo_DEBUG.exe
Hope that helps, its often the simplest problems that are the hardest to find an answer to!
#13
04/19/2009 (7:32 pm)
City...I just have to say thank you for your follow up post on this. I have been beating my head against the desk for hours trying to figure out what was going on. I switched to release as you suggested and it works great. Thank you again.
Torque Owner Westy
be after the definition of the class?
Not too sure on this one, but try:
#include "console/simBase.h" //---------------------------------------------------------------------------- class fuzzyLogic : public SimObject { typedef SimObject Parent; public: fuzzyLogic(); ~fuzzyLogic(); }; DECLARE_CONOBJECT(fuzzyLogic); IMPLEMENT_CONOBJECT(fuzzyLogic);