Jump to content

Get reference to Scene from Action Manager Event


jroosterman
 Share

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?

Link to comment
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.

Link to comment
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?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...