0

After writing a few files for saving in my JSON file in Godot. I saved the information in a variable called LData and it is working. LData looks like this:

{
  "ingredients":[
    "[KinematicBody2D:1370]"
  ],
  "collected":[
    {
      "iname":"Pineapple",
      "collected":true
    },{
      "iname":"Banana",
      "collected":false
    }
  ]
}

What does it mean when the file says KinematicBody2D:1370? I understand that it is saving the node in the file - or is it just saving a string? Is it saving the node's properties as well?

When I tried retrieving data - a variable that is assigned to the saved KinematicBody2D.

Code:

for ingredient in LData.ingredients:
        print(ingredient.iname)

Error:

Invalid get index name 'iname' (on base: 'String')

I am assuming that the data is stored as a String and I need to put some code to get the exact node it saved. Using get_node is also throwing an error.

Code:

for ingredient in LData.ingredients:
        print(get_node(ingredient).iname)

Error:

Invalid get index 'iname' (on base: 'null instance')

What information is it exactly storing when it says [KinematicBody2D:1370]? How do I access the variable iname and any other variables - variables that are assigned to the node when the game is loaded - and is not changed through the entire game?

Clemens Tolboom
  • 1,872
  • 18
  • 30
Mana
  • 352
  • 5
  • 22
  • I've pretty print the JSON. Your code is accessing the wrong keys. Hope that helps. – Clemens Tolboom Jan 02 '20 at 13:50
  • Hi, sorry I rejected the edit by mistake. I will take a look at your suggestion and get back. Thanks. – Mana Jan 02 '20 at 23:36
  • Reedited ... you should use `LData.collected` to get to `iname` – Clemens Tolboom Jan 03 '20 at 18:43
  • Hi. I was trying to access a variable that is also called iname - inside the Kinematic body stored in the first array. I am not trying to access iname of collected data. How do I go about that? – Mana Jan 04 '20 at 03:12
  • `for ingredient in LData.ingredients: print(ingredient) # string` and `for ingredient in LData.ingredients: print(get_node(ingredient)` are ok ... for ingredient in `LData.collected: print(iname)` should get your the iname's – Clemens Tolboom Jan 04 '20 at 12:18
  • When I `print(get_node(ingredients))`, it returns a null object. When I `print(ingredients)` it prints strings of the KinematicBody2D. I am not concerned about the `iname` in `LData.collected` as I know how to access it. – Mana Jan 04 '20 at 21:56

1 Answers1

0

[KinematicBody2D:1370] is just the string representation of a Node, which comes from Object.to_string:

Returns a String representing the object. If not overridden, defaults to "[ClassName:RID]".

If you truly want to serialize an entire Object, you could use Marshalls.variant_to_base64 and put that string in your json file. However, this will likely bloat your json file and contain much more information than you actually need to save a game. Do you really need to save an entire KinematicBody, or can you figure out the few properties that need to be saved (postion, type of object, ect.) and reconstruct the rest at runtime?

You can also save objects as Resources, which is more powerful and flexible than a JSON file, but tends to be better suited to game assets than save games. However, you could read the Resource docs and see if saving Resources seems like a more appropriate solution to you.

rcorre
  • 6,477
  • 3
  • 28
  • 33
  • 1) Is there a method to convert the String to Object? All I want is the code to just recognize the existing Kinematic Object and refer it. 2) Could you please share the syntax to use the Marshalls.variant_to_base64? 3) I don't need to save the entire KinematicBody. I have tired the Resources method previously but I didn't really understand how to make it work using the documentation. Which method do you suggest to use to save data for a 2D Pixel RPG? I am currently using the JSON method you suggested in my previous question. – Mana Dec 28 '19 at 20:49