Author Topic: Help with Schedule  (Read 1705 times)

So I have this code...
Code: [Select]
function FOOBAR_test()
{
echo("FOOBAR_test() called");
}

function FOOBAR::test()
{
echo("FOOBAR::test() called");
}

// This works
schedule(1000, 0, "FOOBAR_test");

// This doesn't
schedule(2000, 0, "FOOBAR::test");

As the comments indicate, the first schedule works but the second doesn't.

And just to clarify, FOOBAR is not a namespace. FOOBAR::test is the whole name.

In functions, if you use :: then it treats it as a namespace. You need to create an object with classname or name of "FOOBAR" to be able to use the function on.

In functions, if you use :: then it treats it as a namespace. You need to create an object with classname or name of "FOOBAR" to be able to use the function on.

http://docs.garagegames.com/tgea/official/content/documentation/Scripting%20Reference/Introduction/TorqueScript.html#Namespaces
Code: [Select]
// Not really namespaces
function Ver1::doIt()
{
    ...
};
           
function Ver2::doIt()
{
    ...
};

According to that reference, this isn't always true.

Oh, and additionally, I have created unique functions with ::'s in their name before and called them without problems. I suspect that schedule() tries to interpret such names as belonging to a namespace, and doesn't look for any variables literally named with ::'s. I would just write my variables differently, but it'd be kind of irking to think that one would have to adjust their variable naming style (or make out-of-place exceptions) just to account for such a quirk.

Alternatively you can just use an underscore (_) instead of ::

Alternatively you can just use an underscore (_) instead of ::
That's what I've ended up doing with the mod I was working on. It seems a similar problem arises when I attempt map a function to a key, e.g....
Code: [Select]
// This works
$remapCmd[$remapCount] = "FOOBAR_test";

// This doesn't work
$remapCmd[$remapCount] = "FOOBAR::test";

At least, this held true for the tests I enacted.

That's what I've ended up doing with the mod I was working on. It seems a similar problem arises when I attempt map a function to a key, e.g....
Code: [Select]
// This works
$remapCmd[$remapCount] = "FOOBAR_test";

// This doesn't work
$remapCmd[$remapCount] = "FOOBAR::test";

At least, this held true for the tests I enacted.

Yes, callbacks can typically only be functions, not methods.

This is because the function name is not FOOBAR::test. The function (or more precisely method) name is test inside the class of FOOBAR.

Put another way, FOOBAR::test does not exist as a function. It's more like FOOBAR is a box with functions inside it, and the function name is test.

The call and schedule functions cannot access methods because it interprets the :: as part of the function name when in reality, as I've covered, it's not.

Solution: container function:

Code: [Select]
function FOOBAR::test() {
    echo("FOOBAR::test called.");
}
function FOOBAR_test() {
    FOOBAR::test();
}
schedule(1000, 0, FOOBAR_test);

The thing is, this should never be needed. You should only ever create methods as part of a class you intend to use with the first variable being the associated object of that class. If you need to call a method off a timer, use SimObject::schedule( timems , function name , args , ... ) on the object you've created to properly utilize classes.

FOOBAR.schedule(1000,test);

Assuming FOOBAR is an actual object, which it should be.
« Last Edit: March 18, 2015, 11:32:53 PM by elm »

It seems I'm not being entirely clear. Yes, I understand that the use of :: has other implication with objects and what not. No, I'm not trying to create/use an object. I simply want to use the :: for the sake of variable and function name organization. Refer to another Coding Help topic I posted recently to get a clearer picture of where I'm coming from.

Sorry for any confusion.

Simply create a script object that's only purpose is to help your organize your functions then?

Simply create a script object that's only purpose is to help your organize your functions then?

Eh... better than nothing. I can roll with that.

It seems I'm not being entirely clear. Yes, I understand that the use of :: has other implication with objects and what not. No, I'm not trying to create/use an object. I simply want to use the :: for the sake of variable and function name organization. Refer to another Coding Help topic I posted recently to get a clearer picture of where I'm coming from.

Sorry for any confusion.
And refer to my reply to that for the reason why this doesn't work for functions

Refer to another Coding Help topic I posted recently to get a clearer picture of where I'm coming from.

Oops. Forgot to provide a link: http://forum.blockland.us/index.php?topic=276461.0

And refer to my reply to that for the reason why this doesn't work for functions

Functions are different though. double colons is permitted only once in a function name, and they're used for methods; you name them in the pattern ClassName::FunctionName and then you make a new script object (or some other types) $test = new ScriptObject() { class=ClassName; }; and call $test.FunctionName()

Ah, yes. I must've read over this or something. Anyyyway, that's all cleared up. Thanks!