Invalid get index global position (on base previously freed) error

Godot Version
v4.1.2.stable.official

extends CharacterBody2D

@export var movement_speed = 40.0

@onready var planta = get_tree().get_first_node_in_group(“planta”)
@onready var sprite = $Sprite2D

func _physics_process(_delta):
var direction = global_position.direction_to(planta.global_position)
velocity = direction*movement_speed
move_and_slide()

if direction.x > 0:
	sprite.flip_h = false
	
elif direction.x < 0:
	sprite.flip_h = true

func _ready():
$lil.play(“move”)

func _on_area_2d_area_entered(_area):
if _area.is_in_group(“planta_area”):
$lil.play(“attack”)

func _on_area_2d_area_exited(_area):
$lil.play(“move”)

There are several plants in the project and when the enemy destroys the plant the project collapses

Previously freed means that the object you are getting the global position from was deleted previously (when you killed it/called queue_free() on it).

You can check if an object still exists with is_instance_valid(object)

func _physics_process(_delta):
    if is_instance_valid(planta):
        # movement code
    else:
        # code to find new planta 

Friend, thank you very much. I don’t know how to make the enemy look for another plant. I don’t know if you could help me with that?

The same way you did it before should work:

@onready var planta = get_tree().get_first_node_in_group(“planta”)

func _physics_process(_delta):
    if is_instance_valid(planta):
        # movement code
    else: 
        planta = get_tree().get_first_node_in_group(“planta”)

Hello, I wrote the code and all these errors appear
Invalid character ““” (U+201C).
Invalid character “”” (U+201D).
(STANDALONE_EXPRESSION):Standalone expression (the line has no effect)

Use" instead for your strings. For example: “planta”
I copied them from your example.

For what code does it show: (STANDALONE_EXPRESSION):Standalone expression (the line has no effect)?

EDIT: Not sure why this forum converts " to the others.

Now it’s fixed a little, it just keeps giving the error

(STANDALONE_EXPRESSION):Standalone expression (the line has no effect)

It says it is on line 20 and on line 20 is the move code address. I don’t know if that’s why.
I’ll leave you the code again just in case

@export var movement_speed = 40.0

@onready var planta = get_tree().get_first_node_in_group(“planta”)
@onready var sprite = $Sprite2D

func _physics_process(_delta):
var direction = global_position.direction_to(planta.global_position)
velocity = direction*movement_speed
move_and_slide()

if direction.x > 0:
	sprite.flip_h = false
	
elif direction.x < 0:
	sprite.flip_h = true
	
if is_instance_valid("planta"):
	direction
else:
	planta = get_tree().get_first_node_in_group("planta")

func _ready():
$lil.play(“move”)

func _on_area_2d_area_entered(_area):
if _area.is_in_group(“planta_area”):
$lil.play(“attack”)

func _on_area_2d_area_exited(_area):
$lil.play(“move”)

direction alone doesn’t do anything, hence the error.

It should look something like this (tabs/spaces are probably wrong because I copied your unformated code):

func _physics_process(_delta):
    # Run movement code if planta exists
    if is_instance_valid(planta):
       var direction = global_position.direction_to(planta.global_position)
       velocity = direction*movement_speed

       if direction.x > 0:
	      sprite.flip_h = false	
       elif direction.x < 0:
	       sprite.flip_h = true
       move_and_slide()
    # Find new planta if it doesn't exist anymore
    else:
          get_tree().get_first_node_in_group(“planta”)

The game no longer crashes when the enemy destroys the plant but the enemy doesn’t move so I put a move_and_slide on the else but the enemy doesn’t go towards the plant until it looks like it’s going to the opposite side.

    else:
          planta = get_tree().get_first_node_in_group("planta")
          print(planta)

What does it print in output? Did it find a new plant?

EDIT: It should be:
planta = get_tree().get_first_node_in_group("planta")

Your forgot planta =

And you don’t want to put move and slide in else, because you don’t want to move if there is not plant to walk to.

That’s because you didn’t find a new plant and you didn’t update the velocity of your character. When you call move_and_slide() it will use the velocity you last set. And in your case it was the direction to your last plant.

thank you very much the code now thank you very much for your patience

1 Like

Hey, no problem! Have fun with Godot! :slightly_smiling_face:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.