clone and events

Discussion of proposed new features.

clone and events

PostPosted by dakilla » 21 Jun 2016, 13:39

Any way to also clone subscribed events for components and nodes when cloning ?

I have some components and nodes that subscribes to events, when cloning them, clones have lost events.
Thanks.
User avatar
dakilla
Have some posts
Have some posts
 
Posts: 31
Joined: 27 Jan 2016, 05:10

Re: clone and events

PostPosted by cadaver » 21 Jun 2016, 14:09

Cloning uses the exact same method as load/save, attribute serialization. Event subscription status is separate from this.

If possible, I'd just recommend making the event connections in OnNodeSet() or Start() / DelayedStart() functions, as appropriate.
User avatar
cadaver
Urho3D author
Urho3D author
 
Posts: 1802
Joined: 16 Jan 2014, 14:52
Location: Finland

Re: clone and events

PostPosted by dakilla » 22 Jun 2016, 08:42

ok.
And what about to send a cloned event with the clone ptr in map to trigger actions for clones when a object is cloned ? maybe usefull for lot of things like auto registering to some other events :?

I added :

Code: Select all
/// A component has been cloned.
URHO3D_EVENT(E_COMPONENTCLONED, ComponentCloned)
{
    URHO3D_PARAM(P_SCENE, Scene);                  // Scene pointer
    URHO3D_PARAM(P_NODE, Node);                    // Node pointer
    URHO3D_PARAM(P_COMPONENT, Component);          // Component pointer
}


and at end of Node::CloneComponent()

Code: Select all
    // Send component cloned event
    using namespace ComponentCloned;

    VariantMap& eventData = GetEventDataMap();
    eventData[P_SCENE] = scene_;
    eventData[P_NODE] = this;
    eventData[P_COMPONENT] = cloneComponent;
    SendEvent(E_COMPONENTCLONED, eventData);


It works for cloned components, but I'm not sure for cloned Node where to send a similar event.
User avatar
dakilla
Have some posts
Have some posts
 
Posts: 31
Joined: 27 Jan 2016, 05:10

Re: clone and events

PostPosted by cadaver » 22 Jun 2016, 08:51

Events are often used for when something happens that you can't directly anticipate. For example screen resize. The scene already sends node and component creation/deletion events, which are used by the editor for keeping the hierarchy window up to date. But that doesn't differentiate between cloning and other methods of creating new nodes.

Since the engine doesn't ever call Clone() on its own, but it's always your application logic and you know when it's called, you could follow with any post-cloning setup imperatively, just by calling some function in the cloned component(s). So I don't see a separate engine-defined event necessary here. Or if you wish, you could send an event yourself at the same time when cloning.
User avatar
cadaver
Urho3D author
Urho3D author
 
Posts: 1802
Joined: 16 Jan 2014, 14:52
Location: Finland

Re: clone and events

PostPosted by dakilla » 22 Jun 2016, 09:35

ok right. E_COMPONENTADDED should be sufficiant.
however I get some crash using the component ptr from E_COMPONENTADDED after cloning.

When I look the CloneComponent code, SafeCreateComponent (who call addComponent and then trigger the event E_COMPONENTADDED), the sended component does not have yet attributes applied by CloneComponent, this event shouldn't be sended only after ? (when I move the send event code after it fix the crash)
User avatar
dakilla
Have some posts
Have some posts
 
Posts: 31
Joined: 27 Jan 2016, 05:10

Re: clone and events

PostPosted by cadaver » 22 Jun 2016, 12:56

The meaning of the event is exactly what it says (component added). It doesn't tell anything of the attribute contents, which could be populated some time after, or possibly never (In case it's a node/component creation outside serialization or cloning.)

At the time when the component is added, and the event is sent, Node code cannot know what case it is.
User avatar
cadaver
Urho3D author
Urho3D author
 
Posts: 1802
Joined: 16 Jan 2014, 14:52
Location: Finland

Re: clone and events

PostPosted by cadaver » 23 Jun 2016, 07:02

On second thoughts, the Node::Clone() function knows best when it's done with setting each components' (and its own) attributes, and also the mappings to new and old component. Therefore it could in fact make sense to have events sent from the engine code. Cloning shouldn't be a high-frequency operation (not any more frequent than instantiation of prefabs) and furthermore the events should be cheap performance-wise if no-one is listening to them.
User avatar
cadaver
Urho3D author
Urho3D author
 
Posts: 1802
Joined: 16 Jan 2014, 14:52
Location: Finland

Re: clone and events

PostPosted by cadaver » 23 Jun 2016, 11:52

Added in master branch.
User avatar
cadaver
Urho3D author
Urho3D author
 
Posts: 1802
Joined: 16 Jan 2014, 14:52
Location: Finland

Re: clone and events

PostPosted by dakilla » 24 Jun 2016, 07:47

great thanks.
User avatar
dakilla
Have some posts
Have some posts
 
Posts: 31
Joined: 27 Jan 2016, 05:10


Return to Feature Request

Who is online

Users browsing this forum: No registered users and 0 guests

cron