Help with data handling

Godot Version

v4.2.2.stable.official [15073afe3]

Question

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 sounds like the best option. It’s much more taxing to create or remove nodes in the scene tree.

1 Like

Thank you for the answer. What do you think about creating a dictionary? Would it make sense?

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.

Example :

class_name Database

static var Hero_List = { "He-Man": HeroBase.new(), "She-Ra": HeroBase.new() }
class_name Character extends Node

@export var Actor_ID: String

var Current_Hero : HeroBase :
	get : return Database.Hero_List [Actor_ID]
	set(value) : Database.Hero_List [Actor_ID] = value

func Increase_Strength():
	Current_Hero.Attack += 10

1 Like

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 :wink:

1 Like

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.

Do you need to delete or reset data?

class_name DataManager

static var ReasourceA : BaseData = BaseData.new()
static var ReasourceB : BaseData = BaseData.new()

static func Reset_B_Data():
	ReasourceB = BaseData.new()

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?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.