Help With Tables? [Code Included]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Rukiryo
Prole
Posts: 13
Joined: Sun Dec 18, 2011 4:29 am

Help With Tables? [Code Included]

Post 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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Help With Tables? [Code Included]

Post 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)
Kurosuke needs beta testers
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: Help With Tables? [Code Included]

Post 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.
Last edited by Ellohir on Sun Dec 18, 2011 8:34 pm, edited 1 time in total.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Help With Tables? [Code Included]

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 6 guests