Dynamically adding items to an ItemList

Godot Versionhttps://forum.godotengine.org/t/separate-items-in-itemlist/1709

4.2

Question

I have an ItemList that is dynamically filled with names of files that I have on disk. For some reason those names get saved statically. Like… I can look into the editor and find those names even though the itemList should be empty. At runtime those turn into duplicate entries.

Can somebody help me out? What am I doing wrong?

2 Likes

Post your code maybe?

I think that might be just an editor thing.

I don’t know what code to post. I literally just add items in the ready method:

func _ready():
	io = My_IO.new()
	for item in io.load("res://files"):
		$ItemList.add_item(item)

I have a button to add a resource, but I’ve not been using it and items still get duplicated.

I should maybe add that I’m trying to code a plugin. So this class has the @tool annotation. I don’t know why that would make a difference though.

I should also add that I don’t know when the duplication happens. At some point duplicated items just turn up in the ItemList.

1 Like

What do you mean? How is that an editor thing?

I’ve never seen someone use this IO class directly. Maybe it loads the file and also the .import file that Godot generates?

Try using the DirAccess for iterating though files in a directory. Hope it helps.

2 Likes

The _ready function is called once every time the node is added to the node tree. Since the class has a @tool annotation, that means every time the scene is opened in the editor, in addition to when it is loaded by the actual game.

2 Likes

Sry if it was confusing I renamed the class to make it easier to read. The IO class is a custom class. It’s not actually named IO in my code.

1 Like

Sounds logical, but it doesnt explain why items are present in the editor.

no_items

I basically tracked down when the error happens. I just don’t understand why.

items

Whenever I restart Godot, the items will for some reason be part of the resource.

If the script is a @tool, it runs in the editor! So it adds the items once when you start up the editor and the second time when you run the game.

I’d say you can fix this by only loading the items if the array is empty. Or call .clear() on the array to empty it before each loading

I don’t use this class in the game. The plugin works kind of like Dialogic. I use the plugin to generate objects that I use in game. So I go to the plugin, do something without ever starting the game, then close Godot and for some reason when I restart Godot the items will be part of the resource as shown in the pictures.

The add_item function adds to an array and that array has the @export annotation. That’s why you can see it in the editor.

The thing is that exported variable values are serialized and saved as part of the scene. Remove the annotation and it will not save when you save the scene or exit the godot editor

1 Like

That sounds promising. I don’t exactly know how to remove that annotation though? Is there a flag in the editor?

I mean… ItemList is some kind of C extension right? I can’t just go into the code and remove the export annotation.

Or should I just call clear on startup everytime.

OH sorry I didn’t realize that $ItemList was the built-in node. I though it was your own node.

Then yes, you cannot do anything about it being exported. Clear the array before every load.

2 Likes

Thanks for your patience. Happy coding!!!

1 Like

Now that I’m thinking about it, it’s weird that the items only duplicated once. I would expect them to keep accumulating every time you open and close the editor. Unless you deleted them manually every time I guess.

1 Like

Good luck!

1 Like

They do keep on accumulating. I’m sorry if I wasn’t clear with my description.

2 Likes

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