无效设置索引'weapon' (base: 'Spatial')的值为type"空间(weapon_manager.gd)"。
我的代码如下:
extends Spatial
# All weapons in the game
var all_weapons = {}
# Carrying Weapons
var weapons = {}
#HUD
var hud
var current_weapon #Reference to the current weapon extension???
var current_weapon_slot = "Empty" # The current weapon slot
var changing_weapon = false
var unequipped_weapon = false
func _ready():
hud = owner.get_node("HUD")
all_weapons = {
"Unarmed" : preload("res://Unarmed.tscn"),
"Pistol_A" : preload("res://pistol_a/pistol.gd"),
"Rifle_A" : preload("res://rifle_a/rifle_a.tscn")
}
weapons = {
"Empty" : $Unarmed,
"Primary" : $Pistol_A,
"Secondary" : $Rifle_A
}
# Initializing refernces for each weapon
for w in weapons:
if weapons[w] != null:
weapons[w].weapon_manager = self
weapons[w].player = owner
weapons[w].visible = false
# Set current weapon to unarmed
current_weapon = weapons["Empty"]
change_weapon("Empty")
# Disable process
set_physics_process(false)
func _process(delta):
if unequipped_weapon == false:
if current_weapon.is_unequip_finished() == false:
return
unequipped_weapon = true
current_weapon = weapons[current_weapon_slot]
current_weapon.equip()
if current_weapon.is_equip_finished() == false:
return
changing_weapon = false
set_process(false)
func change_weapon(new_weapon_slot):
if new_weapon_slot == current_weapon_slot:
current_weapon_slot.update_ammo()
return
if weapons[new_weapon_slot] == null:
return
current_weapon_slot = new_weapon_slot
changing_weapon = true
weapons[current_weapon_slot].update_ammo()
# CHanging weapons
if current_weapon != null:
unequipped_weapon = false
current_weapon.unequip()
set_process(true)
# Update HUD
func update_hud(weapon_data):
var weapon_slot = "1"
match current_weapon_slot:
"Empty":
weapon_slot = "1"
"Primary":
weapon_slot = "2"
"Secondary":
weapon_slot = "3"
hud.update_weapon_ui(weapon_data, weapon_slot)
您的weapon_manager.gd
文件似乎缺少一个名为weapon
的变量,因此不可能设置它。
另外,我找不到这个脚本文件中错误的来源;它可能起源于你代码中的其他地方。请确保您使用了正确的变量名,并尝试在正确的对象上设置值。