Handling player input from _input(event) vs NetworkTime.before_tick_loop.connect(_gather) #215
-
I've set up my player input similar to the forest brawl example, gathering inputs before tick loop, but I've ran into an issue where I need to detect |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The Relying on input events for this is not the best idea, since they are not tied to the network loop at all. What I've found to work well is input buffering - i.e. remember what the player has pressed, then reset your variables before the next tick. This way, you can use the input events to set your variables, but if the event is not fired at the exact right time, you still won't lose that input. Here's a very simplified version of what I'm currently using in a different game: extends BaseNetInput
class_name PlayerInput
# Input
var movement = Vector3.ZERO
var is_interacting = false
var is_building = false
var is_firing = false
var is_selling = false
var _samples = 0
func _ready():
super()
# Reset actions after every tick, so on-press things don't happen multiple times
NetworkTime.after_tick.connect(func(_dt, _t): _reset_actions())
# Full input reset after every tick loop
NetworkTime.after_tick_loop.connect(_reset)
func _process(_dt):
_samples += 1
movement += Vector3(
Input.get_axis("move_west", "move_east"),
0,
Input.get_axis("move_north", "move_south")
)
if Input.is_action_just_pressed("action_interact"):
is_interacting = true
if Input.is_action_just_pressed("action_build"):
is_building = true
if Input.is_action_pressed("action_attack"):
is_firing = true
if Input.is_action_pressed("action_sell"):
is_selling = true
func _gather():
movement /= max(_samples, 1)
movement = process_direction_input(movement)
func _reset():
movement = Vector3.ZERO
_samples = 0
_reset_actions()
func _reset_actions():
is_interacting = false
is_building = false
is_firing = false
is_selling = false I've removed a lot of code, but what happens is that I check the actual input multiple times between each network tick loop, and remember the values. Then, on _gather, I average them - this only happens with the movement, since it doesn't make sense with the action flags. You can do something similar, only instead of setting your action variables ( is_firing, is_selling, etc. ) in the _input event callback. It should work similarly well. |
Beta Was this translation helpful? Give feedback.
The
RollbackSynchronizer
node collects and submits input after every network tick. So, as long as your variables are set to the proper value at that point in time, you should be good.Relying on input events for this is not the best idea, since they are not tied to the network loop at all. What I've found to work well is input buffering - i.e. remember what the player has pressed, then reset your variables before the next tick. This way, you can use the input events to set your variables, but if the event is not fired at the exact right time, you still won't lose that input.
Here's a very simplified version of what I'm currently using in a different game: