Sign in to follow this  
jroosterman

Get reference to Scene from Action Manager Event

Recommended Posts

I set up an OnKeyDownTrigger Action for my scene.

scene.actionManager = new BABYLON.ActionManager(scene);
scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyDownTrigger, keyDownAction));
        
...

 var keyDownAction = function (evt) {
    	if (evt.sourceEvent.keyCode == 37) {
    	        evt.getSceneSomehow().doSomethingWithScene();
	} else if (evt.sourceEvent.keyCode = 39) {
		evt.getSceneSomehow().doSomethingELSEWithScene();
	}
};

In the keyDownAction I want to get a reference to the scene that registered the action.

Can I do this?

Share this post


Link to post
Share on other sites

Hi JR.  I'm not sure why you would ever need to do that. 

var keyDownAction = function (evt) {
    console.log(scene);     
};

The actionManager is for the current scene.  This actionManager registered a func within THAT scene's scope... called keyDownAction().  That func only exists in the scope of the current scene.  The global scene variable is the scene you want, and it is ready for 'action'.  ;)

For more scenes...

scene2.actionManager = new BABYLON.ActionManager(scene2);
scene2.actionManager.registerAction...
scene3.actionManager = new BABYLON.ActionManager(scene3);
scene3.actionManager.registerAction...

This playground uses a scene.actionManager keydown func in lines 54-58. In line 57, I change the scene's background color... for every keydown.  Unless I am mistaken (often  I am), there is no need to derive/glean a ref to a scene object... from an evt object. 

Perhaps I have misunderstood, or I am blind to potential use-cases.  It MIGHT be add-able to the BJS actionManager event object, but I'm not sure if it will be used.  Using our 3-scene example above, are you wishing that all 3 scenes... use the same keyDownAction() handler?  *nod*.  I guess I can understand that.

Smarter people than I may comment soon.  Stay tuned.

Share this post


Link to post
Share on other sites

I wouldn't say that it's a global scene variable, but anything in scope where the function is declared will be available via Lexical Scoping:

https://developer.mozilla.org/en/docs/Web/JavaScript/Closures

Scene is not available from the OnKeyDownTrigger evt - here is what is available:

https://doc.babylonjs.com/classes/3.0/actionevent

So, looks like best bet is to have it in scope and then just use it - unless evt.meshUnderPointer.scene will always work?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.