tts-community / tts-community-bug-tracker

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

loading_custom is false whilst still loading #12

Open Benjamin-Dobell opened 4 years ago

Benjamin-Dobell commented 4 years ago

Describe the bug

When spawning a custom Deck or Card using spawnObjectJSON, when the callback_function is executed, the object should be in a valid state. However, loading_custom is incorrectly false despite the fact the card/deck is still not in its final state, as custom resources are still being loaded at this point.

To Reproduce

  1. Create a new Single Player game, dismiss the Load prompt to use the default empty scene.
  2. Open the Scripting editor and paste the Lua attached below into Global.
  3. Hit "Save & Play".

Expected behavior

Should see in Game chat tab:

Spawning: false Loading custom: true

Alternatively, if the resource really did load in one frame, the card object should be in its final ready to use state. Presently, that's not the case, it's still grey and the card's aspect ratio still reflects the default custom card aspect ratio.

Actual behavior

Instead seeing:

Spawning: false Loading custom: false

Lua Scripts:

Global:

function onLoad()
    local json = [[{
      "Name": "Card",
      "Transform": {
        "posX": 0,
        "posY": 3,
        "posZ": 0,
        "rotX": 0,
        "rotY": 180,
        "rotZ": 0,
        "scaleX": 1,
        "scaleY": 1,
        "scaleZ": 0.8571428571
      },
      "Nickname": "Test Card",
      "Hands": true,
      "CardID": 2111,
      "CustomDeck": {
        "21": {
          "FaceURL": "https://kb.tabletopsimulator.com/img/custom-deck/template-sheet.png",
          "BackURL": "https://kb.tabletopsimulator.com/img/custom-deck/template-sheet.png",
          "NumWidth": 10,
          "NumHeight": 7,
          "BackIsHidden": true,
          "UniqueBack": true
        }
      },
    }]]

    spawnObjectJSON({
        json = json,
        callback_function = function(object)
            print("Spawning: " .. tostring(object.spawning))
            print("Loading custom: " .. tostring(object.loading_custom))
        end
    })
end

Tabletop Simulator Info (please complete the following information):

Known workarounds

None. We have no way of knowing when the resource loading has actually finished.

In practice, if the load came from the cache, waiting not one, but two frames, after the callback_function has fired, seems to be enough time for the resources to loaded and applied. Although, that could easily be platform specific, in general this approach is far from reliable.

Nonetheless, the above only "works" for cached resources. When loading from the network, despite the fact loading_custom will be true in the callback_function, it's set to false too early. Even if we do something like:

Wait.condition(function()
    Wait.frames(function()
        -- Code you want to execute with a fully loaded card/deck
    end, 2)
end, function()
    return not object.spawning and not object.loading_custom
end, 10)

waiting 2, or even 3 frames isn't enough. At this point it's far too much of guessing game, and it's not likely to be consistent.

Additional context

Stack traces and screenshots: https://gist.github.com/Benjamin-Dobell/f4808ab5b3bee7068779a288f1268576