Save and Load as files

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Serenade
:warning: Old Version Published before Godot 3 was released.

Hey!
How can you make save(export as a tscn or any other type) scene or multiply scenes and load then when, lets say, you start the game?

:bust_in_silhouette: Reply From: keke

I’m not sure I understand exactly what you’re asking, but I think this may help you:
http://docs.godotengine.org/en/stable/tutorials/engine/saving_games.html?highlight=save

I just tried that tutorial but what i got are errors, godot cant find save() and to_jason()…
What i meant is, i want to be able to export(save) nodes as files, like, you create it in godot, save it and then you can load it (all in simulation, not from code, like, load game, but for objects)… :slight_smile:

Serenade | 2017-03-16 20:01

:bust_in_silhouette: Reply From: M. Alkotob

I think this is what you are looking for:

https://forum.godotengine.org/903/how-to-save-a-scene-at-run-time

**Edit: **There’s another thing I should’ve mentioned.

The above 2 answers (by keke and me) are for saving a scene inside the code.

If you have a node in the editor that you want to save and instance later, you can right-click and press “Save Branch as Scene”.
This will save as nodeName.tscn so you can instance it from the code as many times as you want.

The link in my answer is best used for saving a level that changes dynamically, such as randomly generated levels. For any normal level, you want to use Save Branch as Scene.

Thanks, ill try this one out too :slight_smile:

Serenade | 2017-03-16 20:02

There’s another thing I should’ve mentioned.

The above 2 answers (by keke and me) are for saving a scene inside the code.

If you have a node in the editor that you want to save and instance later, you can right-click and press “Save Branch as Scene”.
This will save as nodeName.tscn so you can instance it from the code as many times as you want.

The link in my answer is best used for saving a level that changes dynamically, such as randomly generated levels. For any normal level, you want to use Save Branch as Scene.

M. Alkotob | 2017-03-17 07:24

I tried your link,saving worked, but i cant manage to change name on export, like, if there is a file with same name, add some sort of word or number to it… and i dont know how can i make it to recognise files in the folder and load them all, instead of giving a direct link to each specific file… :frowning:

Serenade | 2017-03-17 09:35

Can you try sending a pic of your node tree?

I’m not really sure what your goal is, so if you send me a pic I could suggest the best way to do it.

Edit: Usually saving a branch means you can “instance” it later to make copies, so if you have one monster scene you can make 10 identical ones and place them in different positions.

There’s an instance button next to the + icon that lets you add multiple “instances” of the same scene.

That’s another thing to keep in mind, but please send me a picture so I can better understand what you’re trying to accomplish.

M. Alkotob | 2017-03-17 09:36

I, while learning, changed the code 100x… (im now trying the code from…
RecExBuild - Pastebin.com …(that is not working too)
So i dont have the code that i had while replying to you…

What i have in the main scene is a plain node and added scene that consists of a Node2D-Sprite(packed scene)… and thats all…
ONode
–ONode2D(scene)

I guess you can imagine it like a card game, where you can make your own cards, with input.
So i want it to save added input for each card

Serenade | 2017-03-17 13:15

Ok now things make more sense.

Now I will reply to this problem of yours:

I tried your link,saving worked, but i cant manage to change
name on export, like, if there is a file with same name, add some sort
of word or number to it… and i dont know how can i make it to
recognise files in the folder and load them all, instead of giving a
direct link to each specific file… :frowning:

Basically I think you should have a file with a fixed name, like list_of_cards.tscn, and put all cards in it and load/save this only. This should load all the cards instanced inside the parent node.

Another thing you could do is just save the file names of the individual cards in a file separately, or even the number of cards and have them loaded use “card”+str(i)+“.tscn” where i goes from 0 to number of cards.

I’d go with the first method and then try 2 & 3 if it doesn’t work.

M. Alkotob | 2017-03-17 15:31

I see…
What are those plus sign in +str(i)+ ?
Like in ------ path = “res://”+folder+“/”+i+“.”+ext -------- too?

Serenade | 2017-03-17 16:13

Yes that’s if you want to save each card as it’s own scene, but like I said I suggest you try saving a parent node with all cards inside it as that would be easier.

M. Alkotob | 2017-03-17 16:14

How can you check, has that node pack already loaded or not or Is it in the scene already, though?

Serenade | 2017-03-17 17:57

You can try:
if(get_node(“List of Cards”) != null):
do things

you can substitute “List of Cards” with a longer path like “Main/Game/List of Cards”

get_node() can cause problems (although I personally use it with no problems) so you might want to try has_node() instead, like this:

if(has_node("List of Cards"):
do things

I don’t use has_node() myself but it’s supposed to be the better way.

M. Alkotob | 2017-03-17 18:14

Trying this, actually i got error when i used “get_node” because, before i loaded the instance, there was no node with the name, but “has_node” worked like a charm :slight_smile:

Serenade | 2017-03-17 21:29

Glad to know things worked out :slight_smile:

M. Alkotob | 2017-03-18 12:06