How to check Player's direction?

Post here for help and support regarding LunaLua and SMBX2's libraries and features.

Moderator: Userbase Moderators

Flarestripe
Spiny
Spiny
Posts: 29
Joined: Wed Jul 19, 2017 4:19 pm

How to check Player's direction?

Postby Flarestripe » Sat Aug 15, 2020 12:08 pm

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>

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9865
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: How to check Player's direction?

Postby Emral » Sat Aug 15, 2020 12:24 pm

player.direction is an integer either containing -1 (left) or 1 (right).
https://docs.codehaus.moe/#/reference/player

Flarestripe
Spiny
Spiny
Posts: 29
Joined: Wed Jul 19, 2017 4:19 pm

Re: How to check Player's direction?

Postby Flarestripe » Sat Aug 15, 2020 12:37 pm

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)

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9865
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: How to check Player's direction?

Postby Emral » Sat Aug 15, 2020 12:54 pm

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.

Flarestripe
Spiny
Spiny
Posts: 29
Joined: Wed Jul 19, 2017 4:19 pm

Re: How to check Player's direction?

Postby Flarestripe » Sat Aug 15, 2020 1:07 pm

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?

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9865
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: How to check Player's direction?

Postby Emral » Sat Aug 15, 2020 1:24 pm

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

Flarestripe
Spiny
Spiny
Posts: 29
Joined: Wed Jul 19, 2017 4:19 pm

Re: How to check Player's direction?

Postby Flarestripe » Sat Aug 15, 2020 1:43 pm

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)

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9865
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: How to check Player's direction?

Postby Emral » 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.

Flarestripe
Spiny
Spiny
Posts: 29
Joined: Wed Jul 19, 2017 4:19 pm

Re: How to check Player's direction?

Postby Flarestripe » Sat Aug 15, 2020 2:01 pm

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

Image

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9865
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: How to check Player's direction?

Postby Emral » Sat Aug 15, 2020 2:24 pm

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.

Flarestripe
Spiny
Spiny
Posts: 29
Joined: Wed Jul 19, 2017 4:19 pm

Re: How to check Player's direction?

Postby Flarestripe » Sat Aug 15, 2020 2:47 pm

The Keys_Down part doesn't do anything.
Is there any part I was supposed to change, to make it register me hitting it?

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9865
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: How to check Player's direction?

Postby Emral » Sat Aug 15, 2020 2:57 pm

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!)

Flarestripe
Spiny
Spiny
Posts: 29
Joined: Wed Jul 19, 2017 4:19 pm

Re: How to check Player's direction?

Postby Flarestripe » Sat Aug 15, 2020 3:03 pm

I probably did something wrong, tbh
Idk what I did-
Image

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9865
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: How to check Player's direction?

Postby Emral » Sat Aug 15, 2020 3:42 pm

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.

Flarestripe
Spiny
Spiny
Posts: 29
Joined: Wed Jul 19, 2017 4:19 pm

Re: How to check Player's direction?

Postby Flarestripe » Sat Aug 15, 2020 4:03 pm

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?)

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9865
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: How to check Player's direction?

Postby Emral » Sat Aug 15, 2020 4:31 pm

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 ...

Flarestripe
Spiny
Spiny
Posts: 29
Joined: Wed Jul 19, 2017 4:19 pm

Re: How to check Player's direction?

Postby Flarestripe » Sat Aug 15, 2020 4:38 pm

Oh, oki.
Thank chu

Hoeloe
Phanto
Phanto
Posts: 1465
Joined: Sat Oct 03, 2015 6:18 pm
Flair: The Codehaus Girl
Pronouns: she/her

Re: How to check Player's direction?

Postby Hoeloe » Tue Aug 18, 2020 9:41 am

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().

Lusho
Blooper
Blooper
Posts: 163
Joined: Wed Dec 25, 2019 11:39 pm
Flair: Intention is what matters
Pronouns: he/him

Re: How to check Player's direction?

Postby Lusho » Mon Aug 24, 2020 6:18 pm

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


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 3 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari