Meaning verification DefineEngineFunction & ConsoleFunction

Expanding and utilizing the engine via C++.
  • 1
  • 2
12 posts Page 1 of 2
Torr
Posts: 6
Joined: Wed Jun 28, 2017 11:29 pm
by Torr » Fri Jun 30, 2017 1:35 am
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?
LukasPJ
Site Admin
Posts: 358
Joined: Tue Feb 03, 2015 7:25 pm
 
by LukasPJ » Fri Jun 30, 2017 3:18 am
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.
Torr
Posts: 6
Joined: Wed Jun 28, 2017 11:29 pm
by Torr » Fri Jun 30, 2017 4:07 pm
Oh, Thanks you! I've understood the wrong thing by reading the code documentation.

So each function, object, class that is exposed to c-script is also available to console?
Torr
Posts: 6
Joined: Wed Jun 28, 2017 11:29 pm
by Torr » Fri Jun 30, 2017 5:02 pm
I've created this class:

Code: Select all

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:

Code: Select all

%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?

Attachments

Untitled.png
Untitled.png (2.97 KiB) Viewed 388 times
Happenstance
Posts: 12
Joined: Sat Apr 11, 2015 9:08 pm
by Happenstance » Fri Jun 30, 2017 7:27 pm
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
Torr
Posts: 6
Joined: Wed Jun 28, 2017 11:29 pm
by Torr » Sat Jul 01, 2017 1:30 am
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.
Happenstance
Posts: 12
Joined: Sat Apr 11, 2015 9:08 pm
by Happenstance » Sat Jul 01, 2017 1:34 pm
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
Torr
Posts: 6
Joined: Wed Jun 28, 2017 11:29 pm
by Torr » Sat Jul 01, 2017 7:35 pm
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!!
Happenstance
Posts: 12
Joined: Sat Apr 11, 2015 9:08 pm
by Happenstance » Sat Jul 01, 2017 8:44 pm
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.
Torr
Posts: 6
Joined: Wed Jun 28, 2017 11:29 pm
by Torr » Mon Jul 03, 2017 9:07 am
I searched for the examples, on google and on git-hub, but I've not found nothing. Can you please give me the link to these?
  • 1
  • 2
12 posts Page 1 of 2

Who is online

Users browsing this forum: Baidu [Spider] and 6 guests