urho3d/Urho3D — pull request #107 by alexparlett (2014-01-02 17:09:59+00:00)
Title: Add the ability to allow pure script objects that are not created through a ScriptInstance to handle events

This works like it currently does using:

SubscribeToEvent("Foo","FooHandler");

Where if called inside a procedural function the ScriptFile will handle it, if called inside an object if that object was created through a ScriptInstance that ScriptInstance will handle the event, otherwise the ScriptFile associated with the current script module will handle it.

There is one caveat with this should you can't have multiple objects have the same event with no sender specified ie:

class Foo()
{
...
Foo()
{
SubscribeToEvent("Update","FooUpdateHandler");
}
...
}

class Bar()
{
...
Bar()
{
SubscribeToEvent("Update","BarUpdateHandler");
}
...
}

Foo@ foo;
Bar@ bar;

void Start()
{
@foo = Foo();
@bar = Bar();
}

Only Bar's event hanlder would be called as Foo's handler would be erased.

This also make starting directly in objects when using a ScriptFile easier, as that main object can handle all necessary events like Update and pass them down the chain instead of requiring a procedural function to do so meaning no global object declarations are necessary.

scriptFile_ = GetSubsystem()->GetResource("Scripts/Editor.as");

if (scriptFile_)
scriptObject_ = scriptFile_->CreateObject("Editor");

// If script loading is successful, proceed to main loop
if (scriptObject_ && scriptFile_->Execute(scriptObject_, "void Start()"))

~~(No hints as what's in the process of being re-written with this change)~~