Exposing protected and private class members to script
by Noah Dyer · in Torque Game Engine · 01/06/2006 (7:11 am) · 4 replies
I just wanted to make sure I understand the scripting to engine interface correctly. I can't manipulate initialize a class variable in script without exposing it via addField. But if I expose a private or protected class member to script for initialization purposes, there is no access protection to prevent the variable from being change later. TorqueScript doesn't have the equivalent of public, private, and protected, right? If that is correct, in most cases I can see it being convenient. It's just that I forsee some instances where a novice scripter/programmer like me is going to accidentally change a variable that I didn't intend to :)
#2
Thanks.
01/06/2006 (3:08 pm)
Clear as water, thanks for the great reply. It makes sense that they'd do everything to make TorqueScript as easy to use as possible, and if that includes removing access specification and type declarations, so be it. Haven't worked with PERL myself, but your description of it makes me think I ought to start ;) I hope your right and that my scripts are for the most part problem free even though I can't "protect" myself in the same ways as I can with C++.Thanks.
#3
Most of the time the technique is to create a 'real' accessor method in C++ for your protected/private member variable, and then create a ConsoleMethod that calls the accessor method. In this ConsoleMethod is where you might put any "safety" checks you would like to have.
01/06/2006 (5:50 pm)
FYI, the alternate method of allowing access to member variables to script is by creating ConsoleMethods that act as your standard accessor/helper methods in "normal" C++. You can then write within the ConsoleMethod any restrictions you like, such as checking for a flag on the object for what state it's in (initialized, unchangable, whatever you like).Most of the time the technique is to create a 'real' accessor method in C++ for your protected/private member variable, and then create a ConsoleMethod that calls the accessor method. In this ConsoleMethod is where you might put any "safety" checks you would like to have.
#4
01/07/2006 (6:15 am)
That makes absolute sense. So instead of initializing protected members in the script initialization list, you would just have a few calls to the appropriate "set" functions after the list. Sounds Ideal.
Torque Owner Kirby Webber
You are correct, in Torque script there is no definition of Public, Private or Protected. In fact, Torque script is typeless as well, meaning you don't have to bother with int, float or char declarations - which some people love, and some people don't. (I personally love it, but then it reminds me of PERL - which makes me all warm and fuzzy. =P)
As for inadvertantly changing variable that you don't want changed, I think you'll be surprised how little that sort of scenario plays out.
For one thing, every variable that is accessible from script (aside from globals of course) is "housed" within a namespace. This means that when you alter %this in a wheeledVehicle script, it has absolutely no affect on %this in player.cs.
Clear as mud?