tts-community / tts-community-bug-tracker

Community maintained list of Tabletop Simulator bugs.
2 stars 0 forks source link

getComponent("Transform").get("localScale") access error #25

Open AufderWelt opened 4 years ago

AufderWelt commented 4 years ago

Describe the bug the call

obj.getComponent("Transform").get("localScale") 

generates an error, if the object does not contain lua code. the call works, if the obj contains some lua code, for example "--"

To Reproduce opend a new singleplayer table. put this code in the Global

function onLoad()
  spawnParams = {
    type = "Card",
    callback_function = function(obj)
      log(obj.getScale())

      Wait.frames(function()
        log( obj.getComponent("Transform").get("localScale") )
      end, 1)
    end
  }
  spawnObject(spawnParams)
end

Expected behavior the obj.getComponent("Transform").get("localScale") should work, without the object containing lua code

Tabletop Simulator Info:

Known workarounds if lua code are injected into the object, the call works

function onLoad()
  spawnParams = {
    type = "Card",
    callback_function = function(obj)
      log(obj.getScale())

      obj.setLuaScript("--")
      Wait.frames(function()
        log( obj.getComponent("Transform").get("localScale") )
      end, 1)
    end
  }
  spawnObject(spawnParams)
end

Additional context the call lua obj.getComponents()[1].get("localScale") does not work. it does not matter, if the object contains lua code or not

Link to TTS post https://forums.tabletopsimulator.com/showthread.php?8928-obj-getComponent(-quot-Transform-quot-)-get(-quot-localScale-quot-)-access-error&p=28926#post28926

Benjamin-Dobell commented 4 years ago

Alternate work-around You can get at this scale without going through the Component API by using:

local function getObjectTransformScale(obj)
    local rotation = obj.getRotation()
    local onesVector = Vector(1, 1, 1):rotateOver('z', rotation.z):rotateOver('x', rotation.x):rotateOver('y', rotation.y)
    local scale = obj.positionToLocal(obj.getPosition():add(onesVector))
    return scale
end
AufderWelt commented 4 years ago

@Benjamin-Dobell the scale needs to be divided by 1, or? to get the same scale vector, which you get by the call obj.getScale()

Benjamin-Dobell commented 4 years ago

@AufderWelt Yeah, sorry, what I posted was the inverse scale of localScale, as that's just what I happen to use more frequently. If you want to directly calculate localScale I believe the following ought to do it:

local function getLocalScale(obj)
    local rotation = obj.getRotation()
    local onesVector = Vector(1, 1, 1):rotateOver('y', -rotation.y):rotateOver('x', -rotation.x):rotateOver('z', -rotation.z)
    local scale = obj.positionToWorld(onesVector):subtract(obj.getPosition())
    return scale
end
Benjamin-Dobell commented 4 years ago

If you happened to grab that right after I posted, I've just reversed the order of the rotateOver calls, as I believe this is correct now.

Admittedly, I've not really tested this, so may want to give it a go on some objects with different scalars for x, y and z components of the scale.