math.round() doesn't work

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
mk8
Prole
Posts: 34
Joined: Thu Apr 22, 2021 7:11 am

math.round() doesn't work

Post by mk8 »

So I have a function that look something like this:

Code: Select all

function checkPlayerCollisions()
	local x = player.x
	local y = player.y
	local rx = math.round(x)
	local ry = math.round(y)
	. . .
end
And the problem is, that the program can't call math.round() for some reason, it always says "attempt to call field 'round' (a nil value)", even if I remove the 'local' thing, just always, with any variable in this particular function (it doesn't throw any errors in the other functions).
can some1 halp pls
hippity hoppity ur code is my property (thanc in advanc)
Jose_antonio_920
Prole
Posts: 8
Joined: Wed Jul 15, 2020 10:56 pm

Re: math.round() doesn't work

Post by Jose_antonio_920 »

This code has one erro, not is math.round, os math.random, but hás love.math.random too, for use math.round, before add "math.round=math.random" or "math.round=love.math.random"
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

Re: math.round() doesn't work

Post by Eglaios »

Still new there, but I'll give a try...
From what I understood, there isn't "math.round" in Lua ; instead, we have math.floor and math.ceil (pretty much self-explanatory).
Currently working on game music...
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: math.round() doesn't work

Post by ReFreezed »

math.round doesn't exist in Lua by default. You'll have to define it yourself:

Code: Select all

math.round = function(n)
	return math.floor(n + 0.5)
end
See the Lua manual for all existing math functions.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
mk8
Prole
Posts: 34
Joined: Thu Apr 22, 2021 7:11 am

Re: math.round() doesn't work

Post by mk8 »

thank you all so much it works now
hippity hoppity ur code is my property (thanc in advanc)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 24 guests