Crash when using my function

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
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Crash when using my function

Post by NoreoAlles »

Hey, i want to be able to place blocks in my Platformer and wrote this function to have them all on the same grid.

Code: Select all

--v is the value, m is the value that v should be the multiple of and dir says if v should be subtracted by one or if one should be added to it
function roundToMultiple(v, m, dir) 
    local v = v 
    local m = m
    local dir = dir 
    math.floor(v)
    while(v%m < 0 or v%m > 0) do 
        v = v + dir
    end
    return v
end
And this is the function in wich i use the seen above function.

Code: Select all

function Player:placeBlock(key)
    if key == "b" then 
        if self.x%50 == 0 and  self.y%50 == 0 then 
            obj = Object.new(self.x, self.y,50, 50)
            table.insert(Bdraw, obj)
        else 
            local x = roundToMultiple(self.x, 50, -1)
            local y = roundToMultiple(self.y, 50, -1)
            obj = Object.new(x, y,50, 50)
            table.insert(Bdraw, obj)
        end
    end
end

But it crashes
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
BrotSagtMist
Party member
Posts: 614
Joined: Fri Aug 06, 2021 10:30 pm

Re: Crash when using my function

Post by BrotSagtMist »

Youre not actually using math.floor(v), thats about the only error i see.
It has to be v=math.floor(v).

Further a construction like local v = v is nonsense since having v in the function definition already makes it a local.
obey
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Crash when using my function

Post by zorg »

If it crashes, post the error and traceback you get; if it hangs, then you just learned why arithmetic with floating point numbers equaling a specific number might not ever work unless you keep your numbers whole.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Crash when using my function

Post by NoreoAlles »

zorg wrote: Mon Mar 28, 2022 1:58 pm If it crashes, post the error and traceback you get; if it hangs, then you just learned why arithmetic with floating point numbers equaling a specific number might not ever work unless you keep your numbers whole.
It just hangs
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Crash when using my function

Post by NoreoAlles »

BrotSagtMist wrote: Mon Mar 28, 2022 1:29 pm Youre not actually using math.floor(v), thats about the only error i see.
It has to be v=math.floor(v).

Further a construction like local v = v is nonsense since having v in the function definition already makes it a local.
Thanks, that fixed it.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
Post Reply

Who is online

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