Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to "delay" the return of a function when its return value depends on an event ? — Gideros Forum

How to "delay" the return of a function when its return value depends on an event ?

jalmincejalmince Member
edited June 2014 in General questions
My function here below uses UrlLoader, but returns before the Event.complete strikes....
function GetHighScores()
 
	local url = "<a href="http://www.jalmince-lab.com/BallTrip/high_scores.php&quot" rel="nofollow">http://www.jalmince-lab.com/BallTrip/high_scores.php&quot</a>;
	local headers = { ["Content-Type"] = "application/x-www-form-urlencoded"}	
 
	local result = "" 
 
	local 	function onComplete(event)
				result = event.data
                        end			
	local loader = UrlLoader.new(url, UrlLoader.POST )
	loader:addEventListener(Event.COMPLETE, onComplete)	
 
	--- Need a "delay" here !!
	return(result)
end
Tried with a dummy loop " while not finished do end" before the return, setting finished=true in the event handler. But that freezes the player.

thanks for your help !
Jp

Comments

  • Hi @jalmince,
    You should call drawGUI function inside onComplete function
    local 	function onComplete(event)
    				result = event.data
    drawGUI(result)
     
     
    end
    And then in code, you only need to call : GetHighScores()
    Coming soon
  • thanks Vitalitymobile, but It doesn' t work.
  • Hi @jalmince, maybe you can refer to Weather example (in Gideros setup folder ) to find out more...
    onComplete is callback function, so when url load done, it's return result, and then you can pass it to other method to execute next ( that why you don't have to return in GetHighScores function)
    Coming soon
  • thanks Vitality, but that is not the answer to my issue.
    The weather example processes the xml result INSIDE the onComplete function.
    Imagine now that you want to create a function, say "get_weather()", that returns the xml from yahoo, and that you want to call that fonction from another function; for example:
    ...
    xml = get_weather()
    "process" xml
    ....
    How do you write the function to return the xml ONLY when the event has been processed ? yor function will look like that :
    function get_weather()
    ....
       local xml
    ...  
       local function onComplete(event)	
          xml = XmlParser:ParseXmlText(event.data)
       end
    ...  
    local loader = UrlLoader.new("<a href="http://xml.weather.yahoo.com/forecastrss/USCA0987.xml&quot" rel="nofollow">http://xml.weather.yahoo.com/forecastrss/USCA0987.xml&quot</a><img class="emoji" src="http://forum.gideros.rocks/resources/emoji/wink.png" title=";)" alt=";)" height="20" />
    loader:addEventListener(Event.COMPLETE, onComplete)
    ...
    return xml
    and the bottom return is always reached before the event is processed, returning nil.
  • I think loader and onComplete is async callback. If you really want get result when the event has been processed, you can show a loading dialog in the end of get_weather function and then in onComplete function, dispose dialog and process result?

    Something like this will work
     
    local function get_weather()
     
    ---- detail
    end
     
    local function process(xml)
     
    ---detail
     
    end
     
    local xml  = get_weather();
    process(xml)
     
    --->> change to async callback style
     
    local function process(xml)
     
    ---detail
     
    end
     
    local function get_weather()
     
    ---- detail
      local function onComplete(event)	
         local xml = event.result;      
        process(xml)
       end
    end
     
    --- main entry
    get_weather(); --- no need return here, when urlloader query data done, it is automatic call process
    Coming soon
Sign In or Register to comment.