我是Godot的使用者,我一直在关注Godot教程,买Godot说

发布于 2025-02-09 06:08:43 字数 2420 浏览 2 评论 0原文

无效的设置索引'武器'(在基础上:'空间')具有类型的价值 '空间(auber_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)
                

Invalid set index 'weapon' (on base: 'Spatial') with value of type
'Spatial (weapon_manager.gd)'.

My code is below:

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)
                

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2025-02-16 06:08:43

您的auber_manager.gd文件似乎缺少一个称为武器的变量,因此将其设置为不可能。

另外,我找不到该错误在此脚本文件中的位置;它可能起源于您的代码中的其他地方。
确保您使用正确的变量名称,并尝试在正确的对象上设置值。

Your weapon_manager.gd file seems to lack a variable called weapon, and thus setting it is impossible.

Also, I can't find where the error would originate in this script file; it probably originated somewhere else in your code.
Make sure you're using the right variable names, and trying to set values on the right objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文