Results 1 to 2 of 2

Thread: Why when using takeObject in a loop with a timeout it gives all the cards at once

  1. #1

    Why when using takeObject in a loop with a timeout it gives all the cards at once

    When using takeObject one at a time it shows the animation of moving the card to the zone, but when using it to take all the objects they all move at the same time, even though I added a small timeout after each takeObject.

    There are 4 zones to give cards to from all the objects (which right now is just a deck of cards)
    This is my test script I have right now.
    Code:
    function onLoad()
        zones = {
            c1 = getObjectFromGUID('91eaf0'),
            c2 = getObjectFromGUID('d4789f'),
            c3 = getObjectFromGUID('a1632b'),
            c4 = getObjectFromGUID('f5bfa1'),
        }
    
        dealCards()
    end
    
    function dealCards()
        for _, object in ipairs(getAllObjects()) do
            if (object.tag == 'Deck') then
                object.shuffle()
    
                for i = 1, object.getQuantity() do
                    for _, zone in pairs(zones) do
                        local x = object.takeObject({
                            position = zone.getPosition(),
                            rotation = Vector(zone.getRotation().x, zone.getRotation().y, 180),
                        })
    
                        Wait.frames(function()
                            log(x.resting)
                            Wait.time(|| x.resting, 0.5)
                        end, 10)
    
                    end
                end
            end
        end
    end
    I tried just Wait.frames alone, no change, added the Wait.time inside frames, again nothing changed.
    I also tried adding the whole takeObject into Wait with the same results.

    I'm wondering what am I doing wrong.
    Thanks

  2. #2
    I've just got my head around this same problem in my own script.

    You have to realise that Wait.frames() and Wait.time() DO NOT PUT A PAUSE INTO A LOOP. Wait statements do not have to complete their argument function for the Lua code to move on to the next instruction after the Wait.

    What they actually do is queue up the argument function to start after the wait time you specify INDEPENDANTLY of the loop. ( Wait statements might be better named QueueFunction statements. )

    So imagine your code executing as it is now -> the loop executes each iteration almost immediately after the previous one ( computers are fast! ). Each iteration of the loop hits the Wait.frame statement just milliseconds after the previous loop iteration. So what you end up with is ( if the loop executes 10 times for argument's sake ) telling TTS to execute 10 Wait.frame() statements ALL with the same time delay. This results in 10 cards moving ( almost ) simultaneously because the 10 Wait.frame statements don't actually pause the loop - they are just all queued up to execute outside the loop after the specified number of frames.

    What I did to get around this is create a local variable called "wait_time" which I started as 0.
    Then each pass through the loop add half a second to wait_time i.e. wait_time = wait_time +0.5

    Get rid of your Wait.frames mini-code from within the loop - it's not actually doing what you thought it would.

    Create a new Wait.time statement inside the loop that calls a single function that does ALL the work of getting and moving the card and delay this by "wait_time"

    So what will happen now is the loop still iterates to completion within milliseconds BUT the wait statements are queued up in increasing times so the first card will move after 0 seconds, the second after 0.5 seconds, the third after 1.0 seconds, the fourth after 1.5 seconds, etc.

    If that is too slow for you just decrease how much you increase wait_time within the loop.
    Last edited by groovyclam; 12-01-2020 at 05:40 PM.

Similar Threads

  1. For loop
    By Maris in forum Scripting Bug Reports
    Replies: 4
    Last Post: 05-11-2020, 02:08 PM
  2. [CONNECTION ISSUES] Disconnected: Timeout Server
    By purinblood in forum Technical Support
    Replies: 1
    Last Post: 08-23-2019, 10:35 PM
  3. Loop Logic Help
    By Tragic in forum Scripting
    Replies: 2
    Last Post: 03-24-2018, 08:20 AM
  4. Replies: 4
    Last Post: 10-02-2017, 03:34 PM
  5. Forum Session Timeout
    By dizneyguy in forum Forum & Website Help
    Replies: 2
    Last Post: 03-24-2016, 04:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •