Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Use UrlLoader within the function — Gideros Forum

Use UrlLoader within the function

turker2000turker2000 Member
edited May 2019 in General questions
Hello to everyone,

The file "loader.lua" is below;
function get(address)
 
	local SOURCE
	local loader = UrlLoader.new(address)
 
	if loader then
 
		loader:addEventListener(Event.COMPLETE, function(event)
			--print(event.data)
			SOURCE = event.data
			return SOURCE
		end)
 
		loader:addEventListener(Event.ERROR, function(event)
			print("error")
		end)
 
		loader:addEventListener(Event.PROGRESS, function(event)
			print("progress: " .. event.bytesLoaded .. " of " .. event.bytesTotal)
		end)
 
	end
 
	return SOURCE
 
end
Below is the main.lua file;
print(get("any web address"))
Screen output is;
Uploading finished.
nil
progress: 320 of 320
progress: 320 of 320
Why does the function "nil" return a value? Where am I making a mistake. Can you help me fix the code I wrote?

Likes: MoKaLux

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • Apollo14Apollo14 Member
    edited May 2019
    Hi guys! I'm also curious about urlloader returning 'nil'. But I have to go to sleep right now, can't debug it.

    I've just tested Gideros' example, it works as it should.
    Maybe there's something there that you missed?
    This code is working perfectly well, try it:
    local info = TextField.new(nil, "loading...")
    info:setPosition(10, 10)
    stage:addChild(info)
     
    local function onComplete(event)
    	info:setText("done")
     
    	local out = io.open("|D|image.png", "wb")
        out:write(event.data)
        out:close()
     
        local b = Bitmap.new(Texture.new("|D|image.png"))
    	b:setAnchorPoint(0.5, 0.5)
    	b:setPosition(160, 240)
        stage:addChild(b)
    end
     
    local function onError()
    	info:setText("error")
    end
     
    local function onProgress(event)
        info:setText("progress: " .. event.bytesLoaded .. " of " .. event.bytesTotal)
    end
     
    --(link is broken by forum engine)
    local loader = UrlLoader.new("<a href="http://giderosmobile.com/giderosmobile.png" rel="nofollow">http://giderosmobile.com/giderosmobile.png</a>")
     
    loader:addEventListener(Event.COMPLETE, onComplete)
    loader:addEventListener(Event.ERROR, onError)
    loader:addEventListener(Event.PROGRESS, onProgress)

    Likes: MoKaLux

    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
    +1 -1 (+1 / -0 )Share on Facebook
  • Apollo14Apollo14 Member
    edited May 2019
    uh, maybe it's because we're trying to print 'event.data' before it's actually loaded, we should set timer delay (or any other workaround that suits your particular goal)

    Likes: MoKaLux

    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
    +1 -1 (+1 / -0 )Share on Facebook
  • No problem in UrlLoader operation. The problem occurs when you use UrlLoader in a function. The function returns the result before "event.data" occurs. Therefore, the return value is "nil". I know it, but I can't find the solution. Using "timer delay" cannot be a solution because the waiting time is very variable depending on the speed of the Internet or the size of the loaded page. It doesn't make sense to use the biggest value and always expect the longest time.
    I can't be the only one to have this problem. Does anyone solve this problem?
  • @turker2000 what exactly are you downloading with UrlLoader? Text data or JPG/PNG?
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • I communicate with the web service I have prepared. Incoming-outgoing data is encrypted-decrypted with aes128.
  • I don't know, maybe @hgy29 knows if it's a bug or a normally expected behavior.

    I'd choose some simple workaround via Event or something.
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • hgy29hgy29 Maintainer
    What you see is perfectly normal as @Apollo14 first pointed out: UrlLoader is asynchronous, you can’t return its data from a function without a way to wait for Complete event result beforehand. If you want a kind of synchronous behavior, wrap your function into an asyncCall call and use Core.yield() waiting for the Event result before returning
  • turker2000turker2000 Member
    edited May 2019
    I didn't find any examples in the Gidos Reference pages about Core.asyncCall. In the forum I was only able to find two pages you wrote. But it didn't help me understand the matter. I think I'll try to solve it using a timer. Thanks anyway.
  • Apollo14Apollo14 Member
    edited June 2019
    @turker2000 you can check examples of using Core.asyncCall in
    C:\Program Files (x86)\Gideros\Examples\Graphics\AsyncTasking
    C:\Program Files (x86)\Gideros\Examples\Graphics\AsyncTextureLoading

    And in forum discussions:
    https://www.google.com/search?q=site:forum.giderosmobile.com+"Core.asyncCall"&amp;rlz=1C1HLDY_enKG812KG812&amp;oq=site:forum.giderosmobile.com+"Core.asyncCall"&amp;aqs=chrome..69i57j69i58.5286j0j7&amp;sourceid=chrome&amp;ie=UTF-8

    Core.asyncCall is totally cool and awesome feature, definitely worth checking!
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
Sign In or Register to comment.