0 votes

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)
Godot version V.3.2.3
in Engine by (40 points)

1 Answer

0 votes

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 getinput(). Since In your code, function getinput() only be called by function logic(plPath: String) which is in the same script.

by (526 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.