Jump to content

Meaning verification DefineEngineFunction & ConsoleFunction


Torr

Recommended Posts

I'm new of Torque3D


Looking the source to understand how to expose function to c-script I found these: DefineEngineFunction, ConsoleFunction


Can you please confirm me if I've understood well?


With DefineEngineFunction the function exposed can be called either on console and c-script but when the console is removed / dropped this function is available only for c-script.


With ConsoleFunction the function is available either on console and c-script but when the console is removed / dropped the function cannot be accessed by none of the two.


How can I drop the console to make a test?

Link to comment
Share on other sites

I'm not entirely sure what you mean with "dropping the console".


ConsoleFunctions is, essentially, a deprecated version of DefineEngineFunction. They serve the same purpose, DefineEngineFunction is simply a bit more sophisticated, and allows for typing declaration of the function (maybe more?).

As far as I know, DefineEngineFunction actually uses ConsoleFunction internally.

Link to comment
Share on other sites

I've created this class:

 

class MyFirstClass : public ConsoleObject {
public:
typedef ConsoleObject Parent;

public:
String mName;

public:
DECLARE_CONOBJECT(MyFirstClass);
MyFirstClass();

static void initPersistFields();
};

 

And I want init an object of it using c-script, so i written:

%firstObject = new MyFirstClass();
%firstObject.name = "Text written from .cs file";
echo(%firstObject.name);

 

but I get an error that says this I need inerith from SimObject. I attached a screenshot with the full error text.


How can I Init this object in c-script?

How can I Init an object in c++ and register its pointer to the memory manager?

Untitled.png.f5293a473c8de40ddd27f65cbedb931c.png

Link to comment
Share on other sites

Your code looks fine as far as I can tell but inheriting from SimObject (or even lower, to SceneObject or GameBase) is typically what you want to do. The only classes in the engine that inherit from ConsoleObject are SimObject and NetEvent, neither of which are meant to be instantiated directly from script.


This was written for an older version of Torque but is still (mostly) relevant and might be of use to you:

Class Hierarchy - ConsoleObject

Link to comment
Share on other sites

Thanks for this information, but if the ConsoleObject cannot be instantiated, why they put "Console" in the name?


What stand this name SimObject?


Since I can't find nothing about memory management or Garbage collector, This engine uses a memory system or it's demand to the programmer to manage it?


If you know, specify it for either c++ and c-script please.

Link to comment
Share on other sites

The point of ConsoleObject is just to define some basic functionality required to interface with the console/scripting system. SimObject inherits from ConsoleObject but adds additional required functionality. simObject.h explains this is more detail. I think it used to be possible (waay back in the early TGE days) to instantiate a ConsoleObject directly but that's no longer the case. If you look at line 869 in compiledEval.cpp you'll see why you get an error.


As far as memory management, TorqueScript isn't garbage collected and there's no concept of new'd objects going out of scope, so it's possible to leak memory if you're not careful. Few examples to demonstrate:

 

// $myObj is a global var that stores the 'SimId' mapped to MyFirstObject()
// I can reference $myObj from any script
$myObj = new MyFirstObject();
 
// But if I remap $myObj to something else...
$myObj = "Oh no!";
// Then I've essentially created a memory leak: MyFirstObject() is still exists but I have no reference to it

 

function CreateObject()
{
   // %myObj is a local var that stores the 'SimId' mapped to MyFirstObject()
   // The %myObj variable will be destroyed when the function goes out of scope, but MyFirstObject() will still exist...
   %myObj = new MyFirstObject();
}

 

function CreateObject()
{
   // %myObj is a local var that stores the 'SimId' mapped to MyFirstObject()
   // The %myObj var will be destroyed when the function goes out of scope, but MyFirstObject() will still exist...
   // However, I've also given MyFirstObject() a name this time: 'MyObject' that I can reference anywhere - memory leak averted!
   %myObj = new MyFirstObject(MyObject);
 
   %myObj.doStuff();
   MyObject.doStuff();
}
 
%myObj.doStuff(); // will cause a console error - %myObj not found
MyObject.doStuff(); // no error
Link to comment
Share on other sites

Thanks for this, since the doc is not so wide, expecially for c++ part, this thread is very important.


Now I'm looking on how to instantiate a mesh, so I looked at player class.


Do you have a link where is explained how to instantiate it? Or the only way is to check the current existent classes?


Thanks!!

Link to comment
Share on other sites

Typically the two renderable classes you'll work with are TSStatic (a lightweight static mesh class with basic support for animations, collision, etc.) and StaticShape (like TSStatic but with much more functionality, including datablock support).


TSStatic is usually used for static scenery objects like buildings, etc. StaticShape tends to be used for gameplay objects like CTF flags, doors, etc.


There are also examples in the engine that explain how to create your own class that renders a mesh and uses a datablock. See the renderMesh, renderObject, and renderShape.cpp/h files for more on those.

Link to comment
Share on other sites

Here's the specific documentation bit that pertains to that console initialization stuff:


https://github.com/GarageGames/Torque3D/blob/a858fa775f9a96123a56cd66b302fd92d04ef0c5/Engine/source/console/consoleObject.h#L101-L197


So while you look good on the header side of things for the declaration of the class object, you'll also want to have the IMPLEMENT_* macro in your cpp file for when you define all the functions and stuff. There's two macros to use IMPLEMENT_CONOBJECT and IMPLMENENT_CO_NETOBJECT_V1


you use the NETOBJECT stuff in the event you need this class to be ghostable down to the client. If you use the regular conobject stuff, it'll only exist where you create it(create it on the server, it exists only on the server, no clients are aware of it, etc).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...