Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | HughMungus |
Hi,
I really hope someone can help. This problem is completely show-stopping for me.
I’m creating nodes with new(), then adding them to the hierarchy with add_child(), then using set_owner() after that.
However, the nodes never show up in the editor (hierarchy), no matter what I do.
I know that they are being created, because by using print_tree_pretty(), it shows them there.
Also, the created nodes are destroyed after the game is closed. They don’t stay in the hierarchy.
Here’s the link to download the example project so you can see for yourself:
https://www.dropbox.com/s/hjufss85461tanq/Add%20Child%20Example.zip?dl=0
(Press the “Enter” key to run the function that creates child nodes, then read the output for information. It will create a child every time you press “Enter”.)
If you don’t want to download that file, here’s the code from the project.
(Sorry for the formatting.)
The hierarchy Looks likes this:
–Main
----Holder
[Here’s the Main node’s script:]
tool
extends Node
var child_node : Node
const child_name : String = "Child"
var child_script : Script = preload("res://child_script.gd")
var holder_node : Node
const holder_name : String = "Holder"
func add_child_to_holder_node():
child_node = Node.new()
child_node.name = child_name
holder_node.add_child(child_node)
child_node.set_owner(self)
child_node.set_script(child_script)
print_tree_pretty()
func _ready():
holder_node = find_node(holder_name)
pass
func _input(event):
if event.is_action_pressed("ui_accept"):
add_child_to_holder_node()
[Here’s the child_node’s script:]
extends Node
var number_of_child_nodes : int
func _init():
number_of_child_nodes = get_parent().get_child_count()
print(" ")
print("child_script was successfully attached to created child_node.")
print("number_of_child_nodes = " + str(number_of_child_nodes))
print(" ")
Ok, I think I understand now. You want to add and remove nodes while the game runs (no problem) and to make this persistent (so save the scene during runtime)? (a bit of a problem)
This is a completely different story.
You can change scenes in the editor. Manually, or by tool scripts or by plugins/addons.
Then save the scene in the editor.
You normally can’t save changed scenes during runtime. Scenes are compiled before running and also often in some package. Also, scenes are usually in the resource (res://) path which is or can be read only on many platforms.
So if you want to add nodes to a scene while the game runs and make this persistent, the easiest way is to keep track of this in a data structure (typically a dictionary). Save this dictionary to i.e. a JSON file before the game ends and restore it either when the game runs the next time. You could probably also use a tool script to read and apply that file permanently with a tool script in the editor. (Just have to check that the user:// path where you would change such file normally is physically the same during runtime and in editor)
Maybe there also exist tools which read and write parts or entire scene trees from/to files or strings during runtime.
wombatstampede | 2019-03-17 16:06
Yes, I’m trying to have those changes be saved while the game is running.
So basically, it’s not possible.
I tested your child creation/deletion script, and it works great, but the user won’t have access that export variable toggle.
I tried to call that setter function manually upon a button press, and it simply does the same thing as it was doing before.
I’ll have to look into writing to and reading from files. I was afraid I would have to do something like that.
But It probably wouldn’t be efficient to write to file/reload scene/read from file every time I want if I want to be creating/destroying things.
I’ll have to rethink how to structure my project then.
Thanks a lot for your help.
HughMungus | 2019-03-17 19:19
Basically, reading and writing files isn’t very hard. And while you advance through programming knowledge you should master this.
And, it’s also in the “Fine Manual” (docs):
Saving games — Godot Engine (3.0) documentation in English
Just be aware that there might be differences in the user:// path (maybe also res://) between runtime and editor.
But what you want to do isn’t very typical for a game. That leads to the question if your basic approach is really the right way to go.
In your scenario the game user would have to have access to the whole project source files and the godot editor. This is less like a game and more like a game designer tool.
This would IMHO make more sense to run entirely in the editor. Then the right approach would be to make this an addon.
wombatstampede | 2019-03-18 06:53
Thanks again for your help and advice!
Someone pointed out to me that while the game is running, I can switch from Local debugging mode to Remote debugging mode.
I never noticed that option before, and remote debugging shows me all the items I create dynamically, which is great.
I wanted to be able to see if I was creating/destroying objects correctly, and Remote debugging allows me to do that.
Next I’ll work on getting saving and loading to work.
Thanks!
HughMungus | 2019-03-20 03:18