0 votes

When using:

var weapon = load("res://weapon.tscn")
var w = weapon.instance()
get_tree().get_root().get_node("Level/Player").call_deferred("add_child", w)

The game adds the node with weird version of the original name, instead of "weapon", imports it by "@weapon@8" and cause error due to some get_node() functions

When I try to use

get_node("Level/Player/@weapon@8")

in the previous function, it imports it with the name of "weapon" for some reason. There's a way to fix this? Thanks in advance.

Godot version 3.3.2
in Engine by (64 points)

1 Answer

0 votes
Best answer

Create an array of weapons or a weapon variable and a weapon pickup function in your Player script:

# Player.gd
var weapons = []
# OR: var current_weapon

func pickup_weapon(instance_of_weapon_to_pickup):
    call_deferred("add_child", instance_of_weapon_to_pickup)
    weapons.push_back(instance_of_weapon_to_pickup)
    # OR: current_weapon = instance_of_weapon_to_pickup

func get_last_picked_up_weapon():
    if not weapons.empty():
        return weapons.back()
    # OR: return current_weapon

Then your code becomes:

var weapon = load("res://weapon.tscn")
var w = weapon.instance()
get_tree().get_root().get_node("Level/Player").pickup_weapon(w)
by (3,389 points)
selected by
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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.