Results 1 to 6 of 6

Thread: Blackjack Counter issue

  1. #1

    Blackjack Counter issue

    Hey all!

    So I have a Scripted Blackjack game going and I have timers for my card counters. Problem is that for some reason my timer.destroy doesn't seem to want to destroy the timer I have created. So any load rewind or anything else starts causing multiple timer errors. Heres the code let me know what you think.

    --Card zone
    Zone = 'fdf87c'
    -- Zone Items
    ZoneItems = {}
    -- Item Counts
    Counts = {
    ['Ace'] = 1,
    ['Two'] = 2,
    ['Three'] = 3,
    ['Four'] = 4,
    ['Five'] = 5,
    ['Six'] = 6,
    ['Seven'] = 7,
    ['Eight'] = 8,
    ['Nine'] = 9,
    ['Ten'] = 10,
    ['Jack'] = 10,
    ['Queen'] = 10,
    ['King'] = 10,
    ['10-Up'] = 10,
    ['5-Up'] = 5,
    ['1-Up'] = 1,
    ['Fire Flower'] = -3,
    ['Frost Flower'] = -6
    }
    --Init Function
    function onLoad(save_state)
    --Load zone objects.
    Zone = getObjectFromGUID(Zone)

    --Update Count Timer
    Timer.destroy(Zone.getGUID())
    Timer.create({
    ['identifier'] = Zone.getGUID(),
    ['function_name'] = 'updateCount',
    ['delay'] = 0.5,
    ['repetitions'] = 0,
    })

    end
    function countTable(table)
    local count = 0
    for _ in pairs(table) do count = count + 1 end
    return count
    end
    function updateCount()
    local count = 0
    local aces = 0
    local cards = 0
    local objects = Zone.getObjects()

    for key,obj in pairs(objects) do
    local z = obj.getRotation().z
    if obj.tag == 'Deck' then

    local deck = obj.getObjects()
    cards = countTable(deck) + cards
    for key2, deckCard in pairs(deck) do

    local addValue = Counts[deckCard.nickname]
    if addValue == nil then
    addValue = 0
    end

    if deckCard.nickname == 'Ace' then
    aces = aces + 1
    end
    count = count + addValue
    end

    else
    if Counts[obj.getName()] and (z > 345 or z < 15) then
    count = Counts[obj.getName()] + count
    if obj.getName() == 'Ace' then
    aces = aces + 1
    end
    end
    if obj.tag == 'Card' then
    cards = 1 + cards
    end
    end

    end

    if aces ~= 0 then
    local testCount = count+10
    if testCount == 21 then count = 21 end
    end

    self.setValue(count)
    if count == 21 and cards <= 2 then
    self.setColorTint( {0.2, 1, 0.5} ) --Blackjack, turns Yellow
    elseif count == 21 and cards > 2 then
    self.setColorTint( {1, 1, 0} ) --21 turns green
    elseif count > 21 then
    self.setColorTint( {1, 0, 0} ) --Bust color red
    else
    self.setColorTint( {0, 0, 0} ) --regular color
    end
    end

  2. #2
    What is your timer.destroy returning?

  3. #3
    Quote Originally Posted by Curnil View Post
    What is your timer.destroy returning?
    Doesn't return anything. Problem I get is if I change games or use the rewind button I start getting spammed Error calling Lua function, updateCount in Timer

  4. #4
    What does print(Zone.getGUID()) say?

    Three suggestions:
    a) You are are getting the object for a hardcoded zone GUID -> you might as well hardcode the GUID for the timer as well.
    b) try to destroy the timer in the onDestroy() event of the zone. I would think that this event gets called when the time is rewound - I haven't checked though.
    c) unless you need timers for multiple dynamically created zones you could just use anything as the timer ID, it does not have to be a guid.

  5. #5
    a and b tried with no avail. And isn't a and c essentially the same thing?

  6. #6
    Join Date
    May 2016
    Posts
    1,072
    Delete your current timer code. In its place, put startTimer() then create a function elsewhere in the code (outside of other functions)

    function startTimer()
    Timer.destroy(Zone.getGUID())
    Timer.create({
    identifier = Zone.getGUID(),
    function_name = 'updateCount',
    delay = 0.5,
    })
    end

    then somewhere in your updateCount() function, just have a line of startTimer(). This ensures you only will ever have 1 timer looping at a time. (I got rid of the repetitions)

    Also, about Zone = getObjectFromGUID(Zone). I am 99% sure that needs to be inside of a function (like function onload() for it to actually establish Zone. Try to use print(Zone) in your current code, if it prints "nil" then Zone isn't being established correctly.

Posting Permissions

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