Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Can someone please help to explain this? :) — Gideros Forum

Can someone please help to explain this? :)

NinjadoodleNinjadoodle Member
edited October 2017 in General questions
Hi guys

I've been playing around with variables and have been declaring them in this way
Scene1 = gideros.class(Sprite)
 
local monkey = 12
 
function Scene1:init()
 
    print(monkey)
Basically I have two scenes.

As expected if I print(monkey) in Scene1, I get the number 12.

If I print monkey in Scene2, I get NIL, as monkey was declared as a local in Scene1 lua file

However, if I add 12 to monkey on a touch event etc. inside Scene1 ... goto Scene2 ... and come back to Scene1 ... I now get 24 instead of 12.

Does this mean that the declaration of monkey outside of the scene's init() function only happens on the first time the lua file is loaded?

Thanks guys!

Comments

  • SinisterSoftSinisterSoft Maintainer
    edited October 2017 Accepted Answer
    Be careful because when exported all the Lua files are merged together when exported as a full export (the luac command has an option to do this). The exception to this is if you exclude it from execution (right-click the file in the list to see a menu) and add it with a 'require'.

    We had to do it this way to get Macros (which can be used for pseudo equates) working properly.

    I think that in any event, variables are made in the order they are seen - as Lua is interpreted. So the order of files in the list can be important.

    Likes: antix, Ninjadoodle

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+2 / -0 )Share on Facebook
  • Hi @SinisterSoft

    Thanks for the reply :)

    Will this setup cause me problems with a full export ...
    Scene1 = gideros.class(Sprite)
     
    local monkey
     
    function Scene1:init()
      monkey = 12
    end
     
    Scene2 = gideros.class(Sprite)
     
    local monkey
     
    function Scene2:init()
      monkey = 6
    end
    Will the variables stay local to their respective files?

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • I'm on my mobile right now so can't try it. Export it to html5 and play it - you can see print messages by pressing f12.

    Likes: Ninjadoodle

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • I think that should be fine! If it isn't, I'm in trouble.

    Likes: Ninjadoodle

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Thank for the help guys, I think I have this sorted - so far no problems :)

    Likes: antix

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