Results 1 to 4 of 4

Thread: Scripting Question: onPlayerTurnStart Script drawing 2 instead of 1?

  1. #1

    Scripting Question: onPlayerTurnStart Script drawing 2 instead of 1?

    Hi, sorry if this is in the wrong place, new here. I have a question about implementing the onPlayerTurnStart function. (Note, I'm new to LUA and picking it up as I go. I've looked through the documentation, but can't find an answer.)

    Basically, I'm trying to make a script where a player draws 1 card from their own specific deck automatically at the start of their turn. This is what I have so far(Player 1 is White, Player 2 is Blue):

    function onPlayerTurnStart(player_White_start, player_Blue_previous)
    if Turns.turn_color == 'White' then
    DrawP1()
    else if Turns.turn_color == 'Blue' then
    DrawP2()
    end
    end
    end

    DrawP1 and DrawP2 are functions where the player draws from their specific deck as long as it has cards:

    function DrawP1()
    local objDeck = getDeck(DeckZoneGUID_1)
    if not(objDeck == nil) then
    objDeck.dealToColor(1, Color_1)
    end
    end


    function DrawP2()
    local objDeck = getDeck(DeckZoneGUID_2)
    if not(objDeck == nil) then
    objDeck.dealToColor(1, Color_2)
    end
    end

    When the draw functions are linked to a button, they work perfectly, and a player draws 1 card from their deck when the button is pressed. However, with the onTurnPlayerStart function, it's drawing 2 cards instead of 1.

    Any ideas? Help very appreciated.

  2. #2
    It's a bug I think, turn events triggering twice. A workaround for now would be to ignore when they fire too rapidly (e.g. faster than 0.5s apart), it would be something like
    Code:
    -- should we ignore the next call?
    local ignore = false
    
    function onPlayerTurnStart(start, prev)
        if not ignore then
            
            -- your typical stuff there
            -- (what you had in this function before)
            
            -- ignore the next call
            ignore = true
            -- reset the ignore variable after half a second
            Wait.time(function() ignore = false end, 0.5)
        end
    end

  3. #3

    Another way, not better, but trades a little memory for a little speed.

    I would keep a variable, (lastPlayer) and only deal a card if newPlayer ~= lastPlayer.

    Code:
    -- first two lines just set up context
    decks = {White = getDeck(DeckZoneGUID_1), Blue = getDeck(DeckZoneGUID_2)}
    lastPlayer = nil
    
    function onPlayerTurnStart(old, new)
    if new ~= lastPlayer then
        decks[new].deal(1, new)
        lastPlayer = new
    end
    Last edited by cche; 07-31-2018 at 01:23 PM. Reason: oversight

  4. #4
    This bug was reported two years ago and is still not fixed!??

Similar Threads

  1. In Hotseat function onPlayerTurnStart() goes twice
    By olejika in forum Scripting Bug Reports
    Replies: 0
    Last Post: 07-13-2017, 07:45 AM
  2. Replies: 16
    Last Post: 09-28-2016, 05:21 PM
  3. Scripting Question Saving / Loading Values
    By allencoded in forum Scripting
    Replies: 2
    Last Post: 07-10-2016, 08:57 PM
  4. Replies: 2
    Last Post: 04-05-2016, 01:26 PM
  5. onPlayerTurnStart problem
    By dizneyguy in forum Scripting
    Replies: 3
    Last Post: 03-08-2016, 03:14 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
  •