Script gives an error when switching from godot 3 to godot 4

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

For example, I get the error “invalid get index is_enemy” on characterbody2d on the script where the control mechanism of a 2d tank object is. all codes are below

  extends CharacterBody2D
@export var type = ""
@export  var speed = 70
@export var healt = 450
@export  var attack = 75
@export  var defence = 300
@export  var attack_speed = 2
@export  var vehicles = 200
@export var is_enemy = false
var dead = false
var enemy_list:Array


var moving = false

@onready var target = position

var velocitys = Vector2()
var on_click = false

func _ready():
	$smoke_animation.stop()
	$smoke.texture = null
	$fire_timer.wait_time = attack_speed
	$healtbar.max_value = healt
	if not is_enemy:
		$healtbar.set_progress_texture(preload("res://tank/images/progress2.png"))
	
func _input(event):

	if event is InputEventScreenTouch and event.pressed:

		if on_click:
			target = get_global_mouse_position()
			moving = true
			look_at(target)
			on_click = false
			$circle.visible = false
			$attack_circle.visible = false
			Global.selected = false
			
func _physics_process(delta):
	if healt <=0:
		$fire_timer.stop()
		dead = true
		$bomb_animation.play("bomb")
		
	
	
	velocity = position.direction_to(target) * speed
	
	
	if enemy_list.size() :
		$top.look_at(enemy_list[0].position)
		if $fire_timer.is_stopped():
			$fire_timer.start()
		
	else:
		$fire_timer.stop()
		$fire_animation.stop()
		$top/fire.visible = false
		
	$healtbar.value = healt
	
	if position.distance_to(target) > 3:
		set_velocity(velocity)
		move_and_slide()
		velocity = velocity
		if !$smoke_animation.is_playing():
			$smoke_animation.play("run")
	else:
		moving = false
		if $smoke_animation.is_playing():
			$smoke_animation.stop()
			$smoke.texture = null


func _on_KinematicBody2D_input_event(viewport, event, shape_idx):
	if event.is_pressed() and !Global.selected:
		on_click = true
		Global.selected = true
		$circle.visible = true
		$attack_circle.visible = true


func _on_Area2D_body_entered(body):
	if body.position != position:
		if body.is_enemy != is_enemy and body.type != "wall":
			enemy_list.append(body)
			#print("girdi")


func _on_Area2D_body_exited(body):
	if body.is_enemy != is_enemy and body.type != "wall":
		enemy_list.erase(body)
		$fire_animation.stop()
		$top/fire.visible = false

func _on_fire_timer_timeout():
	$top/fire.visible = true
	$fire_animation.play("fire")

func _on_fire_animation_animation_finished(anim_name):
	if !dead and enemy_list.size():
		enemy_list[0].healt -= attack
	$top/fire.visible = false


func _on_bomb_animation_animation_finished(anim_name):
	queue_free()
:bust_in_silhouette: Reply From: mcanton

I think this can happen when entering some other body that does not have attached this script. You could give this script a name by typing class_name ChooseAName extends CharacterBody2D at the beginning and replace in _on_Area2D_body_exited(body) method:

if body is ChooseAName and body.is_enemy != is_enemy...

thx my friend, working!

mydworfthink | 2023-05-13 22:03