Godot Version
Godot 4.4
Question
Packedscene won't register as packedscene when trying to check for new dictionary key:
I’m trying to make a dictionary that holds different packedscenes
of bullets I’ve made for a main project I’m working on.
Issue:
the problem is that when I’m using the built in function I made (add_to_bullet_dictionary(key_name:String, _preload:PackedScene)
)
for some reason the packedscene I’m trying to add doesn’t register as one in the code, but shows in a error print i made as a packed scene:
Code below:
extends Node
class_name bullet_container
var global_dict: Dictionary = {}
extends CharacterBody2D
class_name bullet_logic
@export_group("physics")
@export var speed:int
@export var damage_over_time:int
@export var raw_damage: int
func _physics_process(_delta: float) -> void:
move_and_slide()
func add_to_bullet_dictionary(key_name:String, _preload:PackedScene):
if _preload != PackedScene:
push_error("preload is not a packscene but is: ", _preload)
if _preload == null:
push_error("_preload is null")
# adds key to dictionary when _preload is a packscene and key_name is not found in dict:
if not BulleftDict.global_dict.has(key_name) and _preload == PackedScene:
BulleftDict[key_name] = _preload
extends bullet_logic
var bullet_name = "purple bullet"
@onready var _preload:PackedScene
func _ready() -> void:
_preload = preload("res://Scene's/Bullets/purple_bullet.tscn")
add_to_bullet_dictionary(bullet_name, _preload)
print(BulleftDict.global_dict)
func _on_area_2d_mouse_entered() -> void:
queue_free()
print("mouse is in box")
With !=
you are comparing between instances, to type check you should use is not
instead i.e. _preload is not PackedScene
.
I’ll try this and see if there’s a different outcome, I appreciate the input.
So I tweaked the code and now I’m getting an old error that pop-up before I started checking to see if the node was a packed scene. And that’s the invalid assignment to property
error.
I know that it occurs when the type you’re trying to put through is wrong, but everything points to the item still being a packedscene
so it’s somewhat confusing.
Code below:
extends CharacterBody2D
class_name bullet_logic
@export_group("physics")
@export var speed:int
@export var damage_over_time:int
@export var raw_damage: int
func _physics_process(_delta: float) -> void:
move_and_slide()
func add_to_bullet_dictionary(key_name:String, _preload:PackedScene):
if _preload is not PackedScene:
push_error("preload is not a packscene but is: ", _preload)
if _preload == null:
push_error("_preload is null")
if not BulleftDict.global_dict.has(key_name) and _preload is PackedScene:
BulleftDict[key_name] = _preload
extends bullet_logic
var bullet_name = "purple bullet"
@onready var _preload:PackedScene
func _ready() -> void:
_preload = load("res://Scene's/Bullets/purple_bullet.tscn")
var _load = _preload.instantiate()
add_to_bullet_dictionary(bullet_name, _preload)
print(BulleftDict.global_dict)
func _on_area_2d_mouse_entered() -> void:
queue_free()
print("mouse is in box")
So I figured out that no matter what value BulletDict.global_dict[key_name]
is supposed to equal it will show the invalid assignment error.
For example:
BulletDict.global_dict[key_name] = raw_power
This would give me the same error except instead of saying packedscene
it would say int.
The problem was that I wasn’t using the built in dictionary, but the global script to try and add a key.
all I had to do was put BulletDict.global_dict
instead of just BulletDict
when adding the key into the dictionary.
Thank you for trying to help @constanze3 on this very simple problem lol, I hope you have a good day.