Page 1 of 1

[CLOSED] Function disappearing [CLOSED]

Posted: Mon May 11, 2015 1:18 am
by seuterr
I've been working on my first game, and the code is very messy. However, I have separate files for different functions, and at the bottom of each file, I've been putting a larger function that holds all of the functions in that specific file. From there, I have

Code: Select all

require "collide"
in my main code. For each different type of function I have in those files, my super functions have a different category, such as

Code: Select all

COLLIDE_UPDATE()
to go into the love.update function. All of my other super functions like this are working perfectly, but when I try using my COLLIDE_UPDATE() function, when I load it up, it returns:

Error

main.lua:62: attempt to call global 'COLLIDE_UPDATE' (a nil value)


Traceback

main.lua:62: in function 'update'
[C]: in function 'xpcall'

When I take all of the individual functions and put them there without using the super function, it works.
For future reference after this point, gsize is what I multiply everything related to the coordinates, because my textures are 32x32, and I want them to be 64x64
collide.lua:

Code: Select all

function collidescreen(entity)
	--Keep entity on tiles
	if entity.x < 32 * gsize then
		bump:setVolume(.5)
		bump:play()
		entity.x = 32 * gsize
	end
	if entity.x > (288 * gsize) - 32 * gsize then
		bump:setVolume(.5)
		bump:play()
		entity.x = (288 * gsize) - 32 * gsize
	end
	if entity.y < 32 * gsize then
		bump:setVolume(.5)
		bump:play()
		entity.y = 32 * gsize
	end
	if entity.y > (288 * gsize) - 32 * gsize then
		bump:setVolume(.5)
		bump:play()
		entity.y = (288 * gsize) - 32 * gsize
	end
end
function collideconveyorbelt(entity)
	--Do entity conveyorbelt
	if entity.x > 64 * gsize and entity.x < 96 * gsize and entity.y > 64 * gsize and entity.y < 96 * gsize then
		entity.x = entity.x + 3.5
	end
end
function COLLIDE_UPDATE()
	collidescreen(player)
	collidescreen(zombie)
	collideconveyorbelt(player)
	collideconveyorbelt(zombie)
end
Yes, this is a very bad way to lay out code, and such, but I don't care about that right now.
love.update in main.lua:

Code: Select all

function love.update(dt)
	music:play()
	if gamestate ~= "paused" then
		--WASD Controls
		if love.keyboard.isDown("d") then								
			player.r = 90								
			player.x = player.x + player.speed * dt 	
		elseif love.keyboard.isDown("a") then											
			player.r = 270								
			player.x = player.x - player.speed * dt 	
		elseif love.keyboard.isDown("s") then	
			player.r = 180								
			player.y = player.y + player.speed * dt 	
		elseif love.keyboard.isDown("w") then							
			player.r = 0								
			player.y = player.y - player.speed * dt 	
		end
		--Entity movement
		zombie.loop = zombie.loop + zombie.inc * dt
		if zombie.loop > 0 and zombie.loop <= 7 then
			zombie.r = 0
			zombie.y = zombie.y - zombie.speed * dt
		elseif zombie.loop > 7 and zombie.loop <= 14 then
			zombie.r = 180
			zombie.y = zombie.y + zombie.speed * dt
		elseif zombie.loop > 14 then 
			zombie.loop = 1
		end
	end
	--Collision updates
	COLLIDE_UPDATE()
end
The entities I'm using (in entities.lua):

Code: Select all

--Player entity statuses
player = {
	x = 48 * gsize,
	y = 48 * gsize,
	speed = 125,
	r = 0
}
--Zombie entity statuses
zombie = {
	x = 240 * gsize,
	y = 272 * gsize,
	speed = 80,
	r = 0,
	loop = 1,
	inc = 1
}
--Girl NPC entity statuses
girl = {
	x = 80 * gsize,
	y = 40 * gsize,
	r = 180
}
If you need more information to help me get this fixed, I can post all of the code for my game. Until then, if you can help me, please do.

Re: Function disappearing

Posted: Mon May 11, 2015 8:14 am
by BOT-Brad
If you could attach your .love file or simply a .zip or .rar of your code, that would help find the issue much easier. :nyu:

Re: Function disappearing

Posted: Mon May 11, 2015 12:26 pm
by s-ol
You probably just forgot to "require" the file that defines it.

Re: Function disappearing

Posted: Mon May 11, 2015 1:03 pm
by seuterr
S0III0s: I will put up the code later today, but as of the moment I actually do have the require for the said file, and like I said, it works when I don't use the super function, but when I do, it fails and generates the error.

Re: Function disappearing

Posted: Mon May 11, 2015 11:08 pm
by seuterr
Here is the download link for the zip file containing everything required to run the game. https://www.mediafire.com/?g7gs3dmssnf9uaz

Re: Function disappearing

Posted: Tue May 12, 2015 12:48 pm
by BOT-Brad
Just from looking at the code, there is no COLLIDE_UPDATE function in your collide.lua file. If I add in

Code: Select all

function COLLIDE_UPDATE()
   collidescreen(player)
   collidescreen(zombie)
   collideconveyorbelt(player)
   collideconveyorbelt(zombie)
end
under your MAP_UPDATE function, then the game runs and works as I expect it should.

Re: Function disappearing

Posted: Tue May 12, 2015 10:32 pm
by seuterr
BOT-Brad wrote:Just from looking at the code, there is no COLLIDE_UPDATE function in your collide.lua file. If I add in

Code: Select all

function COLLIDE_UPDATE()
   collidescreen(player)
   collidescreen(zombie)
   collideconveyorbelt(player)
   collideconveyorbelt(zombie)
end
under your MAP_UPDATE function, then the game runs and works as I expect it should.
For some reason, I uploaded the wrong file. When I run what you have, the game works fine, but with my current version, the map is not stored in the collide.lua file. I'm probably just being really dumb, but I replaced the file with the new one.

Re: Function disappearing

Posted: Thu May 21, 2015 11:28 pm
by seuterr
Yup. I was just being stupid. Thank you for all of the help!