Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Compiling process order — Gideros Forum

Compiling process order

ashlyn_scottashlyn_scott Member
edited August 2012 in General questions
hi everyone!
In a Gideros code ,from which line to which line does compiling process start? And then how does it continue ?
For example this :
 
-- example 1
ClassA = Core.class(EventDispatcher)
ClassB = Core.class(EventDispatcher)
 
function ClassA:funcA(event)
    print("funcA", self, event:getType(), event:getTarget())
end
 
local a = ClassA.new()
local b = ClassB.new()
 
b:addEventListener("myevent", a.funcA, a)   -- when b dispatches an "myevent" event,
                                            -- a.funcA will be called with 'a'
                                            -- as first parameter
 
b:dispatchEvent(Event.new("myevent"))       -- will print "funcA"
 
 
-- example 2
Ball = Core.class(Sprite)
 
function Ball:onEnterFrame()
    self:setX(self:getX() + 1)
end
 
ball = Ball.new()
ball:addEventListener(Event.ENTER_FRAME, ball.onEnterFrame, ball)
which line does the compiler read firstly?

Comments

  • Not quite sure what you mean, but...
    My understanding is it starts at the beginning and continues in a linear fashion until it reaches the end, this is why you must declare everything before it's used (or else lua assumes it's a global and if not found there is nil)
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • ashlyn_scottashlyn_scott Member
    edited August 2012
    Not quite sure what you mean, but...
    you must declare everything before it's used (or else lua assumes it's a global and if not found there is nil)
    1st question: in which condition lua assumes it's a global ?I didn't understand.
    Moreover;
    function ClassA:funcA(event)   --it's a function definition.When the code comes here 
    --firstly , We don't make below print work .Am i right ?(2nd question)
        print("funcA", self, event:getType(), event:getTarget())
    end          
     
    3.question:
    b:addEventListener("myevent", a.funcA, a)   --here , does addEventListener function
    --call second  parameter? Namely; where does function calling becomes?(this is a general question) How many different ways do we call a function? for example ;one way is func()
    the other is addEventListener(event.Compile, func) (I'm not sure whether this is right)
    is there any other options?
  • Still not 100% sure what your asking and why?
    Are you trying to learn more about lua or do you have a specific instance where something isn't working as you'd assumed?
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • MellsMells Guru
    edited August 2012
    I try to answer (but I am not sure I get what you mean) :

    A function has to be declared first before you use it (you can also use a forward declaration). If the function is not called, it's not executed.
    So "funcA" will never be printed if the method ClassA:funcA is never called, or triggered by an event.
    b:addEventListener("myevent", a.funcA, a)   --here , does addEventListener function
    --call second  parameter? Namely; where does function calling becomes?(this is a general question)
    addEventListener doesn't call the function if it's not triggered by an event.
    With addEventListener you are adding a 'listener' to "myevent".
    If "myevent" happens, a.funcA is called. In the meantime, nothing happens.

    You can call your function :
    -- directly in your code 
    a.funcA()
     
    -- or by dispatching an event (Remember to add an event listener before)
    b:addEventListener("myevent", a.funcA, a)
    b:dispatchEvent(Event.new("myevent"))       -- will print "funcA"
    The documentation in the reference manual is here.


    If you asked for something else, please be more specific.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • ashlyn_scottashlyn_scott Member
    edited August 2012
    you understand me correctly. But the below code works perfeclt although there is no function calling like funcA() and no dispatchEvent like b:dispatchEvent(Event.new("myevent"))
    How is it working then ?
     
    local loader = UrlLoader.new("<a href="http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png&quot" rel="nofollow">http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png&quot</a><img class="emoji" src="http://forum.giderosmobile.com/resources/emoji/wink.png" title=";)" alt=";)" height="20" />
     
    local function onComplete(event)
        local out = io.open("|D|image.png", "wb")
        out:write(event.data)
        out:close()
     
        local b = Bitmap.new(Texture.new("|D|image.png"))
        stage:addChild(b)
    end
     
    local function onError()
        print("error")
    end
     
    local function onProgress(event)
        print("progress: " .. event.bytesLoaded .. " of " .. event.bytesTotal)
    end
     
    loader:addEventListener(Event.COMPLETE, onComplete)
    loader:addEventListener(Event.ERROR, onError)
    loader:addEventListener(Event.PROGRESS, onProgress)
  • @ashlyn_scott because UrlLoader class starts working on creation, there is no need to call start or something to make it work. It automatically starts on .new call.
    And all the events are dispatched inside UrlLoader class - internally. That's why they does not appear in the code.

    You can create you own class i separate file which dispatches events. And then use it in another file (creating class instance, etc) and there will be no code to dispatch event, because you are doing it inside your class in another file.

    Hope that clears something ;)
  • ashlyn_scottashlyn_scott Member
    edited August 2012
    you understand me correctly. But the below code works perfeclt although there is no function calling like funcA() and no dispatchEvent like b:dispatchEvent(Event.new("myevent"))
    How is it working then ?
     
    1.local loader = UrlLoader.new("<a href="http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png&quot" rel="nofollow">http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png&quot</a><img class="emoji" src="http://forum.giderosmobile.com/resources/emoji/wink.png" title=";)" alt=";)" height="20" />
     
    2.local function onComplete(event)
    3.    local out = io.open("|D|image.png", "wb")
    4.    out:write(event.data)
    5.    out:close()
     
    7.    local b = Bitmap.new(Texture.new("|D|image.png"))
    8    stage:addChild(b)
    9.end
     
    10.local function onError()
     11.   print("error")
    12.end
     
    local function onProgress(event)
        print("progress: " .. event.bytesLoaded .. " of " .. event.bytesTotal)
    end
     
    18.loader:addEventListener(Event.COMPLETE, onComplete)
    19.loader:addEventListener(Event.ERROR, onError)
    20.loader:addEventListener(Event.PROGRESS, onProgress)
    Could you please explain via line numbers ?
  • The lua "INTERPRETER" starts at line 1. it declares a local variable called loader - which is then set to the object (table) reference that is returned from the UrlLoader class (which internally calls the UrlLoader:init() function)

    Given that a URL is passed via the .new() function - internally the UrlLoader class will actually start the download process working, I'm not 100% sure of the exact implementation as to how it works but you can think of it as running on a separate thread parallel to what's currently happening if you like.

    Lines 2 -> 17 then tell the INTERPRETER to define functions so that later on they can be called.

    lines 18-20 then adds the references to the above functions to the loader table (or object instance if you prefer) so that once the UrlLoader instance that is loader has finished (or develops an error) it can tell you about it.

    Remember the actual process of doing the http request etc for UrlLoader will take a specific amount of time - in terms of computing an absolute age compared to the time it takes lua to process and action the rest of the code, so the chances of UrlLoader completing BEFORE the event listeners are added is tiny.

    Does that help?
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
Sign In or Register to comment.