Skip to content
This repository has been archived by the owner on Apr 8, 2021. It is now read-only.

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

Open
AufderWelt opened this issue Jun 24, 2020 · 4 comments
Open

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

AufderWelt opened this issue Jun 24, 2020 · 4 comments

Comments

@AufderWelt
Copy link

AufderWelt commented Jun 24, 2020

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:

  • OS: Windows
  • Version 12.4.3

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
Copy link
Member

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
Copy link
Author

@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
Copy link
Member

Benjamin-Dobell commented Jul 27, 2020

@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
Copy link
Member

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants