Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
A question about functions — Gideros Forum

A question about functions

NinjadoodleNinjadoodle Member
edited October 2017 in General questions
Hi guys

I wonder if anybody can help with this ...

I have a scene, and I have a function that I would like to reuse inside each scene (it sets up local variables and some basic gfx etc.)

is there a way of using the function in a way similar to below, which would make the variables setup local to the scenes init() function and not the original function? (I know about using self etc and passing that, but I would like to use local vars).
Stage01 = gideros.class(Sprite)
 
function Stage01:init()
 
	setupStage()
 
        print(num)
 
end
 
-- in a separate file
 
function setupStage
 
        local player = "ninja"
        local num = 20
 
end
Thanks in advance for any help :)

Comments

  • keszeghkeszegh Member
    edited October 2017 Accepted Answer
    if you don't insist on local variables then it's easy to do with self.player etc. also a nicer solution would be to have a common base class containing setupStage and your stage classes all inherit from this base class.

    however, if you insist on local then i'm not sure you can do it, as it goes against the meaning of 'local'. i thought that loadfile (EDIT: i meant loadstring) could be an ugly workaround, but according to docs https://www.lua.org/pil/8.html loadstring always compiles in a global enviroment so it is not exactly that its code is injected in the place where it is loaded (as i hoped so).

    maybe people with more experience with lua have some ideas.

    all in all i'd go with the self.player and self.num route, you anyway probably use these values in other functions of the class too and being self.* vs local * is not that huge speed penalty i guess, i never felt it in my apps.

    another way would be to add all these values as parameters to the init function.

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • totebototebo Member
    edited October 2017 Accepted Answer
    Not sure what you're trying to do, but I think you want to have multiple instances of a class using local variables with different values? If so, this is not possible, because local vars are shared amongst all instances. Self is the only option, if the vars need to store different values.

    Likes: Ninjadoodle

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Hi guys

    Thanks for the help :) To put it simply I would just like to inject a piece of reusable code into a scenes init function, and only have the code take effect once it has been injected.

    This way I don't have to keep rewriting the same code in many scenes, and I can avoid using self.***

    for example ...
    Stage01 = gideros.class(Sprite)
     
    function Stage01:init()
     
    	require("setupStage")
            print(num)
     
    end
     
    -- in a file called setupStage
     
    local num = 20

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • piepie Member
    Accepted Answer
    The problem is that local functions are available only inside their scope.
    Why do you hate self? :D
    If you just need to insert variables you could read them from a json file, if you need functions I am afraid that you would need to use self or at least one global function that returns everything you need.
    However, it seems that it is not crazy to localize global functions

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • love your "self" @Ninjadoodle @};-

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • Hi guys

    Thanks heaps for your input on this, I really appreciate it :)

    I've decided to use self + a global function for my stage setup code, and everything is working well.

    @antix - lol

    Thanks again!
  • Just till say, I also dislike self, because it's a lot slower than local vars.

    Likes: antix

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • NinjadoodleNinjadoodle Member
    edited October 2017
    @totebo - I think that in my case it should be ok since I'm only working with a handful of sprites per stage (warioware style levels) - what do you think / how slow are we talking?

    It's a choice between having to retype the same code for a lot of levels VS using a global function and self, and not having to fix stuff across multiple files every time I make a change.

    Let me know what you think :)

    Thanks again!

  • Use self! I use it all the time, it's kind of unavoidable if you go object oriented. So long as you know about the trade-off, you're good. For loops you can pop the self var in a local variable before you use it, so you only have to access it once
    My Gideros games: www.totebo.com
    +1 -1 (+3 / -0 )Share on Facebook
  • Ok cool, that makes sence - thanks for the tips. I’m sure there are a lot of optimisations I’ll pick up from you guys over time :)
Sign In or Register to comment.