Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Nested loading — Gideros Forum

Nested loading

loves_oiloves_oi Member
edited September 2012 in General questions
I'M using nested loading function and it confuses me . It worked for one loading .Then i put a loading function for putting image to screen. It should load both the text and picture and they should be visible on screen. But loading of picture doesn't work.Why doesn't it work?
 
results = Core.class(Sprite)
function results:init(params)
 
	local screen = Bitmap.new(Texture.new("resources/images/opt_back.png"))
	self:addChild(screen)
	screen:setPosition(0,0)	
	p = 2
	local a = 1
	mySize = table.getn(params)
	k=1
	tit = ""
	q = ""
 
 
 
	function onComplete2(p, event)
				q = event.data
 
				the[p] = TextField.new(nil, "hello") the[p]:setPosition(100,50 +a*50) the[p]:setTextColor(0x007B25) the[p]:setScaleX(3)the[p]:setScaleY(3)
 
				------------------------------------------
				if ( tit == "xx") 
				then
 
					local picLoader = UrlLoader.new("<a href="http://localhost/bird.png&quot" rel="nofollow">http://localhost/bird.png&quot</a><img class="emoji" src="http://forum.gideros.rocks/resources/emoji/wink.png" title=";)" alt=";)" height="20" />  
 
					local function onComplete3(event)
						local out = io.open("|D|image.png", "wb")
						out:write(event.data)
						out:close()
 
						local b = Bitmap.new(Texture.new("|D|image.png"))
						b:setPosition(10,10)	
						stage:addChild(b)   -----HERE IS NOT OKAY.PNG FILE DOESN'T LOAD
					end
 
					picLoader:addEventListener(Event.COMPLETE, onComplete3)
					picLoader:addEventListener(Event.ERROR, onError)
					picLoader:addEventListener(Event.PROGRESS, onProgress)
					---------------------------------------------------------------
 
				end
				the[p]:setText(params[p+1] .. q)  
				print("q : " .. q)
 
				stage:addChild(the[p])        -- HERE IS OKAY
				a = a + 1
 
	end
 
	while(k < mySize+1) do
		if (params[k] ==true) 					
		then			
				p=k
				local loader2 = UrlLoader.new(params[k+2])
				loader2:addEventListener(Event.COMPLETE, onComplete2, p)
		end
				k = k+3
	end
end
Thanks in advance

Comments

  • techdojotechdojo Guru
    edited September 2012
    The use of closures and nested functions is NOT easy to get your head round - especially for the beginning lua programmer. To be honest in this case there is NO need to add that level of complexity, also your actually wasting a LOT more memory and resources as each time round the loop your actually CREATING a whole new set of functions and retaining references to upvalues.

    Remember K.I.S.S.

    Likes: loves_oi

    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
    +1 -1 (+1 / -0 )Share on Facebook
  • loves_oiloves_oi Member
    edited September 2012
    Actually,the code doesn't give any error and it works. But picture isn't visible bcs it isn't loaded.Only text is visible.
    If i don't not use nested functions , what else can i do to perform it? Any idea?
    Thanks a lot
  • IF the code worked then you wouldn't have posted with a problem - it might work the first time but it's on the subsequent runs your getting an issue.

    Move ALL of your onComplete functions OUTSIDE of (preferably before) the :init function and make them local like so
    local function onComplete2(self,event)
             ....
    end
    Then change your addEventListener to something like
        loader2:addEventListener(Event.COMPLETE, onComplete2, self)
    NOTE : the self at the end - then when onComplete is called the first parameter (self) will actually reference the correct instance of results - you can prove this by "print(self)" in both cases and you'll get a "table : xxxxxxxx" where xxxxxxxx is the address of the table in memory and will be the same in both functions.

    You can then freely (and safely) access any variables defined in results:init() and you can be sure your looking at the correct instance.
    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
  • loves_oiloves_oi Member
    edited September 2012
    Ok. I did it and it worked perfectly. Thanks.
    local picLoader = UrlLoader.new(URL)
    although i send two different values of URL , it will load the same URL for them.
    But when i select two checkbox ,
    Moskow MoskowPicture
    Madrid MadridPicture
    in screen , it will look like as:
    Moskow MoskowPicture
    Madrid MoskowPicture
    in screen.
    When i run the same code without changing anything ,it may look like :
    Moskow MadridPicture
    Madrid MadridPicture
    However , in all attempts , the right url of the Moskow and Madrid comes .
    This is the code:
     
    results = Core.class(Sprite)
    function results:init(params)
     
    	local screen = Bitmap.new(Texture.new("resources/images/opt_back.png"))
    	self:addChild(screen)
    	screen:setPosition(0,0)
     
    	the = {}
    	p = 2
    	local a = 1
    	mySize = table.getn(params)
    	k=1
    	tit = ""
    	q = ""
     
    	local function onComplete3(p,event)
    						local out = io.open("|D|image.png", "wb")
    						out:write(event.data)
    						out:close()
     
    						local b = Bitmap.new(Texture.new("|D|image.png"))
    						b:setPosition(50,50+p*40)
    						stage:addChild(b)
    	end
     
     
    	function onComplete2(p, event)
    				q = event.data
     
    				the[p] = TextField.new(nil, "hello") the[p]:setPosition(100,50 +a*50) the[p]:setTextColor(0x007B25) the[p]:setScaleX(3)the[p]:setScaleY(3)
     
    				i=1
    				while (not(string.find(string.sub(q,i,i), "\t")))        -- take the tit 
    				do
    					tit = tit ..  string.sub(q,i,i)
    					i = i + 1
    				end
     
    				if (string.find(string.sub(q,i,i), "\t"))
    				then 
    					i = i + 1
    				end
    				print("tit : " .. tit)
     
     
    				if ( tit == "xx")
    				then
     
    					--TIT IS OBTAINED
     
    					print ("URL is :" .. tit) -------TİT is holding the url of the cities which will be loaded
     
    					local picLoader = UrlLoader.new(tit)  ---This line works wrongly. It doesn't load the second URL. It loads the same photo although this line works two times and values of tit are different. 
    					tit=""
    					picLoader:addEventListener(Event.COMPLETE, onComplete3,p)
    				end 
    				stage:addChild(the[p])
    				a = a + 1
    	end
     
    	while(k < mySize+1) do
    		if (params[k] ==true) 					
    		then			
    				p=k
    				local loader2 = UrlLoader.new(params[k+2])
    				loader2:addEventListener(Event.COMPLETE, onComplete2, p)
    		end
    				k = k+3
    	end
    end
    output is :
    URL is :http://localhost/moscow.png
    URL is :http://localhost/madrid.png

    However , in all attempts , when i checked moscow and madrid ,
    in screen , it will look like as:
    Moskow MoskowPicture
    Madrid MoskowPicture

    When i run the same code without changing anything ,it may look like :
    Moskow MadridPicture
    Madrid MadridPicture

  • @loves_oi: I'm rather confused looking at the code in this conversation. Your response to @techdojo was you followed his advice, but the code you posted does not remove the nested functions from the init functions at all. I would also recommended better variable names for reading your code because it may become hard to maintain your code as it grows. It's great to use short variables when you're just using them quick and tossing them, but to persist so many shorthand variables can make it pretty hard to read the code.

    There's also some things, I'm not sure why you would do the way you're doing and I wonder if maybe you should look over some general Lua material (I've used http://www.lua.org/pil/index.html quite a bit). I'm rather new to some of this myself and I realize there can be a lot to take in.

    It's really hard to narrow down what's going on with what you've provided and some examples of what I feel like might be oddities are:

    For instance your comment here:
    ---This line works wrongly. It doesn't load the second URL. It loads the same photo although this line works two times and values of tit are different.
    This is inside an if statement checking tit=="xx", so your code block should be ensuring the value is the same.

    Your 'the' table will have a lot of random holes because it looks like the parameter your passing is every 3rd parameter, but you're using the index from the params to pass into your 'the' table. (ie: you will have values at the[1], the[4] etc.)

    Likes: techdojo

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.