Game Development Community

How to access public functions from script

by Howard Dortch · in Torque Game Engine · 07/18/2004 (10:33 am) · 4 replies

I am trying to figure how to access a function in CC code from script.

public:
//creation methods
DECLARE_CONOBJECT(GuiRadarCtrl);
GuiRadarCtrl();
static void initPersistFields();
static void consoleInit();

//Parental methods
bool onWake();
void onSleep();

void setBitmap(const char *name);
void setBitmap(const TextureHandle &handle);
void onRender(Point2I offset, const RectI &updateRect);
void SetRange(F32 Range);
void RadarOnOff(bool RState);
void MapOnOff(bool MState);

I have one of these declared in my gui script file and want to turn the map off. How do I set the MapOnOff(false); ?

#1
07/18/2004 (11:12 am)
You need to make the Console/TorqueScript aware of your functions/methods with the appropriate macros
(ConsoleFunction and ConsoleMethod)
Here is two links from the docs :
www.garagegames.com/docs/torque/general/ch07s04.php

www.garagegames.com/docs/torque.sdk/engine/Console.php

Read it, as you should already have done :)
#2
07/18/2004 (1:03 pm)
Well I had read that. In the example above DECLARE_CONOBJECT(GuiRadarCtrl);
makes the class available to script.
In script I new GuiRadarCtrl(Radar){} so now there is an object made from that class I called Radar. So Radar.MapOnOff(true) should work. no?
#3
07/18/2004 (1:26 pm)
Nope, as you haven't exposed any of the member functions to script, that's what ConsoleMethod/ConsoleFunction are for : exposing C++ functions and member functions to TScript.
The DECLARE macros (and their matching IMPLEMENT macro) only allow you to construct an instance of that class in torquescript, by making the console aware of your class

Read the interfacing with C++ parts of the console chapters, plus look at some of the existing classes in code as that will make it all clear :)

hth
#4
07/18/2004 (1:29 pm)
No.

You might want to read the second link again.

You need to use the "ConsoleMethod" macro to define exactly what functions can be accessed. Yes, you may be able to generate one of these objects from script simply because of the "DECLARE_CONOBJECT" macro but this doesn't automatically expose all the member functions to script. You need to explicitly define functions, their parameters and what they return using the "ConsoleMethod".

Use the "ConsoleFunction" when you want to execute a non-class function.

- Melv.