Invalid call. Nonexistent function 'hp_increase' in base 'Control (upgrade_menu.gd)'

Godot Version

Godot-4

Question

This error pops up every time when i chose that upgrade :
Invalid call. Nonexistent function ‘hp_increase’ in base ‘Control (upgrade_menu.gd)’.

Script

extends Control

@onready var player = $“.”
@onready var transition = $Transition

var upgrades = [
{“name”: "Fire Rate + 0.2 ", “description”: “Increase speed by 0.2”, “effect”: “fire_rate”},
{“name”: “Health + 20”, “description”: “Increase max health by 20”, “effect”: “health_increase”},
{“name”: “Damage + 5”, “description”: “Increase damage by 5”, “effect”: “damage_boost”}
]

var chosen_upgrades =

func _ready():
show_random_upgrades()

func show_random_upgrades():
chosen_upgrades =
var available_upgrades = upgrades.duplicate()

for i in range(3):
	if available_upgrades.size() > 0:
		var random_index = randi() % available_upgrades.size()
		chosen_upgrades.append(available_upgrades[random_index])
		available_upgrades.erase(available_upgrades[random_index])


for i in range(chosen_upgrades.size()):
	var button = $ButtonContainer.get_child(i)
	button.text = chosen_upgrades[i]["name"]
	button.pressed.connect(Callable(self, "_on_upgrade_selected").bind(chosen_upgrades[i]))

func _on_upgrade_selected(selected_upgrade):
match selected_upgrade[“effect”]:
“fire_rate”:
Fire_rate()
“health_increase”:
increase_health()
“damage_boost”:
boost_damage()

func Fire_rate():
print(“Speed boost applied!”)

func increase_health():
player.hp_increase()
get_tree().change_scene_to_file(“res://scenes/world.tscn”)

func boost_damage():
print(“Damage boosted!”)

Wheres the player in the scenetree relative to the upgrademenu?
You are setting the player to the upgrademenu itself here:

@onready var player = $“.”

how should i do it?

This depends on where the player is in relation to your upgrademenu.
How does your scenetree look like?

image

i guess in this case you can either use

@export var player: CharacterBody2D

and select the player when you go to the inspector.

or you have to do this:

@onready var player = $"../car_player"

I assume car_player is your player? The first solution is more flexible

when i try to do first solution it will show me just nodes from that scene

upgrade_menu scene

When you “export” a variable it becomes editable in the actual Inspector when the node is selected.

Since your UpgradeMenu.tscn is in the same scene as your player you do the following:

As herrspatan said above drop the export variable in your UpgradeMenu script:

@export var player: CharacterBody2D

Then save it.

Then go to the UpgradeMenu node in the tree and click it. Once clicked go to the Inspector and look for the “player” slot for you to drag and drop your car_player node into.

Now your UpgradeMenu script always has access to the player node

now this error pops up Invalid call. Nonexistent function ‘hp_increase’ in base ‘Nil’.

alr im tiredf of this nonstop errors im ending for today thanks for help you both

Does your player already exist in the scene or do you instantiate it?