Radoan
Radoan

Reputation: 1

Spawning an Object with State TTS

I would like to combine two and more objects in the TabletopSimulator. Wenn I spawn the objects I can combine like this page https://kb.tabletopsimulator.com/host-guides/creating-states/. I would like this create with Lua. So I need help... I spawn the objects like here, but I didn`t get two objects with 2 states.

function SpawnLevel1(Obj1, ID)
        CID = ID
        spawnparamslvl = {
            type              = 'Custom_Assetbundle',
            position          = Obj1.getPosition(),
            rotation          = Obj1.getRotation(),
            scale             = {x=1, y=1, z=1},
        }
        paramslvl = {
            assetbundle = data[CID].assetbundle,
            type        = 1,
            material    = 0,
        }
        Obj2 = spawnObject(spawnparamslvl)
        obj_name = data[CID].display_name
        Obj2.setDescription(obj_name)
        Obj2.setName(obj_name)
        Obj2.setCustomObject(paramslvl)
        Obj1.addAttachment(Obj2).SetState(1)
    end

function deploy(PID)
  display_name      = data[PID].display_name
  spawnparams = {
      type              = 'Custom_Assetbundle',
      position          = self.getPosition(),
      rotation          = self.getRotation(),
      scale             = {x=1, y=1, z=1},
  }
  params = {
      assetbundle = data[PID].assetbundle,
      type        = 0,
      material    = 0,
  }
    Spawning(spawnparams, params, display_name, PID)
end

function Spawning(spawnparams, params, display_name, PID)
    Obj1 = spawnObject(spawnparamsmain)
    ID = PID
    Level1 = SpawnLevel1(Obj1, ID)
    Obj1.setCustomObject(paramsmain)
    Obj1.setName(display_name)
end

Thank you for your help Radoan

Upvotes: 0

Views: 1111

Answers (1)

ikegami
ikegami

Reputation: 386706

You have to use spawnObjectData or spawnObjectJSON. The format of the "object data" follows the format of the save file.

Rather than hardcoding the large data structures needed to build an object, I'll use existing objects as templates. This is a common practice that's also reflected by the following common idiom for modifying an existing object:

local data = obj.getData()
-- Modify `data` here --
obj.destruct()
obj = spawnObjectData({ data = data })

The following are the relevant bits of "object data" for states:

{ -- Object data (current state)
   -- ...
   States = {
      ["1"] = { -- Object data (state 1)
         --- ...
      },
      ["3"] = { -- Object data (state 3)
         -- ...
      },
      ["4"] = { -- Object data (state 4)
         -- ...
      }
   },
   -- ...
}

So you could use this:

function combine_objects(base_obj, objs_to_add)
   if not objs[1] then
      return base_obj
   end

   local data = base_obj.getData()

   if not data.States then
      data.States = { }
   end

   local i = 1
   while data.States[tostring(i)] do i = i + 1 end
   i = i + 1  -- Skip current state.
   while data.States[tostring(i)] do i = i + 1 end

   for _, obj in ipairs(objs_to_add) do
      data.States[tostring(i)] = obj.getData()
      obj.destruct()
      i = i + 1
   end
   
   base_obj.destruct()
   return spawnObjectData({ data = data })
end

Upvotes: -1

Related Questions