Page 1 of 1

Help With Tables? [Code Included]

Posted: Sun Dec 18, 2011 6:49 pm
by Rukiryo
I'm trying to make these clouds generate and slowly move throughout my map, but it won't work. I need each cloud to have an X and Y value, so I stuck each cloud in a table, and that table goes into the table that the script draws and moves. It keeps giving me "stack overflow" error, on the 'love.draw(v,v.X,v.Y)' so am I referring to the values in the table wrong? Note: All images exist, it's just the cloud part that is difficult. And yes, the cloud image exists. I've gotten the cloud to spawn from my second script, no problem, just I need to make multiple clouds generated and have them move. Code:

main.lua

Code: Select all


require("cloudGen.lua")

cloudDraw = false
newcloud = 0

function love.load()
load_clouds()
island = love.graphics.newImage("SkyIsland.png",500,300)
tree = love.graphics.newImage("Tree.png",100,150)
bench = love.graphics.newImage("Bench.png",100,50)
end


	function love.draw()
	draw_clouds()
	love.graphics.draw(island,100,200)
	love.graphics.draw(tree,150,205)
	love.graphics.setBackgroundColor(240, 248, 255)
	end


function love.update()

newcloud = newcloud + 1
	if newcloud >= 60 then
	newcloud = 0
	needCloud = true
	
	
	end
end
cloudGen.lua

Code: Select all



needCloud = false
clouds = {}
pendingClouds = {}


	
	function load_clouds()
		if needCloud == true then
		needCloud = false
		local cloud = love.graphics.newImage("Cloud.png",100,50)
		local cloudTable = {}
		table.insert(pendingClouds,cloudTable)
		table.insert(cloudTable,cloud)
			for i, v in pairs(pendingClouds) do
			X = 500
			Y = math.random(50,300)
			table.insert(cloudTable,X)
			table.insert(cloudTable,Y)
			end
			for i = 1,#pendingClouds do
			table.insert(clouds,pendingClouds[i])
			table.remove(pendingClouds,i)
			end
		end
	end

	
	function draw_clouds()
	for i, v in pairs(clouds) do
	love.draw(v.cloud,v.X,v.Y)--The "stack overflow" error says it's this line
	v.X = v.X - 0.003
	end

	
	end
Any ideas? I'm new to this language.

Re: Help With Tables? [Code Included]

Posted: Sun Dec 18, 2011 7:30 pm
by tentus

Code: Select all

love.draw(v.cloud,v.X,v.Y)--The "stack overflow" error says it's this line
should be

Code: Select all

love.graphics.draw(v.cloud, v.X, v.Y)

Re: Help With Tables? [Code Included]

Posted: Sun Dec 18, 2011 7:50 pm
by Ellohir
EDIT: The problem is the mentioned by tentus and Taehl, but I looked into your code anyway ;)

Why are you using that "needClouds" thing? You only load the clouds once, you don't need to check if they exist. I think you should remove that part.

Code: Select all

local cloud = love.graphics.newImage("Cloud.png",100,50)
      local cloudTable = {}
      table.insert(pendingClouds,cloudTable)
      table.insert(cloudTable,cloud)
This is a very weird way to do this. You can simply do:

Code: Select all

      local c = {}
      c.cloud = love.graphics.newImage("Cloud.png",100,50)
      table.insert(pendingClouds,c)
The name "cloudTable" makes me think of a list of clouds, not on a single cloud. I renamed it to "c", I think it's a good way to know that it's a "simple" element (like the elements on a ipairs for). And you don't have this on a loop so you only have one "pendingClouds" on the load function. Maybe this is not what you wanted.



Code: Select all

         for i, v in pairs(pendingClouds) do
         X = 500
         Y = math.random(50,300)
         table.insert(cloudTable,X)
         table.insert(cloudTable,Y)
         end
You loop through pendingClouds but you do nothing with them, you do things to a local variable cloudTable. And, as I said, there is onlye one cloud in "pendingClouds", so... you should take a look at that too.

It was a bit hard to understand your code. Try to comment it. I think you should revise your code completely understanding what it does and explaining it to us.

Re: Help With Tables? [Code Included]

Posted: Sun Dec 18, 2011 8:17 pm
by Taehl
The reason that typo gave you a stack overflow is because love.draw is the name of the entire drawing function! In effect, you were doing this:

Code: Select all

function runAwayLoop()
	runAwayLoop()	-- recurse forever!
end
It'll call itself infinitely, which would make Lua freeze. Instead, Love gives you an error.