Page 1 of 1

Questions about "spawning things" with love and lua

Posted: Sat Mar 02, 2013 11:11 am
by Comonut
Hi guys
I've been playing with Love and so far I love it. However, I'm struggling with something which is important for whatever project i would try to do. I have no idea how to program things which behave in a certain way and that can spawn frequently. For example, how can i program arrow which spawn on click? I suppose that this is where object oriented programming comes in handy, but I have no idea how to implement the idea of OOP. I'm not sure that I've explained my problem clearly, so if you need more info you can ask.
Thanks

Re: Questions about "spawning things" with love and lua

Posted: Sat Mar 02, 2013 11:42 am
by T-Bone
Well, it depends on what you're trying to do. Lua isn't a fully object oriented language, so you don't need to formally create classes and stuff. So much of what you can read about OOP might not be necessary for a game.

There are several ways to "spawn things" in Lua. One of the simplest is to just use functions that create tables. Sort of like this:

Code: Select all

function spawnEnemy() 
	enemy = {}
	--do stuff with it, add stuff to the table enemy
	return enemy
end

--use like this
enemy1 = spawnEnemy()
enemy2 = spawnEnemy()

--or like this
enemies = {}
for i=1,100 do
	table.insert(enemies,spawnEnemy())
end

For a total beginner to programming, this is definately good enough. It is not the fastest and most efficient way of doing it, but it's not something you're going to notice in a relatively simple game.

Re: Questions about "spawning things" with love and lua

Posted: Sat Mar 02, 2013 2:48 pm
by scutheotaku
You could do this with tables. Try something like this...

Short version without lengthy comments:

Code: Select all

--declare arrow table
arrow = { }

--this represents whether an arrow has been marked for removal - this is explained below
arrowRemoved = false

arrowImage = --SET AN IMAGE FOR THE ARROW HERE!

--function for spawning arrows
function NewArrow(x,y,sx,sy,r,rSpeed)
	local id = #arrow+1

	arrow[id] = {
			x = x,
			y = y,
			sx = sx,
			sy = sy,
			r = r,
			rSpeed = rSpeed,
			removed = false
	}

        return id
end

function UpdateArrows(dt)
	--removing arrows marked for removal
	if arrowRemoved then
		arrowRemoved = false
		for i = #arrow, 1, -1 do
			if (arrow[i].removed == true) then
				table.remove(arrow, i)
			end
		end
	end
	
	--update loop
	for i,v in ipairs(arrow) do
		if (v.removed == false) then
			v.r = v.r + v.rSpeed * dt
		
		end
	end
end

--now, let's draw the arrows!
function DrawArrows()
	for i,v in ipairs(arrow) do
		love.graphics.draw(arrowImage, v.x, v.y, v.r, v.sx, v.sy)
	end
end

--function for marking a particular arrow for removal
function RemoveArrow(i)
	arrow[i].removed = true
	arrowRemoved = true
end
Lengthy comments version:
https://dl.dropbox.com/u/17269775/arrow.lua

If you'd like more traditional OO programming, I suggest that you start here:
http://www.lua.org/pil/16.html