Page 1 of 1
How to check Player's direction?
Posted: Sat Aug 15, 2020 12:08 pm
by Flarestripe
I'm trying to make it where the game checks what direction you're facing, and then sets your speed to be a certain amount depending on which way you're facing.
(if you're also on the ground and holding a certain button, I already got that part figured out, tho)
<Any help is appreciated. I'm sorry for any questions I ask after, I'm just very curious- XD>
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 12:24 pm
by Emral
player.direction is an integer either containing -1 (left) or 1 (right).
https://docs.codehaus.moe/#/reference/player
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 12:37 pm
by Flarestripe
Oki, thank chu =3 (dang, someone is being very helpful, aren't you? XD
Thank you, tho.
I really appreciate it)
Added in 5 minutes 37 seconds:
So, for right, would I type it as:
If player.direction = 1 then
Or how would I type it as an "If then" statement?
(If that's possible)
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 12:54 pm
by Emral
You have to do == because = is assignment and == is comparison. I usually remember it with the syllables. One = is "set" and two is "e-quals". But yes, that would work given that adjustment.
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 1:07 pm
by Flarestripe
Oki, thank chu.
Added in 12 minutes 55 seconds:
Ot worked great! ^-^
One last question kinda related to this:
Ie there a way to make a part of code be delayed for a bit, instead of activating immediately?
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 1:24 pm
by Emral
You can make a timer variable, so just any old variable that you then increment or decrement when you need to. And when it reached a certain point, you can check in an if statement to see what should happen then.
local timerVar = 0
function onTick()
timerVar = timerVar + 1
if timerVar == 64 then -- approximately 1 second
....
end
end
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 1:43 pm
by Flarestripe
I'm confused how to exactly put that in the code... pwp
Here, if I send you a picture of what I have so far, and what I'm trying to do with it, would you please show me visually where to put certain codes?
(Idk how, maybe re-make the code and then add the timer for it, maybe that would help me better)
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 1:54 pm
by Emral
Sure, go ahead.
Generally though, it's all in luna.lua, and you define the timer at the top of your file and then access and manipulate it in onTick, because onTick runs every frame. The exact placement depends on what you want to do. For instance, you would only want to increment the timer when you are actually using it, which you can check for with another if statement and perhaps another variable that determines whether or not the timer should currently be active.
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 2:01 pm
by Flarestripe
I'm still not TOO smart with Luna yet, so I'm sorry if I'm hard to help- XD
Here's the Code I have so far, tho.
I just want to make it where, after both instances of "Player-Speed 8", it waits a set amount of time, and then changes the speeds to something lower, like 4/-4 for each direction

Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 2:24 pm
by Emral
Ah, so what you can do there for example is have a variable called "slowdownTimer" defined at the start of your code:
local slowdownTimer = 0
Then there'd be basically three parts to using it. Activation, countdown and deactivation.
Activation happens in your onKeyDown. Though GENERALLY I would recommend not using onKeyDown and using player.keys directly. For example like this:
Countdown happens in onTick, and deactivation also happens in onTick, because you're polling for when the timer reaches a certain value.
Code: Select all
local slowdownTimer = 0
function onTick()
if slowdownTimer > 0 then -- if the timer is active, so to speak
slowdownTimer = slowdownTimer - 1
if slowdownTimer == 0 then-- the timer has just ended. Here is where you can slow down the speed.
-- do stuff
end
else -- if not in this state
if player.keys.down == KEYS_PRESSED then -- on the frame the player presses down...
player.speedX = 8 * player.direction
slowdownTimer = 120 -- 120 frames is just a bit less than 2 seconds. As an example. Setting this will cause the if statement in line 3 to swing around and start the countdown and also prevent this from being retriggered.
end
end
end
I put activation in an else here, to prevent repeat activation. But you can forego the else if you want repeat activation to be possible. Hope this makes sense.
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 2:47 pm
by Flarestripe
The Keys_Down part doesn't do anything.
Is there any part I was supposed to change, to make it register me hitting it?
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 2:57 pm
by Emral
It should work automatically. player.keys.down is the standard way to check if a button has been pressed/held/released. Can you screenshot your code again? Make sure to include all the lines if possible.
(Also I just realized I forgot the groundTouching check, oops!)
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 3:03 pm
by Flarestripe
I probably did something wrong, tbh
Idk what I did-

Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 3:42 pm
by Emral
Like I said, onKeyDown is best avoided. It's very old and you're better off using onTick and the player.keys variables. Also, it's player:isGroundTouching(). Notice the colon. Also, make sure slowdownTimer is defined somewhere above onTick. The line local slowdownTimer = 0 will do that.
player:isGroundTouching() is equivalent to player.isGroundTouching(player) btw. The colon makes the thing left of the colon be passed as the first argument to the function.
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 4:03 pm
by Flarestripe
Yeah, I always mess 1 or 2 things up like that- XD
Works fine now.
Imma work a few extra things into it, and then It'll be done.
Thank chu ^-^
(One last question: is it possible to make it where I'd have to, say, push down AND run or something to make it do the action, or is that too much?)
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 4:31 pm
by Emral
You can chain conditions in an if statement together. If you are merely concerned with whether both keys are held at the same time, you can omit the == KEYS_PRESSED to get the held state, rather than the pressing frame. Then it will be true when both keys are simultaneously held.
if player.keys.down and player.keys.run then ...
Re: How to check Player's direction?
Posted: Sat Aug 15, 2020 4:38 pm
by Flarestripe
Oh, oki.
Thank chu
Re: How to check Player's direction?
Posted: Tue Aug 18, 2020 9:41 am
by Hoeloe
Enjl wrote: ↑Sat Aug 15, 2020 3:42 pm
player:isGroundTouching() is equivalent to player.isGroundTouching(player) btw. The colon makes the thing left of the colon be passed as the first argument to the function.
Probably worth noting that the modern version of that function is player:isOnGround().
Re: How to check Player's direction?
Posted: Mon Aug 24, 2020 6:18 pm
by Lusho
Enjl wrote: ↑Sat Aug 15, 2020 1:54 pm
Sure, go ahead.
Generally though, it's all in luna.lua, and you define the timer at the top of your file and then access and manipulate it in onTick, because onTick runs every frame. The exact placement depends on what you want to do. For instance, you would only want to increment the timer when you are actually using it, which you can check for with another if statement and perhaps another variable that determines whether or not the timer should currently be active.
These player checking stuff should actually go in onTickEnd right? I used onTick and I noticed a bit of delay