Accessing the scene from an external class

Hi all,

In the game I’m currently writing the player and the enemies are all external classes.  I need the player class to dispatch an event that the enemies are listening for and respond to.  What I can’t figure out is how to reference the scene from the external player class.  I’m sure it’s easy but just can’t seem to figure it out.

Thanks in advance.

Chris

Hi there,

Your external objects could query composer to find out what the current scene is and then pass messages from there.  Basically:

--Returns the currently active scene local currScene = composer.getSceneName( "current" )

(See this page for more info).

Another option would be to pass a reference to the current scene when you instantiate your player/enemy objects:

So, in the code for your player object:

local public={} --Local variable holding reference to the main scene local sceneObject = nil local function setScene(obj)   sceneObject = obj end public.setScene = setScene return public

Then when you instantiate the player object in your scene:

--Pass the current scene playerObject = require("playerObject") playerObject.setScene(thisScene)

playerObject will then have a reference to the current scene.  (The syntax on this may not be perfect, I’ve just typed it in, but you get the idea).

Hope this helps,

Simon

@happymongoose

Cheers for that Simon.  Sorry it’s taken so long for me to reply.  I’ll give that a bash tonight when I get home.

Hi there,

Your external objects could query composer to find out what the current scene is and then pass messages from there.  Basically:

--Returns the currently active scene local currScene = composer.getSceneName( "current" )

(See this page for more info).

Another option would be to pass a reference to the current scene when you instantiate your player/enemy objects:

So, in the code for your player object:

local public={} --Local variable holding reference to the main scene local sceneObject = nil local function setScene(obj)   sceneObject = obj end public.setScene = setScene return public

Then when you instantiate the player object in your scene:

--Pass the current scene playerObject = require("playerObject") playerObject.setScene(thisScene)

playerObject will then have a reference to the current scene.  (The syntax on this may not be perfect, I’ve just typed it in, but you get the idea).

Hope this helps,

Simon

@happymongoose

Cheers for that Simon.  Sorry it’s taken so long for me to reply.  I’ll give that a bash tonight when I get home.