Invalid get index

Godot Version###

4.2

Question###

for some reason, when I try and use my code. it tells me that invalid get index(“ammo”) but the resource it is getting from has the correct name.
if anyone needs me too.

extends Node3D

class_name weapons_manager
signal Update_WeaponStack

var current_weapon = null
var WeaponStack =
var weapons_list = {}
var Next_Weapon: String
var weaponIndicator = 0
@export var _weapon_resources: Array[Weapon_resource]
@export var Start_Weapons: Array[String]
@onready var weapon_cast := get_node(“%RayCast3D”)
@onready var Anim_player := get_node(“%AnimationPlayer”)

#gets weapons.
func initialize(_Start_Weapons: Array):
for Weapons in _weapon_resources:
Weapons.ready()
weapons_list[Weapons.weapon_name] = Weapons

	for child in Start_Weapons:
		WeaponStack.push_back(child)
	current_weapon = weapons_list[WeaponStack[0]]
	Update_WeaponStack.emit(WeaponStack)
	enter()

func enter():
Anim_player.queue(current_weapon.equip_anim)

func exit(_next_Weapon: String):
if _next_Weapon != current_weapon.weapon_name:
if Anim_player.get_current_animation():
Anim_player.queue(current_weapon.equip_anim)
Next_Weapon = _next_Weapon

func Change_Weapon(weapon_name: String):
current_weapon = weapons_list[weapon_name]
Next_Weapon = “”
enter()

func _input(event):
if event.is_action_pressed(“weapon_up”):
pass

if event.is_action_pressed("weapon_down"):
	pass

if event.is_action_pressed("shoot1"):
	shoot()

func shoot():
if current_weapon.ammo != 0:
if !Anim_player.is_playing():
Anim_player.play(current_weapon.shoot_anim)
current_weapon.ammo -= 1
if weapon_cast.is_colliding():
var collider = weapon_cast.get_collider()
hit_damage(collider)

func hit_damage(collision):

if collision.is_in_group("test"):
	print ("test")
if collision.is_in_group("door_handle"):
	print("door_handle")
if collision.is_in_group("enemy"):
	print("enemy")
if collision.is_in_group("test_enemy"):
	print("test_enemy")

can you show the script of Weapon_resource ?

1 Like

Yeah I got u.
extends Resource
class_name Weapon_resource

@export_group(“Weapon Animations”)
@export var weapon_name: String
@export var shoot_anim: String
@export var reload_anim: String
@export var equip_anim: String
@export var dequip_anim: String

@export_group(“Weapon Stats”)
@export var damage: int
@export var ammo: int
@export var reserve_ammo: int
@export var max_ammo: int
@export var magazine_size: int

which resource/script has this ready() method?

1 Like

Just realized that the gun resource does not have that. that might be why. Thank you. I’ll let you know if my problem isn’t fixed!

the gun manager does. ammo seems to still not work.

what gun manager do? it .new() the resource?

Basically, the gun manager does all the weapon actions, if that makes sense. The gun manager handles changing weapons, shooting weapons, reloading, as well what weapons are inventory, and dropping.

It’s tough to help you. Try to format the code with the </> tool. Also, include the code for the resources.
In your code, indicate which line (use a remark) is the one that is causing the error.

Try to help people to help you!

Hey man, I know im late on this but, I’ve got the code figured out and I’m doing a lot better at coding now. thanks for trying to help!

1 Like