Godot cannot recognize my variable as an argument

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Decisive

Basically i have a variable named current group, and another four variables named: right, left, down, up. And then everytime a button is pressed i remove from the current group and set it as the new one for that action, but godot give me the error: “Invalid type in function ‘remove_from_group’ in base ‘KinematicBody2D (UWU.gd)’. Cannot convert argument 1 from Nil to String.”

Here’s the code

var current_group = down

#Variables that store the groups
var right = “LR”
var left = “LL”
var up = “LU”
var down = “LD”

func _ready():
self.add_to_group(“LD”)

if self.is_in_group("LD"):
	print("it works mate!")
if self.is_in_group("LR") and self.is_in_group("LD"):
	print("works here too")

func get_input():
velocity = Vector2.ZERO
if Input.is_action_pressed(‘right’):
self.remove_from_group(current_group)
current_group = right
self.add_to_group(right)
velocity.x += 1
if Input.is_action_pressed(‘left’):
velocity.x -= 1
self.remove_from_group(current_group)
current_group = left
self.add_to_group(left)
if Input.is_action_pressed(‘down’):
velocity.y += 1
self.remove_from_group(current_group)
current_group = down
self.add_to_group(down)
if Input.is_action_pressed(‘up’):
velocity.y -= 1
self.remove_from_group(current_group)
current_group = up
self.add_to_group(up)

:bust_in_silhouette: Reply From: timothybrentwood

down isn’t defined by the time current_group is set to down (runs top to bottom) so current_group is set to a Nil value, or “nothing” in layman’s terms. Make this change to your _ready() function and it should work:

func ready():
    self.addto_group("LD")
    current_group = down

    if self.is_in_group("LD"):
        print("it works mate!")
    if self.is_in_group("LR") and self.is_in_group("LD"):
        print("works here too")

The reason it worked at your print statements because you weren’t using the variable current_group