Problems with classes + arrays...

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Afr0
Prole
Posts: 19
Joined: Mon Feb 22, 2010 9:01 am

Problems with classes + arrays...

Post by Afr0 »

Why are my bullets not getting drawn?
My playership is drawn perfectly fine!

Code: Select all

require("PlayerShip")
require("Bullet")

function love.load()
	love.graphics.setBlendMode('alpha')
	love.graphics.toggleFullscreen()
	
	PShip = PlayerShip.Create()
	Bullets = {}
	
	for i = 1, 1000 do
		Bullets[i] = {}
	end
	
	BulletIndex = 1
end

function love.keypressed(k)
	if k == 'escape' then
		love.event.push('q') --Quits the game
	end
	
	--TODO: Create bullet instance and add to array!
	if k == 'lctrl' then
		Bullets[BulletIndex] = Bullet.Create()
		BulletIndex = BulletIndex + 1
	end
end

function love.update(dt)
	if love.keyboard.isDown('up') then
		PShip.Y = PShip.Y - 1
	end
	
	if love.keyboard.isDown('down') then
		PShip.Y = PShip.Y + 1
	end
	
	if love.keyboard.isDown('left') then
		PShip.X = PShip.X - 1
	end
	
	if love.keyboard.isDown('right') then
		PShip.X = PShip.X + 1
	end
	
	--for i = 1, 1000 do
	--	if not Bullets[i] == nil then
	--		Bullets[i].Y = Bullets[i].Y - 1
	--	end
	--end
end

function love.draw()
	love.graphics.draw(PShip.Image, PShip.X, PShip.Y)
	
	for i = 1, 1000 do
		if Bullets[i] == not nil then
			love.graphics.draw(Bullets[i].Image, Bullets[i].X, Bullets[i].Y)
		end
	end
end
Here is my Bullet class:

Code: Select all

Bullet = {}
BulletMT = { Index = Bullet } --This metatable will be attached to every created class instance.

function Bullet:Create()
	local NewInstance = 
	{
		Image = love.graphics.newImage("Bullet.png"),
 		X = 350,
		Y = 80
	} --The new instance
	setmetatable(NewInstance, BulletMT) --All instances share the same metatable.
	
	return NewInstance
end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Problems with classes + arrays...

Post by bartbes »

Afr0 wrote:

Code: Select all

		if Bullets[i] == not nil then
That is some bullshit right there. "== not" doesn't exist, what it does it checks if it is equal to "not nil", which is "true" (the boolean), you could use either

Code: Select all

if Bullets[i] ~= nil then
or even

Code: Select all

if Bullets[i] then
and that will work great.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Problems with classes + arrays...

Post by Robin »

Also,

Code: Select all

Bullet = {}
BulletMT = { Index = Bullet } --This metatable will be attached to every created class instance.
Won't work. You will need to do:

Code: Select all

Bullet = {}
BulletMT = { __index = Bullet } --Note the two underscores, and lowercase i
And:

Code: Select all

function Bullet:Create()
   local NewInstance =
   {
      Image = love.graphics.newImage("Bullet.png"),
      X = 350,
      Y = 80
   } --The new instance
   setmetatable(NewInstance, BulletMT) --All instances share the same metatable.
   
   return NewInstance
end
You might not want to make a new Image for every bullet you create -- it will eat up a lot of resources.

Instead, try this:

Code: Select all

Bullet.Image = love.graphics.newImage("Bullet.png"),
function Bullet:Create()
   local NewInstance =
   {
      X = 350,
      Y = 80
   } --The new instance
   setmetatable(NewInstance, BulletMT) --All instances share the same metatable.
   
   return NewInstance
end
Last but not least, Bullet:Create() is still overly verbose. This will work just as well:

Code: Select all

Bullet.Image = love.graphics.newImage("Bullet.png"),
function Bullet:Create()
   return setmetatable({ X = 350, Y = 80 }, BulletMT)
end
Hope this helps. ;)
Help us help you: attach a .love.
Afr0
Prole
Posts: 19
Joined: Mon Feb 22, 2010 9:01 am

Re: Problems with classes + arrays...

Post by Afr0 »

Thanks guys!
I love the responsetime on this forum, even though it seems to be a relatively small one. :P

I did this:

Code: Select all

Bullet = {}
BulletMT = { __index = Bullet } --This metatable will be attached to every created class instance.

Bullet.Image = love.graphics.newImage("Bullet.png")

function Bullet:Create()
	local NewInstance = 
	{
 		X = 350,
		Y = 90
	} --The new instance
	setmetatable(NewInstance, BulletMT) --All instances share the same metatable.
	
	return NewInstance
end

Code: Select all

require("PlayerShip")
require("Bullet")

function love.load()
	love.graphics.setBlendMode('alpha')
	love.graphics.toggleFullscreen()
	
	PShip = PlayerShip.Create()
	Bullets = {}
	
	for i = 1, 1000 do
		Bullets[i] = {}
	end
	
	BulletIndex = 1
end

function love.keypressed(k)
	if k == 'escape' then
		love.event.push('q') --Quits the game
	end
	
	--TODO: Create bullet instance and add to array!
	if k == 'lctrl' then
		Bullets[BulletIndex] = Bullet.Create()
		BulletIndex = BulletIndex + 1
	end
end

function love.update(dt)
	if love.keyboard.isDown('up') then
		PShip.Y = PShip.Y - 1
	end
	
	if love.keyboard.isDown('down') then
		PShip.Y = PShip.Y + 1
	end
	
	if love.keyboard.isDown('left') then
		PShip.X = PShip.X - 1
	end
	
	if love.keyboard.isDown('right') then
		PShip.X = PShip.X + 1
	end
	
	--for i = 1, 1000 do
	--	if not Bullets[i] == nil then
	--		Bullets[i].Y = Bullets[i].Y - 1
	--	end
	--end
end

function love.draw()
	love.graphics.draw(PShip.Image, PShip.X, PShip.Y)
	
	for i = 1, 1000 do
		if Bullets[i] then
			love.graphics.draw(Bullets[i].Image, Bullets[i].X, Bullets[i].Y)
		end
	end
end
Now I get an error on the love.graphics.draw() call on line 59! 'Expected userdata'!
What does this mean?

Also... any plans to incorporate network support into Löve anytime soon? :D

Edit: Robin, I made a game a long time ago in Blitz Basic I called 'Space'. The game I'm kinda puzzling with now I was thinking of calling 'Space 2'. Hope you don't mind! :P
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Problems with classes + arrays...

Post by bartbes »

Afr0 wrote:Now I get an error on the love.graphics.draw() call on line 59! 'Expected userdata'!
What does this mean?
That you didn't pass an image object. (don't know why, maybe I should actually read your code :P)
Afr0 wrote:Also... any plans to incorporate network support into Löve anytime soon? :D
LuaSocket is integrated and we have an excellent networking library called LUBE
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Problems with classes + arrays...

Post by Robin »

Afr0 wrote:Now I get an error on the love.graphics.draw() call on line 59! 'Expected userdata'!
What does this mean?
I think the problem lies here:

Code: Select all

	for i = 1, 1000 do
		Bullets[i] = {}
	end
You see, empty tables are true. And when drawing those bullets, you only check whether they are true, not if they actually have an Image. If you remove those three lines, it will (hopefully ;)) work fine.
Afr0 wrote:Edit: Robin, I made a game a long time ago in Blitz Basic I called 'Space'. The game I'm kinda puzzling with now I was thinking of calling 'Space 2'. Hope you don't mind! :P
I am only disappointed -- that you are as bad as me in making original names. :P
bartbes wrote:we have an excellent networking library called LUBE
*cough* shameless self-promotion *cough*

:P
Help us help you: attach a .love.
Afr0
Prole
Posts: 19
Joined: Mon Feb 22, 2010 9:01 am

Re: Problems with classes + arrays...

Post by Afr0 »

Ah, thanks!

When I brushed up on Lua variables here, I got the impression that you needed to initialize all arrays in Lua. Apparently not so!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Problems with classes + arrays...

Post by bartbes »

Well, yeah, you do, but before setting them, not after.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 55 guests