Page 1 of 2
Cannot load more the one class object
Posted: Sun Jan 11, 2015 1:22 pm
by savagedogg38
I cannot seem to manage more than one object - in my case, badie - at a time. I can have it to where, in "game" mode, I have one ball, or badie, moving about on screen at one time. Been trying everything I can think of and following all the object oriental tutorials I can find, but I'm at a total loss. Been on this for days straight and just getting exhausted and ready to throw up my hands. Could use some help. I will provide all my code below under "game2". Thanks in advance.
https://github.com/SavageDogg/UntidyTid ... Lua/Game02
badie = class:new()
function badie:init( x, y)
self.x = x
self.y = y
self.dx = 1
self.dy = 1
self.alive = true
end
function badie:update(dt)
self.x = self.x + self.dx*dt
self.y = self.y + self.dy*dt
end
function badie:draw()
love.graphics.circle("fill", self.x, self.y, 10, 8)
end
return badie
"badie" above is called in "game" below which is referenced in main.lua
require "badie"
function game_load(map)
love.graphics.setBackgroundColor(100,100,100)
b.badie:init( math.random(800), math.random(600))
end
function game_update(dt)
b.badie:update(dt)
end
function game_draw()
love.graphics.printf("GAME " .. map, 0, 0, 800)
b.badie:draw()
end
function game_keypressed(key)
if key == "escape" then
state = "menu"
menu_load()
end
end
You'll see that I'm going for the game opening in a splash screen which will then enter a menu and subsequent game map - form there, I'm trying to call objects, bad guys, or anything via object oriented methods.
Anyways, I'm stuck. Thanks again!
Re: Cannot load more the one class object
Posted: Sun Jan 11, 2015 4:22 pm
by Azhukar
Try replacing this:
Code: Select all
b.badie:init( math.random(800), math.random(600))
With this:
Code: Select all
b = badie:init( math.random(800), math.random(600))
And then calling b:draw() instead of b.badie:draw(). Same with b:update() instead of b.badie:update(). You need to first create an instance of a class and then work with that.
Re: Cannot load more the one class object
Posted: Sun Jan 11, 2015 10:42 pm
by savagedogg38
I thought I may have tried your suggestion before which led me to the wacky version you see - I was trying anything at that point - but I tried it again just to be sure. I got game.lua:12: attempt top index global 'b' (a nil value) - for some reason it isn't creating an instance - but my badie.lua looks good. Perhaps it is the basic class object I grabbed from the site?
Here is the traceback provided by LOVE:
Error
game.lua:12: attempt to index global 'b' (a nil value)
Traceback
game.lua:12: in function 'game_update'
main.lua:41: in function 'update'
[C]: in function 'xpcall'
Thanks for affimring my suspecions that that is indeed the correct way to call and work with an instance - I have done it before the dsame way in other programs I have made with LOVE - whihc can also be found on my github - but I was just so frustrated and up all night seeing double at the point I posted this; hence, the krazy looking call in game.lua to badie.lua - I corrected it as you say, but... there it is. Thanks again though. It is much appreciated. If you can find the time, I suspect the problem is in badie.lua or class.lua, but it's merely a guess at this point. I'm rambling...
Re: Cannot load more the one class object
Posted: Sun Jan 11, 2015 10:47 pm
by savagedogg38
New version with changes per suggestion up on github for review for anyone wanting to take a look. thanks again all.
function game_load(map)
love.graphics.setBackgroundColor(100,100,100)
b = badie:init( math.random(800), math.random(600))
end
function game_update(dt)
b:update(dt)
end
function game_draw()
love.graphics.printf("GAME " .. map, 0, 0, 800)
b:draw()
end
function game_keypressed(key)
if key == "escape" then
state = "menu"
menu_load()
end
end
above is game.lua
below is badie.lua
badie = class:new()
badie = {}
function badie:init( x, y)
self.x = x
self.y = y
self.dx = 1
self.dy = 1
self.alive = true
end
function badie:update(dt)
self.x = self.x + self.dx*dt
self.y = self.y + self.dy*dt
end
function badie:draw()
love.graphics.circle("fill", self.x, self.y, 10, 8)
end
Re: Cannot load more the one class object
Posted: Sun Jan 11, 2015 11:30 pm
by bartbes
Please use code tags in the future.
Now, the problem is that you call 'badie:init', which expects a badie object, and initialises it. Well, that's not what we have, so we need to construct an object and initialise that. We could do
Code: Select all
b = {}
badie.init(b, math.random(800), math.random(600))
Or, since SECS is in there, but commented out, if you change the first line of badie.lua back to the class:new call, we can then just do
Code: Select all
b = badie:new(math.random(800), math.random(600))
Re: Cannot load more the one class object
Posted: Sun Jan 11, 2015 11:41 pm
by Foxcraft
Hey savagedogg38,
I think it would be good to point out that you are using the SECS library to create your classes. Always try to give info like that.

Also, providing the .love file directly helps people play with it and get at it quicker.
Anyway, from the recent code and playing with it, I see two things wrong that I think changing should fix it up.
1. Remove "badie = {}" from badie.lua. By having that there, you are erasing "badie = class:new()" by telling badie to be something else.
2. "b = badie:init(someX, someY)" is setting b to nil. Make it not be nil and it won't be nil.
Now, you may be lost on how you're setting it to nil. Remember that when you do "x = someFunction()", you are telling x to be what value someFunction gives to it. Your init function is not giving b anything, and nothing is nil in Lua.
What you want to do is give the badie setup to b.
Code: Select all
function badie:init( x, y)
self.x = x
self.y = y
self.dx = 1
self.dy = 1
self.alive = true
return self -- this should do the trick
end
edit: Oh, the code has changed since I downloaded it....

Re: Cannot load more the one class object
Posted: Sun Jan 11, 2015 11:47 pm
by savagedogg38
I actually did a very crude hack to get it working. I dropped dependence on class.lua entirely. I'm going to try you suggestion anyway but if you would like to review what I have done, you can check it out on my github. All I changed was the init function. I'll copy at post it below for ease too, but the code in its entirety is on my github. thanks! Will try it and let you know!
badie = {} -- badie = class:new() -- also seems to works -- but dropped it anyway
badie.__index = badie --with a complete reworking of init, this seems to be working better
function badie.init( name, x, y)
local entity = {}
setmetatable(entity, badie)
entity.name = name
entity.x = x
entity.y = y
entity.dx = 1
entity.dy = 1
entity.alive = true
return entity
end
Re: Cannot load more the one class object
Posted: Sun Jan 11, 2015 11:50 pm
by savagedogg38
After trying it, it jogged my memory. This, the suggestions I have gotten so far, is where I started and was only able to have one instance at a time. The method I have above gives me as many instances as I want. thanks though - I do think the suggestions I got so far are similar if not exactly where I started and was only able to get one object.
Re: Cannot load more the one class object
Posted: Sun Jan 11, 2015 11:58 pm
by savagedogg38
Sorry, just saw your post about code tags - I will look into it. thanks!
Re: Cannot load more the one class object
Posted: Mon Jan 12, 2015 12:00 am
by savagedogg38
bartbes wrote:Please use code tags in the future.
Now, the problem is that you call 'badie:init', which expects a badie object, and initialises it. Well, that's not what we have, so we need to construct an object and initialise that. We could do
Code: Select all
b = {}
badie.init(b, math.random(800), math.random(600))
Or, since SECS is in there, but commented out, if you change the first line of badie.lua back to the class:new call, we can then just do
Code: Select all
b = badie:new(math.random(800), math.random(600))
I see - very helpful. thank you. I am coming from Python where objects are much better supported. Lua is very new to me but I see the need to learn it so here I am. Thanks!