Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
SceneManager and access to scene variables — Gideros Forum

SceneManager and access to scene variables

HolonistHolonist Member
edited October 2014 in General questions
I use SceneManager to manage my scenes.
Is there a way to access a scene initiated by SceneManager from outside?

Here is what I tried.


GameScene.lua:

GameScene = Core.class(Sprite)

function GameScene:init()
self.hello = TextField.new(nil, "hello")
self.hello:setPosition(50,50)
self:addChild(self.hello)
end



main.lua:

sceneManager = SceneManager.new({
["gameScene"] = GameScene
})

stage:addChild(sceneManager)
sceneManager:changeScene("gameScene", SceneManager.fade, 0, easing.outBack)

sceneManager.scenes["gameScene"].hello:setText("changed")


Error message: main.lua:12: attempt to index field 'hello' (a nil value)

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @Holonist you can only access an instantiated scene (the one that has instance) as in current scene through
    if sceneManager.scene1 and sceneManager.scene1.hello then
        sceneManager.scene1.hello:setText("changed")
    end
    But other scenes might not have instances, so you would only accessing their classes, which won't do you any good
  • Thanks.

    It works. I was a bit confused about the if-query, because in this simple scenario it is clear that the scene instance exists. But apparently what matters is how you call the scene.

    However, thanks again!!
  • @ar2rsawseen

    I just realized to use scenemanager this way doesn't make any sense, or?
    I planned to have a "main scene" (gameScene) where the most important variables are stored and that scene is always active.

    But I should rather just store those main variables in the init file
  • You can store main variables in a global table in the init file
  • talistalis Guru
    edited October 2014
    Or you can always pass values between scenes as a paramater in your constructor when you are initiating(creating instance) your scene.

    Passing values between scenes:
    http://giderosmobile.com/forum/discussion/1474/passing-variables-with-scene-manager/p1

    Likes: Holonist

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.