Problem with hump.gamestate and accessing deltatime dt on update

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
Rastashmup
Prole
Posts: 31
Joined: Mon Jun 26, 2017 10:36 am

Problem with hump.gamestate and accessing deltatime dt on update

Post by Rastashmup »

Hi, I was trying to restructure some of my code using hump.gamestate, then ran into an error doing a calculation with dt.
Apparently it does not access dt as value but as table, the error message is

"luas/state_gam.lua:12 attempt to perform arithmetic on local 'dt' (a table value)"

I reduced the code to a minimum to reproduce it. The following is from my "MiniExampleOld.love", the content of the main.lua does nothing other than run a custom update 40 times per second and show a black screen, but it works:

old main.lua

Code: Select all

function love.load()
	dUpd = 0.0
	tUpd = 0.0125
end

function love.update(dt)

	dUpd = dUpd + dt
	
	if dUpd > tUpd then
	
		dUpd= dUpd-tUpd
		doGamUpdates()
	end
	
end

function love.draw()
--insert code
end

function doGamUpdates()
--insert code
end
Now the attempt to use a gamestate and structure it with a separate file in the "luas" subfolder. I put it into the "MiniExample.love" and it will give me the error mentioned above:

main.lua

Code: Select all


--requires
	gamestate = require "libs/gamestate" --represent current gamestate
	--class = require "libs/class"
	
--luas requires
	require "luas/state_gam"
	
	gamestate.switch(gam)
	gamestate.init()

function love.load()

end

function love.update(dt)

	gamestate.update(dt)
	
end

function love.draw()

	gamestate.draw()
	
end
luas/state_gam.lua

Code: Select all

gam = {} -- previously: Gamestate.new()

function gam.init()

	dUpd = 0.0
	tUpd = 0.0125
end


function gam.update(dt)

	dUpd = dUpd + dt --This line will throw the error
	
	if dUpd > tUpd then
	
		dUpd= dUpd-tUpd
		doGamUpdates()
	end
	
end

function gam.draw()
--insert code
end

function doGamUpdates()
--insert code
	
end
Any advice what is wrong with the use of the deltatime?
Attachments
MiniExampleOld.love
Version without gamestate that works
(1.28 KiB) Downloaded 139 times
MiniExample.love
Version with gamestate that doesnt work
(6.29 KiB) Downloaded 130 times
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Problem with hump.gamestate and accessing deltatime dt on update

Post by bartbes »

hump.gamestate also passes in the gamestate as the first argument to the callbacks. The easiest way to to deal with this is to change the definition of gam.update(dt) to gam:update(dt), which is equivalent to gam.update(self, dt). The same is true for the other callbacks, but you don't really notice because the functions don't accept any arguments.
Post Reply

Who is online

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