Do funcitons in template objects not work?
oranjoose

Hello,

I noticed that when I have a sceneobject on the scene that has the template option turned off, the functions in the functions tab work properly (such as onUpdate), but when I turn the template option on, and then clone and add the sceneobject within app.js, the cloned sceneobject is created without problem, but the functions belonging to the sceneobject no longer work.

Is this by design, or am I doing something wrong?

All 3 Comments
oranjoose

Figured it out. When calling addSceneObject, gotta make the second argument true in order to enable events for the added object.

Gio

Hi

You are correct, you can call addSceneObject with the second argument set to true to automatically enable events for a SceneObject. This will enable all events that have been defined at that point. So if the object had an onMouseDown function, it will start receiving onMouseDown events. If it did not have that function and the function was created after the object was added to the scene, it won't be registered to receive that event.

You can manuall enable / disable events at any point using SceneObject.listenFor and SceneObject.stopListeningFor.

However, template objects do NOT receive events.

Template objects are a bit special. They do not receive events and are set to invisible by default. Their main purpose is to be cloned.

For example if you had a player shooting bullets, you would create a single bullet in the editor as a template object. This is going to be added to the scene, but it won't be drawn (by default) and won't receive any events (so it will not move, will not collide, etc).

When your player shoots a bullet, you would just do:

var myBullet = bulletTemplate.clone();
wade.addSceneObject(myBullet, true);

myBullet is now a regular SceneObject that receives all events, such as onUpdate.

Another possible use for template objects is triggers. For example, you may want to see if you character overlaps a template object (that you can see in the editor, but the player can not see in the game), and trigger an event:

if (player.overlapsObject(myTemplateObject))
{
    door.open();
}

I hope this helps.

oranjoose

Thank you for the additional information. That is very supportive.

I don't want to tell you how to make your own engine, but I figure it might be more intuitive for a beginner if the engine checks if there is any event code belonging to the SceneObject, and set true to listen for events by default, and false if the SceneObject has no event code. If the developer does in fact want to override the default and enable or disable listening for events, they can do that manually after the object has been added. Basically, it would just check the functions object within the sceneobject found inside the .wsc file. If it is empty, then autolisten false and if the functions object has stuff in it, then autolisten true by default.

Of course, this is just a suggestion. Feel free to ignore it =).

Post a reply
Add Attachment
Submit Reply
Login to Reply