Page 1 of 1

How do I make a value absolute?

Posted: Fri Oct 20, 2023 12:01 am
by Hedgo
I've skimmed through the wiki and forum, can't find the answer.
Sorry if this is dumb.

Re: How do I make a value absolute?

Posted: Fri Oct 20, 2023 12:05 am
by BrotSagtMist
Yes, yes it is.
I am not entirely sure what you want but ill just assume you mean deleting something?
That would be value=nil.

Re: How do I make a value absolute?

Posted: Fri Oct 20, 2023 12:18 am
by MrFariator
If you mean to take the absolute value of a number, the function you're looking for is math.abs:

Code: Select all

local value = -2
value = math.abs(value)
print(value) -- prints 2
However, if you mean something like a constant value, ie. a value that can not be changed, that's a bit more complicated topic.

Re: How do I make a value absolute?

Posted: Fri Oct 20, 2023 1:19 am
by BrotSagtMist
Oh i read this as obsolete. My bad.

Re: How do I make a value absolute?

Posted: Fri Oct 20, 2023 3:18 am
by Hedgo
Since I've already made this thread I might as well ask about another problem I'm having.

I'm trying to make a Sonic game and for some reason, I can't get the momentum quite working. I've used virtually the same code on Scratch and it works there, so maybe I messed something up in the process of turning it into LUA, but I can only get the player to move correctly if they're moving right. If I press left they just won't move. (properly, at least.)
sonicproto.love
(3.64 KiB) Downloaded 35 times
There's the love file. The code is a mess from trying to experiment but it's clearly has been in vain. Thanks to whoever helps.

Re: How do I make a value absolute?

Posted: Fri Oct 20, 2023 4:30 am
by BrotSagtMist
I was about too say this looks easy but the longer i look at it the less i understand.
Honestly, start from scratch.
You simply dont have to deal with signs at all.
The position is composed of currentpos+ accellerate*dt.
And accelerate=(accelerate -leftbutton +rightbutton) * resistance (That is, a float smaller 1).
Makes, including keys, 4 lines for a basic left/right movement.
This whole if cases for negative positive directions wont get you anywhere.