Adding new objects to a collection/key

Hello!
Im currently messing with storage and trying to add a new item to an already existing one.
I have 2 functions for this purpose, populateInventory to populate it with 3 objects and spawnBuilding that its supposed to add a 4th one.

So far I havent managed to add the new building, tryied inserting with no success, it seems to overwrite the whole table with the thing to insert.

function PopulateInventory_rpc(context, payload)
  local table = { building1 = {}, building2 = {}, building3 = {} };
  table.building1["name"] = "Main Factory"
  table.building1["id"] = "0"
  table.building1["positionX"] = "0"
  table.building1["positionZ"] = "0"
  table.building1["orientation"] = "1"

  table.building2["name"] = "Attack Tower"
  table.building2["id"] = "1"
  table.building2["positionX"] = "0"
  table.building2["positionZ"] = "2"
  table.building2["orientation"] = "1"

  table.building3["name"] = "Attack Tower"
  table.building3["id"] = "1"
  table.building3["positionX"] = "0"
  table.building3["positionZ"] = "-2"
  table.building3["orientation"] = "1"

  local new_objects = {
    { collection = "baseLayout", key = "buildings", user_id = context.user_id, value = table, permission_read = 1, permission_write = 1 }
  }
  nk.storage_write(new_objects)
  return nk.json_encode({ table })
end


function SpawnBuilding_rpc(context, payload)
  --hardcoded values in order to insert into the player's inventory
  local name = "Defense Tower"
  local id = "2"
  local positionX = "0"
  local positionZ = "0"
  local orientation = "1"
  --populate a table with the values to insert
  local contentToInsert = { ["building4"] = { ["name"] = name, ["id"] = id, ["positionX"] = positionX, ["positionZ"] = positionZ, ["orientation"] = orientation }
  }
  --object_ids to be retrieved
  local object_ids = {
    { collection = "baseLayout", key = "buildings", user_id = context.user_id }
  }
  --reading the player's inventory
  local objects = nk.storage_read(object_ids)

  table.insert(objects[1].value, contentToInsert)
  return nk.json_encode({ objects[1].value })
end

When I get the json_encode I get only the values from contentToInsert, while trying to read objects instead of contentToInsert loads the previously stored ones.

Is there a simplier way to do this? What is exactly failing here?

1 Like

When I get the json_encode I get only the values from contentToInsert

It looks like the rpc is only returning the values you just inserted. So I wouldn’t expect the client to see any other storage objects:

  table.insert(objects[1].value, contentToInsert)
  return nk.json_encode({ objects[1].value })

while trying to read objects instead of contentToInsert loads the previously stored ones.

Are you saying then when you make another call to read the storage objects from the client, you don’t see the inserted content? That would be because you aren’t re-storing the inserted table. So if you add a:

nk.storage_write(objects)

call after your table.insert(objects[1].value, contentToInsert), it will make sure the inserted content is stored in our storage engine.