Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
More Errors With The Code... -_- — Gideros Forum

More Errors With The Code... -_-

MinstrelMinstrel Member
edited June 2015 in General questions
Okay so I've been helped overcome a TextureBase problem and one other problem. Now We have a third. I decided I'd make a new post for this question as my question on my previous post had been answered.
So here's the error:

main.lua:65: attempt to index global 'Pool' (a nil value)
stack traceback:
main.lua:65: in main chunk

Again here is my full code:


local sound = Sound.new("MakoBeamNCSGiderosCompRemix(-32Db).wav")
local channel = sound:play()


application:setBackgroundColor(0x0)

width = application:getContentWidth()

local username = ""

local textInputDialog = TextInputDialog.new("Player1",
"Enter Name Player1", username, "Cancel", "Save")

backgroundtexture = Texture.new("BGComp.png")
backgroundtexture = Bitmap.new(backgroundtexture)
stage:addChild(backgroundtexture)
backgroundtexture:setAnchorPoint(0.5,0.5)
backgroundtexture:setRotation(90)
backgroundtexture:setPosition(239,160)
backgroundtexture:setScaleX(1.75)
backgroundtexture:setScaleY(1.75)

local retrofont = TTFont.new("8-BIT WONDER.TTF", 20, true)
local text = TextField.new(retrofont,username)
text:setPosition(0, 20)
stage:addChild(text)
text:setTextColor(0xfffffff)
text:setScale(1.0)
text:setPosition(10,20)

bullet = Texture.new("bullet.png")
bullet = Bitmap.new(bullet)
stage:addChild(bullet)
bullet:setScale(0.3)
bullet:setPosition(0,0)
bullet:setRotation(-90)

playership1 = Bitmap.new(Texture.new("ship.png"))
stage:addChild(playership1)
playership1:setAnchorPoint(0.5,0.5)
playership1:setPosition(245,300)
playership1:setScale(1.0)
--playership1:setColorTransform(0.1,0.9,0.3)

local function onComplete(e)
--if e.buttonIndex then
username = e.text
text:setText(username)
--end
end

textInputDialog:addEventListener(Event.COMPLETE, onComplete)
textInputDialog:show()
left = false
right = false

shipX=width/2
playerDir=0
bDir=0
by= 1

bullets = {}
counter = 1

bpool = Pool.new()

function Pool:init()
self.pool = {}
end

function Pool:createObject()
local b

if #self.pool > 0 then
b = table.remove(self.pool)
else
b = Bitmap.new(Texture.new("bullet.png", true))
b:setScale(0.3)
b:setPosition(0,0)
b:setRotation(-90)
end

return b
end

function Pool:destroyObject(b)
b:removeFromParent()
table.insert(#self.pool, b)
end


function gameloop(e)
shipX=shipX + (playerDir* 5)

if shipX<20 then
shipX = 20
elseif
shipX>(width-20) then
shipX = width - 20
end

playership1:setX(shipX)

by=by+(bDir*5)
bullet:setY(by)

end

stage:addEventListener(Event.ENTER_FRAME,gameloop)

stage:addEventListener(Event.KEY_DOWN,function(e)
if e.keyCode==KeyCode.LEFT then
left=true
playerDir=-1
elseif e.keyCode==KeyCode.RIGHT then
right=true
playerDir=1
elseif e.keyCode==KeyCode.Z then

print("fire")
bx,by=playership1:getPosition()
bullets[counter] = bpool:createobject()
stage:addChild(bullets[counter])
bullets[counter]:setPosition(bx,by)
bDir=-1

counter = counter+1

end

end)

stage:addEventListener(Event.KEY_UP,function(e)
if e.keyCode==KeyCode.LEFT then
left=false
if right then
playerDir=1
else
playerDir=0
end

elseif e.keyCode==KeyCode.RIGHT then
right=false
if left then
playerDir=-1
else
playerDir=0
end
end
end)

Once again I hope we can overcome this issue and I hope it will be the last.
Thank you for your time.
(BTW I was told how to highlight the code but I'm doing something wrong because nothings happening)

Comments

  • piepie Member
    @Minstrel there is a shortcut to code formatting: the "C" you can see above the text input area (the 5th label button between S and image ).

    you missed somewhere the Pool class definition
    Pool = Core.class(Sprite)
    and you should move everything that is rrelated to Pool class at the beginning of your file:
    Pool = Core.class(Sprite)
     
    function Pool:init()
    self.pool = {}
    end
     
    function Pool:createObject()
    local b
     
    if #self.pool > 0 then
    b = table.remove(self.pool)
    else
    b = Bitmap.new(Texture.new("bullet.png", true))
    b:setScale(0.3)
    b:setPosition(0,0)
    b:setRotation(-90)
    end
     
    return b
    end
     
    function Pool:destroyObject(b)
    b:removeFromParent()
    table.insert(#self.pool, b)
    end
    Since lua files are processed from line 1 to end, if you call
    bpool = Pool.new()
    before loading Pool, you get the error: attempt to index global 'Pool' (a nil value)
    how does it know what Pool should be? :)
  • Ok so I've done what you said and I can now start the game (WooHoo)
    So now were back to my first problem... It's giving me an error when I press the fire key Z.
    But do not fear it's a different one so we're definitely getting there.

    The Error is:
    main.lua:122: attempt to call method 'createobject' (a nil value)
    stack traceback:
    	main.lua:122: in function <main.lua:111>
    and line 122 is:
    bullets[counter] = bpool:createobject()
    Once again thank you for your continued support help and time and I know we're getting there, I can feel it!
  • piepie Member
    @Minstrel sorry my mistake:

    there is no "createobject" method in Pool definition ( no Pool:createobject(), but Pool:createObject() ).

    it should be:
    bullets[counter] = bpool:createObject()  -- cAse SeNsitive

    the most important thing though is that you understand "why" :)

    with : sign you call a method from a class:

    bpool is an instance of Pool class, which has the method createObject(), which returns the bullet (creating a new one, or drawing it out from self.pool if it's already there ) .

    bullets[counter] = bpool:createObject()
    assign to bullets[counter] what is returned from Pool:createObject() method.
  • MinstrelMinstrel Member
    edited June 2015
    Ok thanks and no worries about mistake.
    It works and I can fire but... well you need to see for your self (fire button is Z press it and you'll know what I mean). This really is bizarre...

    Copy this code and add these files into a Gideros File:
    local sound = Sound.new("MakoBeamNCSGiderosCompRemix(-32Db).wav")
    local channel = sound:play()
     
    Pool = Core.class(playership1) 
     
    function Pool:init()
        self.pool = {}
    end
     
    function Pool:createObject()
        local b
     
        if #self.pool > 0 then
    		b = table.remove(self.pool)
        else
            b = Bitmap.new(Texture.new("bullet.png", true))
        b:setScale(0.3)
    	b:setPosition(0,0)
    	b:setRotation(-90)
    	end
     
        return b
    end
     
    function Pool:destroyObject(b)
        b:removeFromParent()
        table.insert(#self.pool, b)
    end
     
    application:setBackgroundColor(0x0)
     
    width = application:getContentWidth()
     
    local username = ""
     
    local textInputDialog = TextInputDialog.new("Player1", 
    	"Enter Name Player1", username, "Cancel", "Save")
     
    backgroundtexture = Texture.new("BGComp.png")
    backgroundtexture = Bitmap.new(backgroundtexture)
    stage:addChild(backgroundtexture)
    backgroundtexture:setAnchorPoint(0.5,0.5)
    backgroundtexture:setRotation(90)
    backgroundtexture:setPosition(239,160)
    backgroundtexture:setScaleX(1.75)
    backgroundtexture:setScaleY(1.75)
     
    local retrofont = TTFont.new("8-BIT WONDER.TTF", 20, true)
    local text = TextField.new(retrofont,username)
    text:setPosition(0, 20)
    stage:addChild(text)
    text:setTextColor(0xfffffff)
    text:setScale(1.0)
    text:setPosition(10,20)
     
    bullet = Texture.new("bullet.png")
    bullet = Bitmap.new(bullet)
    stage:addChild(bullet)
    bullet:setScale(0.3)
    bullet:setPosition(0,0)
    bullet:setRotation(-90)
     
    playership1 = Bitmap.new(Texture.new("ship.png"))
    stage:addChild(playership1)
    playership1:setAnchorPoint(0.5,0.5)
    playership1:setPosition(245,300)
    playership1:setScale(1.0)
    --playership1:setColorTransform(0.1,0.9,0.3)
     
    local function onComplete(e)
    	--if e.buttonIndex then
    		username = e.text
    		text:setText(username)
    	--end
    end
     
    textInputDialog:addEventListener(Event.COMPLETE, onComplete)
    textInputDialog:show()
    left = false
    right = false
     
    shipX=width/2
    playerDir=0
    bDir=0
    by= 1
     
    bullets = {}
    counter  = 1
     
    bpool = Pool.new()
     
    function gameloop(e)
    	shipX=shipX + (playerDir* 5)
     
    	if shipX<20 then
    		shipX = 20
    	elseif
    		shipX>(width-20) then
    		shipX = width - 20
    	end	
     
    		playership1:setX(shipX)
     
    	by=by+(bDir*5)
    	bullet:setY(by)
     
    end
     
    stage:addEventListener(Event.ENTER_FRAME,gameloop)
     
    stage:addEventListener(Event.KEY_DOWN,function(e)
    	if e.keyCode==KeyCode.LEFT then
    		left=true
    		playerDir=-1
    	elseif e.keyCode==KeyCode.RIGHT then
    		right=true
    		playerDir=1
    	elseif e.keyCode==KeyCode.Z then
     
    	print("fire")
    	bx,by=playership1:getPosition()
    	bullets[counter] = bpool:createObject()
    	stage:addChild(bullets[counter])
    	bullets[counter]:setPosition(bx,by)
    	bDir=-1
     
    	counter = counter+1
     
    	end
     
    end)
     
    stage:addEventListener(Event.KEY_UP,function(e)
    	if e.keyCode==KeyCode.LEFT then
    		left=false
    		if right then
    			playerDir=1
    		else
    			playerDir=0
    		end
     
    	elseif e.keyCode==KeyCode.RIGHT then
    		right=false
    		if left then
    			playerDir=-1
    		else
    			playerDir=0
    		end
    	end
    end)
    Btw a huge thanks for the lesson, very few people take time to answer this many posts from the same person, yet alone tell them why you do everything. Even SinisterSoft who helps me irl rarely does that.

    Huge thanks to everyone who has helped so far.
    BGComp.png
    183 x 275 - 66K
    bullet.png
    225 x 225 - 4K
    ship.png
    225 x 225 - 6K
  • What appears to have happened is there is multiple bullets but when fire key is pressed a bullet appears at playership1's position but doesn't move. while this is happening another bullet spawns on the left hand side of the screen and fires upwards as usual.
    I have run over the firing code
    stage:addEventListener(Event.KEY_DOWN,function(e)
    	if e.keyCode==KeyCode.LEFT then
    		left=true
    		playerDir=-1
    	elseif e.keyCode==KeyCode.RIGHT then
    		right=true
    		playerDir=1
    	elseif e.keyCode==KeyCode.Z then
     
    	print("fire")
    	bx,by=playership1:getPosition()
    	bullets[counter] = bpool:createObject()
    	stage:addChild(bullets[counter])
    	bullets[counter]:setPosition(bx,by)
    	bDir=-1
     
    	counter = counter+1
     
    	end
    and all seems to be correct (it is calling "bullet" to the playership in bullets[counter]:setPosition(bx,by)
    and is moving the bullet in bDir=-1). I would assume that it has something to do with the pool we created earlier but other than that I am clueless.
  • piepie Member
    Accepted Answer
    @Minstrel, since every question is related to the same piece of code it's better to keep everything in the same thread. :)

    there you go, I wrote comments here and there, feel free to ask what you don't get
    Pool = Core.class(Sprite) --this  means that Pool is a class that inherits from Core.class>Sprite
     
    function Pool:init()
        self.pool = {}
    end
     
    function Pool:createObject()
        local b
     
        if #self.pool > 0 then
    		b = table.remove(self.pool)
        else
            b = Bitmap.new(Texture.new("bullet.png", true))
        b:setScale(0.3)
    	b:setPosition(0,0)
    	b:setRotation(-90)
    	end
     
        return b
    end
     
    function Pool:destroyObject(b)
        b:removeFromParent()
        table.insert(self.pool, b) --I don't know why there was a lenght operator # here 
    end
     
    application:setBackgroundColor(0x0)
     
    width = application:getContentWidth()
     
    local username = ""
     
    local textInputDialog = TextInputDialog.new("Player1", 
    	"Enter Name Player1", username, "Cancel", "Save")
     
    backgroundtexture = Texture.new("BGComp.png")
    backgroundtexture = Bitmap.new(backgroundtexture)
    stage:addChild(backgroundtexture)
    backgroundtexture:setAnchorPoint(0.5,0.5)
    backgroundtexture:setRotation(90)
    backgroundtexture:setPosition(239,160)
    backgroundtexture:setScaleX(1.75)
    backgroundtexture:setScaleY(1.75)
     
    --local retrofont = TTFont.new("8-BIT WONDER.TTF", 20, true) --I had to do this because I don't have the font
    local text = TextField.new(nil,username)
    text:setPosition(0, 20)
    stage:addChild(text)
    text:setTextColor(0xfffffff)
    --text:setScale(1.0) this is useless, scale is 1 by default. 
    text:setPosition(10,20)
     
     --[[ this is useless
    bullet = Texture.new("bullet.png")
    bullet = Bitmap.new(bullet)
    stage:addChild(bullet)
    bullet:setScale(0.3)
    bullet:setPosition(0,0)
    bullet:setRotation(-90)
     ]]
     
     
    playership1 = Bitmap.new(Texture.new("ship.png"))
    stage:addChild(playership1)
    playership1:setAnchorPoint(0.5,0.5)
    playership1:setPosition(245,300)
    --playership1:setScale(1.0) same as before, default scale is 1
    --playership1:setColorTransform(0.1,0.9,0.3)
     
    local function onComplete(e)
    	--if e.buttonIndex then
    		username = e.text
    		text:setText(username)
    	--end
    end
     
    textInputDialog:addEventListener(Event.COMPLETE, onComplete)
    textInputDialog:show()
    left = false
    right = false
     
    shipX=width/2
    playerDir=0
    bDir=0
    by= 1
     
    bullets = {}
    counter  = 1
     
    bpool = Pool.new()
     
    function gameloop(e)
    	shipX=shipX + (playerDir* 5)
     
    	if shipX<20 then
    		shipX = 20
    	elseif
    		shipX>(width-20) then
    		shipX = width - 20
    	end	
     
    		playership1:setX(shipX)
     
    	--you need to update the position of each bullet in bullets array
    	for bb in pairs(bullets) do --for any entry of bullets, where bb is index 
    		local bp = bullets[bb]:getY() --bulletPosition: each bullet has its own
    		if bp < -100 then --this is what I decided is enough outside screen boundaries to remove the bullet (in pixels, same scale as setX, setY...)
    			bpool:destroyObject(bullets[bb]) --place bullet in pool
    			bullets[bb] = nil --remove entry from array so that next time it won't be processed
    			print("bullet removed:", bb) --tell me that you did it
    		else --I am still inside the screen, so:
    			local by=bp+(bDir*5) --add the increment to this bullet bp
    			bullets[bb]:setY(by) --set the increment to this bullet
    		end
    	end
     
     
    end
     
    stage:addEventListener(Event.ENTER_FRAME,gameloop)
     
    stage:addEventListener(Event.KEY_DOWN,function(e)
    	if e.keyCode==KeyCode.LEFT then
    		left=true
    		playerDir=-1
    	elseif e.keyCode==KeyCode.RIGHT then
    		right=true
    		playerDir=1
    	elseif e.keyCode==KeyCode.Z then
     
    	print("fire")
    	bx,by=playership1:getPosition()
    	bullets[counter] = bpool:createObject()
    	stage:addChild(bullets[counter])
    	bullets[counter]:setPosition(bx,by)
    	bDir=-1
     
    	counter = counter+1
     
    	end
     
    end)
     
    stage:addEventListener(Event.KEY_UP,function(e)
    	if e.keyCode==KeyCode.LEFT then
    		left=false
    		if right then
    			playerDir=1
    		else
    			playerDir=0
    		end
     
    	elseif e.keyCode==KeyCode.RIGHT then
    		right=false
    		if left then
    			playerDir=-1
    		else
    			playerDir=0
    		end
    	end
    end)
  • MinstrelMinstrel Member
    edited June 2015
    Ok 1 more final thing... and this is so insanely newbie-ish you'll probably laugh... what does eof mean? This will help me out loads in future as I often get the error message main.lua:(no.): 'eof' expected near end.
  • MinstrelMinstrel Member
    edited June 2015
    Btw are you sure there's meant to be three ends after the newly added bit and why was
    by=by+(bDir*5)
    bullet:setY(by)
    removed?
  • piepie Member
    It may happen for various reasons: it usually means that you're not closing something properly: that you missed a "then" after an if statement, or an "end" somewhere. The worst thing about this error is that it tells you where it hangs, but not where you should look to fix it.
    As a best practice, I found that learning to close a function or a statement before writing inside it reduces % to have this error. And using "a lot" of TAB key if you're writing nested things, helps dramatically to spot this kind of mistakes. :)
  • Never Mind!!! It WORKS!!! FULLY AS INTENDED!!! You have no idea how excited I was when I found out it worked. And guess why it didn't work after i'd added what you'd put (which probably made it work). The first line! the Pool = Core.class(sprite)! all because I had put playership 1 in there instead of bullet.

    But honestly I can't thank you enough for all the help you've given me. You have taken time out of your life to help me.
    I am extremely grateful.
    I expect you'll be seeing me on the forums again at some point, but until then, THANK YOU! :D:D:D:D:D:D:D:D:D:D:D
  • piepie Member
    edited June 2015
    I am sure about the end thing, otherwise it would crash :D look at the TABS

    the first is the end to if..else
    the second is the end to for..do
    the third is the end to gameloop.
    by=by+(bDir*5)
    bullet:setY(by)
    was not removed, it was updated to
    local by=bp+(bDir*5) --this is the next y position
    bullets[bb]:setY(by) --set the incremented position to this bullet (bb)
    [edit: I'm glad it worked, I was a newbie too :) ]
  • SinisterSoftSinisterSoft Maintainer
    edited June 2015
    @Minstrel Sorry I've not been around to help, it was my birthday yesterday (20th June) and I wasn't at the computer for very long.

    I'm glad you managed to sort it out (with a lot of help from @pie ), I think you need to look at what the code does and try figure out why it does what it does. Save a good copy somewhere (and print it out?) then if you make a mistake you can compare it to what you have and see where you went wrong.

    EOF mean end of file btw (basically you hit then end when you shouldn't have done). ;)

    (re the spam... I deleted your other thread you started btw so you didn't get multiple answers)
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • MinstrelMinstrel Member
    edited June 2015
    Oh yeah:D (Happy Birthyesterday), I was told over Skype yesterday. Did you enjoy your chocolate?
    BTW thanks for removing my posts as I was going to, but had no clue how. also thanks for the EOF info ;)

    Likes: SinisterSoft

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