I’m trying to figure out best way to create and pass some data. The code will create and delete a lot of instances of this data during runtime.
Example of the data:
class_name MoveInstance
var action_resource : Actions
var location_resource : Locations
var room_resource : Rooms
var isRoom = 0
var destination
I thought of these two options but they won’t work exactly how I want. Do you have any other suggestions?
Custom Resource: I’m not sure if it’s a good idea to create and remove a resource during runtime too many times.
Attached to a Scene (PackedScene): I can’t specify the custom class_name within a PackedScene on the editor, so editor won’t give me error if I use a wrong variable name with the instance of the packedscene.
This depends on what you’re doing with that data. You can use signals to pass data and interact with objects within a scene. Or you can use autoloads and get/set references to share data between characters and menus.
I think you probably need to specify what you want to do.
If you want to manage the created data somehow (keep track of how many, or delete by id,…) a dictionary makes sense if you have unique keys, e.g. by using ids (or unique enums), otherwise an array might work as well. Depends on what you’re trying to do.
if you have a set of more or less the same types of data, it might be a good idea to use exports and save these data resources (as .tres files). This way you can inspect them and set up the basic info.
Then you can use them as templates and modify when creating a specific instance.
e.g.
class_name MoveInstance
extends Resource
@export var action_resource: Actions
@export var location_resource: Locations
@export var room_resource: Rooms
var isRoom: int = 0
var destination: Vector2
This is just an example since I don’t actually know what you’re doing
Thank you for the answer. Creating resource sounds the best implemantaion so far but I’m still a bit skeptic as I’ll create and delete the instance of the resource a lot during runtime.
So I created a diagram to explain what I’m trying to do.
I’m trying to figure out how to manage the (let’s call it) DataBundled in the Action Button (ResourceB + Data1).
Everytime I click on the Button1, the DataBundled (ResourceB + Data1) will be created again. So that’s why I’m a bit skeptic about making it a resource. I don’t know if it’s something advisible or not. Apart from that, it’ll be useful because the variable and data types will be same, the values will be different only.
What do you mean by “freed” after click. None of us know what’s going on with your project, so it’s very hard to tell what you’re doing exactly. Are you just disabling the button? That can be solved with boolean true/false. Are your buttons using a state machine? That can be solved with if statements, switch statements or even custom variables that use classes. Are you spawning buttons all over the screen, that the player has to click and delete? Why are you deleting and respawning the same thing?