Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | anssiko |
Hi,
I’m trying to wrap my head around the node-based state machine structure. I’m reverse engineering the “Finite state machine” demo and trying to define which direction to move the player in 2d with a local event for mobile touch input.
It seems that make_input_local is only available for use when extended to KinematicBody2d. Anywhere else in the tree it gives error “method not declared”.
(I have a usable version of player movement with all code in one file, but is is getting difficult to maintain with more states. Which is why I’m trying to split it to different files.)
Questions:
- What are the limitations on applying “make_input_local”? The docs are unclear on this.
- What would be the most simple node based state machine structure? A tutorial on this?
- Where and how in state machine should I define local event touch controls?
I’ll add more detailed information in the comments.
At the moment the input direction in the demo is defined with this (in motion.gd):
func get_input_direction():
var input_direction = Vector2()
input_direction.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
input_direction.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
return input_direction
My usable version of movement detects the direction of the touch from player, like this:
func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
if event.pressed:
var local_event = make_input_local(event)
if local_event.position.x > 20:
state = STATES.MoveRight
elif local_event.position.x < -20:
state = STATES.MoveLeft
elif round(local_event.position.x) in range(-19, 19):
if is_on_ladder:
state = STATES.Climb
elif not is_on_ladder:
state = STATES.Jump
else:
state = STATES.Idle
The node structure of the demo is:
KinematicBody2d
State machine
Idle
Move
Jump
The script structure of the demo is:
player_controller.gd (extends KinematicBody2d)
state_machine.gd (extends Node "StateMachine")
player_node_state_machine.gd (extends Node "StateMachine")
state.gd (extends Node "StateMachine")
motion.gd
on_ground.gd
idle
move.gd
on_air.gd
jump
anssiko | 2020-03-05 13:20