Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Scene Manager and function calls. self.<variable> is nil but shouldn't be! — Gideros Forum

Scene Manager and function calls. self.<variable> is nil but shouldn't be!

joelghilljoelghill Member
edited September 2012 in General questions
I'm trying to display scores retrieved from an online leaderboard. The scoreboard is a scene within the scene manager. The relevant code snippets are below:
 scores = Core.class(Sprite)
 
 
function scores:init()
--setup format
self.online = online
self.scores = false -- a boolean value.  false when score boards not retreived
self.count = 0 -- arbitrary counter
self.y = 0-- important variable I need later
 
self.scoreBoard = Sprite.new()
 
 
 self:addEventListener("enterEnd", self.onEnterEnd, self)
 self:addEventListener("exitBegin", self.onExitBegin, self)
 self:addEventListener("enterBegin", self.onEnterBegin, self)
 self:addEventListener("exitEnd", self.onExitEnd, self)
end
Early in scene I create the class and declare variables. Notice self.y is set to zero and self.scoreBoard is a Sprite

Later, in
function scores:onEnterBegin()
I call this function:
function scores:getScores(gm, leaderboard, a) --
 
	local leader = leaderboard -- test board
	local format = "json" -- xml, json
	local auth = a
 
 
	local method = "UrlLoader.GET"
	local headers = gm.headers
	local body = "auth_token="..auth
	local url = "<a href="https://api.gameminion.com/leaderboards/"..leader.."/scores."..format.."?"..body" rel="nofollow">https://api.gameminion.com/leaderboards/"..leader.."/scores."..format.."?"..body</a>
 
	print(body)
 
	self.info = TextField.new(nil, "loading...")
	self.info:setPosition(380, 240)
	self.info:setScale(3)
	--self:addChild(self.info)
 
	function self.onComplete(event)
		--self:removeChild(info)
		print("data retrieved")
		local t = Json.Decode(event.data)
		print(t[1].username)
		--> joelghill
		-- ADD SCORES TO SCREEN
		print(#t)
			for i = 1, #t, 1 do
 
					--adds a new textfield as a child
					local newText = TextField.new(nil, string.format("%i.", i)) --, self.highScore:getName(i)
					newText:setScale(3)
					newText:setTextColor(0xffffff)
					newText:setY(y)
					newText:setX(10)
					--y = y + newText:getHeight() + 10
					self.scores.scoreBoard:addChild(newText)
 
					newText = TextField.new(nil, string.format( "%.1fm", t[i].value))
					newText:setScale(3)
					newText:setTextColor(0xffffff)
					newText:setY(self.y)
					newText:setX(200 + self.x - newText:getWidth() )
					--y = y + newText:getHeight() + 10
					self.scoreBoard:addChild(newText)
 
					newText = TextField.new(nil, string.format("%s",t[i].username ))
					newText:setScale(3)
					newText:setTextColor(0xffffff)
					newText:setY(scores.y)
					newText:setX(600 - newText:getWidth() )
					scores.y = scores.y  + 40
					--print(y)
					self.scoreBoard:addChild(newText)
					print(self.scoreBoard:getHeight())
			end
 
		end
 
	function self.onError()
		--info:setText("error")
		print("error in getting scoress")
		--add error message to leaderboard
		end
 
	function self.onProgress(event)
		print("progress: " .. event.bytesLoaded .. " of " .. event.bytesTotal)
		end
 
self.loader = UrlLoader.new(url, UrlLoader.GET, headers, body) -- , body
 
self.loader:addEventListener(Event.COMPLETE, self.onComplete)
self.loader:addEventListener(Event.ERROR, self.onError)
self.loader:addEventListener(Event.PROGRESS, self.onProgress)
 
 
end
I get an error indicating that self.y is a nil value (the one used in the for-loop) , and if I replace that with an actual number, I get an error saying that self.scoreBoard is nil (also the one within the for-loop) even though I have it declared previously.

I believe this has something to do with the naming conventions I am using. I have no idea on how to go about using "self.y" within a function that is within a function, that is within a scene :S

Where did I go wrong here?

Comments

  • Update:

    This entire block of code now magically works. I'm not exactly sure what I did to make it work though :S (I think I changed a variable from scores. to self.)

    I suppose if anyone has anything to add on the subject, feel free to bring it up, but I've solved my problem... somehow.
  • zaniarzaniar Member
    Accepted Answer
    self.scores.scoreBoard:addChild(newText)
    should be
    self.scoreBoard:addChild(newText)
    ,
    newText:setY(scores.y)
    should be
    newText:setY(self.y)
    and
    scores.y = scores.y  + 40
    should be
    self.y = self.y  + 40
    you are working inside "scores" class. So if you want to refer to this class' instance, use "self" instead of "scores". "scores" is global variable
Sign In or Register to comment.