Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Function questions — Gideros Forum

Function questions

NinjadoodleNinjadoodle Member
edited June 2016 in General questions
Hi guys

I've noticed that if I'm declaring a function inside a scene's init(), the function has to be setup before I can use it ...

something like this will work ...
local function printHello()
    print("Hello")
end
 
printHello()
but this doesn't ...
printHello()
 
local function printHello()
    print("Hello")
end
this also doesn't work
local printHello
 
printHello()
 
function printHello()
    print("Hello")
end
Is there a way to declare a function so it is available at any time?

Comments

  • talistalis Guru
    edited June 2016
    Define it in init.lua and use where ever you want.
    Is there any specific reason for you to define it in scene init?

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • piepie Member
    edited June 2016
    Hi, lua code is processed in the order as it is written (I believe that is the reason for the "code dependency" section in the resources context menu - you can load specific files first).
    You always need to declare a function before calling it.

    To make a function available at any time (but you need to have declared it already) you can make it global; or define it as a property/method of an object, and use it "inside" it, or send it as a constructor/method parameter.

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • init.lua is a special file which will be always processed first in any case. (even earlier than main.lua)

    Likes: Ninjadoodle

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

    Thanks for the feedback :)

    @talis - the main reason for defining it inside init(), is that I have 16 levels (all different - basically mini games) - so it mainly organisational purposes. Everything for a level sits inside it's own lua file :)

    Thanks again for the help!
  • keszeghkeszegh Member
    edited June 2016
    maybe it would be better to have an object/class in each file and the functions would be methods of them. this is best for making it cleanly organized. e.g.:
    Level1=Core.class(Sprite)
     
    function Level1:init()
        self:printHello()
    end
     
    function Level1:printHello()
         print("Hello")
    end
  • talistalis Guru
    edited June 2016
    I have never been an organized coder , maybe it is because i started with basic and GOTO commands :D
    Anyway hope you will solve it, by the way @kesgezh 's way seems to be the solution.I just tried now and it is working.
  • NinjadoodleNinjadoodle Member
    edited June 2016
    Hi @keszegh - Ahhh! That's exactly what I was looking for, I just wasn't sure how to set it up!!

    Thank you very much for your help!

    @talis - haha c64 was first computer (and still my favourite). I'm trying to get into good habits, as I know the pain it causes me to come back to a game and have to dig through code I rushed through :)
  • keszeghkeszegh Member
    edited June 2016
    following the way i suggested makes it also very easy to release a level from memory, you just nil the object you made and then all of the functions etc. will be garbage collected. e.g.:
    level1=Level1.new()
     
    ...
     
    level1=nil
  • Hi @keszegh

    Awesome, thanks again :) Just one more thing if you have time ...

    How would you set up an onTouch function in this format?
    function Level01:onTouch(target, event)
     
    	if target:hitTestPoint(event.x, event.y) then
    I can't seem to access the event variable.
  • talistalis Guru
    edited June 2016
    In the init scene function are you registering the onTouch event.
    self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown,self)
    And then:
    function Level01:onMouseDown(event)
    	print(event.x)
    end
    I used MouseDown event and it seems working this way.

    Total code
    Level01=Core.class(Sprite)
     
    function Level01:init()
        self:printHello()
        self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown,self)
    end
     
    function Level01:printHello()
         print("Hello")
    end
     
    function Level01:onMouseDown(event)
    	print(event.x)
    end

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • NinjadoodleNinjadoodle Member
    edited June 2016
    Hmmm, I can print out the event.x, but when using it with a button and can't seem t get it working.

    inside init ...
    self.buttonEye:addEventListener(Event.MOUSE_DOWN, self.onTouch, self.buttonEye)
    then ...
    function I06_l13:onTouch(target, event)
    	if target:hitTestPoint(event.x, event.y) then
    		print(event.x)
    	end
    end
    The same function works when declared inside init().
  • Why you are adding an event listener to Button?
    You can use button's own event listener already. Is there any more spesific reaspon to this way?
    Of course i am supposing you are using Button.lua
  • NinjadoodleNinjadoodle Member
    edited June 2016
    Sorry when I say button, I just mean a touchable sprite. My level is basically and "Point & Click" mini-game, and when object's are touched - things happen.

    Inside init() this works no problem, but I can't seem to get it working this "new" way.

    Also, I'm not using button.lua - in the particular example I posted, the sprite is called "buttonEye", but that's just a coincidence. In my level, I have touchable objects that do stuff on interaction. They might animate, tween - increment a variable etc.
  • in your code @Ninjadoodle it's problematic that you send self.buttonEye but then for your function it will be received as self (for a function that belongs to some class the first received property is always 'self' - you need to send it, which happens automatically if you call it like myClass:myFunction() or explicitly you can do it if you call it like myClass.myFunction(myClass) or myClass.myFunction(self) inside another function belonging to myClass. note the difference ('.' instead of ':').

    this is explained in several forum topics and most probably in the oop example video.
    see e.g.
    http://giderosmobile.com/forum/discussion/5924/problem-in-inheritence-and-using-self-keyword/p1

    you should rather use a button class for buttons, button class:
    https://github.com/gideros/Button , and then
    self.myButton:addEventListener("click",self.processButton,self)
    will work as expected.

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • keszeghkeszegh Member
    edited June 2016
    one way to do what you want is:
    self.buttonEye:addEventListener(Event.MOUSE_DOWN, function (self,event) self:onTouch(self.buttonEye, event) end, self)
    (i'm not sure about what to put after 'function(' but hopefully 'self,event' is ok, test it.

    Likes: Ninjadoodle, talis

    +1 -1 (+2 / -0 )Share on Facebook
  • keszeghkeszegh Member
    edited June 2016
    basically with addEventListener you can send only one variable, self. if you want to send more than one then do it like i wrote in my previous comment.

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • NinjadoodleNinjadoodle Member
    edited June 2016
    Thank you, it's confusing, but I understand what you are saying.

    I do really need to use a custom function and not the "button class", so thank you very much for your last example.

    Do you think that calling the function this way ...
    self.buttonEye:addEventListener(Event.MOUSE_DOWN, function () self:onTouch(self.buttonEye, event) end, self)
    ... is a little "hackish" and maybe I should just declare my function inside init()

    I'm just worried about problems popping up when I get deeper.

    PS. You guys are awesome, I really appreciate you help!!
  • By the way there is one more discussion about passing variables to events. Old discussion but still valid answers:)
    http://giderosmobile.com/forum/discussion/926/how-to-pass-variables-through-event-listeners-attempt-to-index-a-nil-value/p1

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • keszeghkeszegh Member
    edited June 2016 Accepted Answer
    it's not that hackish, i've seen this nested function(...) inside an eventlistener many times on the forum from other people.

    see e.g. http://giderosmobile.com/forum/discussion/926/how-to-pass-variables-through-event-listeners-attempt-to-index-a-nil-value/p1


    on the other hand a separate class/object for all your touchable items will make your code better and it will be easier to add new functions to it. also you can use it in any of your levels etc. just change the buttons class for your needs.

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • Thanks again guys ... you ROCK!
Sign In or Register to comment.