0 votes

This keeps happening to me. I call a function from a node, and it returns null.
Here's a script, when the player does inputs (under #Input Attack) attspeed, attpower, or attdefense, it should change the color, (Via set_modulate) But it has to not be null. How can I (For lack of a better word) un-null it.

tool
extends KinematicBody2D
#Characteristics
export var hp = 1
export var damage = 1
#enum statuseffect {normal,damaged,cutting,poison,fire} 
#var priority = 0
export var speed = 1
export var power = 1
export var defense = 1
#Movement
export var playercontroler = 0
export var vel = Vector2()
export var gravity = 0
export var agility = 1000
#Children Nodes
onready var charspr = get_node("sprite")
#Effects
export var speedcolor = Color("07ff00")
export var powercolor = Color("ff0000")
export var defensecolor = Color("1300ff")
export var normalcolor = Color("ffffff")

func _ready():
    set_fixed_process(true)
    pass

func _fixed_process(delta):
    #Input
    var mover = Input.is_action_pressed(str(playercontroler) + "right")
    var movel = Input.is_action_pressed(str(playercontroler) + "left")
    var attspeed = Input.is_action_pressed(str(playercontroler) + "attackspeed")
    var attpower = Input.is_action_pressed(str(playercontroler) + "attackpower")
    var attdefense = Input.is_action_pressed(str(playercontroler) + "attackdefense")
    #Input X axis
    if (mover):#mover
        vel.x = agility
    elif (movel):
        vel.x = -agility
    else:
        vel.x = 0
    #Input Y axis
    vel.y += gravity
    #Input Attack
    if (attspeed):
        add_to_group("speed")
        if charspr != null:
            print("Modulate is working, not null instance")
            charspr.set_modulate(speedcolor)#WIP
#       add_to_group(priority)
    elif (attpower):
        add_to_group("power")
        if charspr != null:
            print("Modulate is working, not null instance")
            charspr.set_modulate(powercolor)
#       add_to_group(priority)
    elif (attdefense):
        add_to_group("defense")
        if charspr != null:
            print("Modulate is working, not null instance")
            charspr.set_modulate(defensecolor)
#       add_to_group(priority)
    #MovePlayer
    var motion = vel * delta
    move(motion)
#   print(vel)
    #Slide player
    if is_colliding():
        var normal = get_collision_normal()
        motion = normal.slide(motion)
        vel = normal.slide(vel)
        move(motion)
    pass
asked Apr 24, 2017 in Engine by lavaduder (264 points)
edited Apr 24, 2017 by lavaduder

1 Answer

+1 vote
Best answer

The error probably says your variable has no value (null), you need to assign one.

In this case, maybe the node called exactly "sprite" does not exist ("sprite"!="Sprite"), look at the tree, check the remote inspector, put a breakpoint and look at charspr value in process.

answered Apr 24, 2017 by eons (7,786 points)
selected Apr 24, 2017 by lavaduder

Hey your right, the node is exactly called sprite, but it's in another node.

onready var charspr = get_node("colid/sprite")

THANK YOU!

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.