How update only one element in nakama storage with Godot GDScript? It is possible?

Hello guys, I hope you can help me. I am trying to migrate my logic to change my backend in php and mysql (WebService) to nakama. I have my project with Godot in GDScript. But I’m testing to see if all the logic I have would work for me, in nakama. And I find a problem. I put you in situation. My game the user has, several creatures, with which he will fight against other users. The user acquires these creatures. I want to store those creatures in the DB(Cockroach). I am using docker and nakama. So, I add these creatures with this function…

func write_creature_async(creatures := []) -> void:
yield(Online.nakama_client.write_storage_objects_async(
		Online.nakama_session,
		[
			NakamaWriteStorageObject.new(
				"all_creatures",
				"creaturesUser",
				ReadPermissions.OWNER_READ,
				WritePermissions.OWNER_WRITE,
				JSON.print({creatures = creatures}),
				""
			)
		]
	), "completed")

And creatures is this:

func _on_Button_button_down():
	var creatures := [
		{id = 0, creatureName = "Floosty", level = 2, experience = 0, max_exp = 25, max_hp = 30, atack1 = "Placaje"},
		{id = 0, creatureName = "Floosty", level = 3, experience = 0, max_exp = 25, max_hp = 30, atack1 = "Placaje"},
	]
	
	yield(write_creature_async(creatures), "completed")

estos datos aparecen en nakama así:

{
  "creatures": [
    {
      "atack1": "Placaje",
      "creatureName": "Floosty",
      "experience": 0,
      "id": 0,
      "level": 2,
      "max_exp": 25,
      "max_hp": 30
    },
    {
      "atack1": "Placaje",
      "creatureName": "Floosty",
      "experience": 0,
      "id": 1,
      "level": 3,
      "max_exp": 25,
      "max_hp": 30
    }
  ]
}

The problem comes when I try, for example, to change a stat of a single creature. To begin with, I can’t find a way to add an autoincremental ID (I’ve created a function to get out of trouble). And then, I can’t find how I could run update the stats of a single creature. The only option I see is to do a get of all the creatures, add them to an array, search the array for the creature I want to modify, and then do a write_storage_objects_async again adding all the creatures again. It is possible that this cannot be done as it is normally done in a DB with an INSERT or an UPDATE or a DELETE of a creature by id. But it seems strange to me and I can’t find any other options. Thank you!

Hi @sergisnt welcome to the forum.

For creature ID I would suggest you generate a UUID rather than use an auto-incrementing integer.

As for updating a single creature’s stats, you might want to consider storing each creature that belongs to a player in a separate storage object.

For example, you could have a "Creatures" collection in Nakama Storage Engine and store each creature belonging to a player inside that collection, keyed by the creature’s UUID.

You can then use the storage engine’s List API to retrieve all creatures that belong to a user (Nakama: Godot | Heroic Labs Documentation).

Once you have the creatures, it is then easy enough to make a change to a single creature and update it using the Write API (Nakama: Godot | Heroic Labs Documentation).

I hope this helps.

1 Like

Do you mean to add the creatures collection and in the Key parameter add the ID of the creature? and have something like this?

On the other hand, there is an already defined way to generate that UUID. Any existing function? Or do I generate the function and manually add the id to the key parameter?

And thanks for your support :smiley:

PS: Do you have any network or way to have a faster chat? Discord or similar?

Yes that’s correct, where the Key Name is the ID of the creature. I would recommend using a UUIDv4 but any unique key will do. You may need to find a GDScript implementation of UUIDv4 as I don’t believe it’s part of GDScript as standard.

The forums are the best way to reach us with questions. :slight_smile:

1 Like