Topic was automatically imported from the old Question2Answer platform.
Asked By
newgodot_user
even after searching up the forums, i still don’t know how to fix this error. it exists with everything line of code with “pl.()”
extends Node
#Have to reopen Project in Editor to take effect
class_name State
#onready var anmp = owner.get_parent().get_node("AnimationPlayer")
#onready var pl: Node = owner.get_parent() as KinematicBody2D
var pl: KinematicBody2D
#var fsm : StateMachine
onready var _states: Dictionary={}
var jump : bool
var dir: float
var next_state:State
func enter() -> void:
next_state=null
pl._animation(name)
#####We need a Fall state!!!
func logic() -> void:
get_input()
pl.move_player()
#If certain Input we go to transition
func get_transition() -> State:
return next_state
func get_input() -> void:
jump = Input.is_action_just_pressed("jump")
dir = Input.get_action_strength("right")-Input.get_action_strength("left")
pl.calc_physic(dir)
It look like that pl is not intanced.
I think that is why it says pl is “Nil”.
Maybe you can use parameter in those functions, and pass instanced object into those functions.
I mean, for example:
extends Node
class_name State
var pl: KinematicBody2D
onready var _states: Dictionary={}
var jump : bool
var dir: float
var next_state:State
func enter(plPath: String) -> void:
pl = get_tree().get_root().get_node(plPath)
if pl is KinematicBody2D:
next_state=null
pl._animation(name)
func logic(plPath: String) -> void:
pl = get_tree().get_root().get_node(plPath)
if pl is KinematicBody2D:
get_input()
pl.move_player()
func get_transition() -> State:
return next_state
# Suppose this only run after function logic call.
# Since in function logic we already got pl, don't need to get again.
func get_input() -> void:
jump = Input.is_action_just_pressed("jump")
dir = Input.get_action_strength("right")-Input.get_action_strength("left")
pl.calc_physic(dir)
Something like that, hope this help.
I think the key point is that if you want call those functions from other script, you need to pass a parameter in, for example function enter(plPath: String).
If the function don’t be called in other script, you don’t need to pass a parameter, for example function get_input(). Since In your code, function get_input() only be called by function logic(plPath: String) which is in the same script.