Results 1 to 5 of 5

Thread: Scripting error when using timers with parameters

  1. #1

    Scripting error when using timers with parameters

    http://steamcommunity.com/sharedfile.../?id=937273477

    Hello everyone! I've not done any programming in years so I might as well be a babe in the woods.. I'm trying to dissect the expertly done Harry Potter: Hogwarts Battle mod to help me make the scripting for Attack on Titan due to its somewhat complicated setup.
    I'm trying to use a recursive function to draw cards for the setup and I'm trying to put in a delay to make it work properly using timers. I'm getting an error that I'm comparing a table to a value so I'm assuming I'm passing my peramiters incorrectly from the timer function.

    If anyone that's better versed at this would like to look at what I have, the link at the top is the mod I uploaded for testing.
    If it helps, I can just post the code, I just don't have the files on me at the moment.

    I'm hoping someone can either let me know what I did wrong or just chastise me for being inefficient and have a better solutions for what I'm trying to do

  2. #2

    Question

    Sorry I didn't provide as much details as I would have liked, I was in the middle of leaving where I was and had to be a bit brief. so here's the scenario:

    I need to take a deck of 80 cards and split it into 6 smaller decks.
    Then 1 card is added to 4 of the 6 decks and shuffled
    the smaller decks are then stacked to remake the big deck.

    I'm in the middle of trying to split out the cards by simply moving cards around to different zones. This is the recursive code I'm having a problem with:

    Code:
    function splitDecks(cardsToSplit, zoneToSplit)
        if cardsToSplit > 0 then
            getObjectFromGUID(startingDeck).takeObject{position={zoneToSplit.x,10,zoneToSplit.z}, rotation={0,0,0}}
            cardsToSplit = cardsToSplit - 1
            delayedCall('splitDecks',0.5,{cardsToSplit, zoneToSplit})
        else end
    end
    
    function delayedCall(functionToCall, delayTime, parametersToCall)
    	local uniqueID = 'timer'..numTimers
    	numTimers = numTimers + 1
    	Timer.create({identifier = uniqueID, function_name = functionToCall, parameters = parametersToCall, delay = delayTime})
    end
    I think I'm passing the parameters incorrectly as it works if I don't try to use the delay function. As a reference, here's the entire thing:

    Code:
    
    
    
    --[[ Lua code. See documentation: http://berserk-games.com/knowledgebase/scripting/ --]]
    
    --[[ The OnLoad function. This is called after everything in the game save finishes loading.
    Most of your script code goes here. --]]
    function onload()
        declareVariables()
        getObjectFromGUID(startingDeck).createButton{click_function = 'setupGame',function_owner = nil,label = 'Automatic Setup',position = {3,-0.1,-1},rotation = {0,0,0},width = 1000,height = 40,font_size = 100}
        getObjectFromGUID(startingDeck).createButton{click_function = 'clearButton',function_owner = nil,label = 'Manual Setup',position = {3,-0.1,0},rotation = {0,0,0},width = 1000,height = 40,font_size = 100}
    end
    
    --[[ The Update function. This is called once per frame. --]]
    function update ()
        --[[ print('Update loop!') --]]
    end
    
    function shuffleStartingDecks ()
        getObjectFromGUID(startingDeck).shuffle()
        getObjectFromGUID(titan2Deck).shuffle()
        getObjectFromGUID(titan3Deck).shuffle()
        getObjectFromGUID(titan4Deck).shuffle()
        getObjectFromGUID(titanOnAttackDeck).shuffle()
    end
    
    function setupGame ()
        broadcastToAll('setupGame pressed', {0.31, 1, 0})
        getObjectFromGUID(startingDeck).clearButtons()
        getObjectFromGUID(startingDeck).flip()
        for i = 0, 1, 0.5 do
            broadcastToAll('for step ' .. i, {0.31, 1, 0})
            delayedCall('shuffleStartingDecks',i)
        end
        splitDecks(13,split2pos)
    
    
    
    end
    
    function splitDecks(cardsToSplit, zoneToSplit)
        if cardsToSplit > 0 then
            getObjectFromGUID(startingDeck).takeObject{position={zoneToSplit.x,10,zoneToSplit.z}, rotation={0,0,0}}
            cardsToSplit = cardsToSplit - 1
            delayedCall('splitDecks',0.5,{cardsToSplit, zoneToSplit})
        else end
    end
    
    function delayedCall(functionToCall, delayTime, parametersToCall)
    	local uniqueID = 'timer'..numTimers
    	numTimers = numTimers + 1
    	Timer.create({identifier = uniqueID, function_name = functionToCall, parameters = parametersToCall, delay = delayTime})
    end
    
    function clearButton ()
        broadcastToAll('clearButton pressed', {0.31, 1, 0})
        getObjectFromGUID(startingDeck).clearButtons()
    end
    
    function declareVariables ()
        --Zone Declaration
        castleDeckZone = 'e6b0c4'
        castleSplit1 = '85e394'
        castleSplit2 = '96a4c3'
        castleSplit3 = 'f23222'
        castleSplit4 = '54a609'
        castleSplit5 = 'e26a1f'
        castleSplit6 = '7cb72c'
    
        --GUID Declaration
        startingDeck = '668551'
        titan1Card = '5c3ab3'
        titan2Deck = '544007'
        titan3Deck = '2df550'
        titan4Deck = 'c2b0c8'
        titanOnAttackDeck = 'e66308'
    
        --Variable Declaration
        numTimers = 0
        split1pos = getObjectFromGUID(castleSplit1).getPosition()
        split2pos = getObjectFromGUID(castleSplit2).getPosition()
        split3pos = getObjectFromGUID(castleSplit3).getPosition()
        split4pos = getObjectFromGUID(castleSplit4).getPosition()
        split5pos = getObjectFromGUID(castleSplit5).getPosition()
        split6pos = getObjectFromGUID(castleSplit6).getPosition()
    end

  3. #3
    Join Date
    May 2016
    Posts
    1,072
    When you pass parameters with a timer, it passes them as a single table, not as separate parameter entries.

    So the first time your code is running, cardsToSplit is equal to a number. When you run it again, now cardsToSplit = {cardsToSplit, zoneToSplit}

    A possible solution would be to re-do the way you initially call the timer so that is takes a single entry of aparameters, formatted as a table, and fetches the parameter values it needs out of that table.

    Another solution would be to only trigger the timer once, using the "repetitions" feature (along with your cardsToSplit variable) to repeat it for as many times as needed. This would be my preferred solution in this case

    Hope this helps

  4. #4
    I'm mostly versed via your tutorials, did you have a repetitions example in one of those I don't recall? otherwise I may have to do some reading.

    EDIT: Just looked it up, I see it's in the timer text. so it should just repeat the called function x times I'm guessing.. I'll have testing to do.

  5. #5
    Join Date
    May 2016
    Posts
    1,072
    Good, because I do not because I they were only fixed up recently and I learned to work without them haha

    Yeah, fortunately it is pretty straight forward. In the same way you have delay=0.5, you make repetitions=5 or whatever and the timer will repeat itself 5 times, each time running for 0.5 seconds.

Similar Threads

  1. Replies: 12
    Last Post: 05-11-2017, 01:23 PM
  2. Timers are preserved between tables
    By customKarma in forum Bug Reports
    Replies: 1
    Last Post: 12-22-2016, 11:58 AM
  3. Timers - Bad Assumptions
    By LordAshes in forum Scripting
    Replies: 4
    Last Post: 07-23-2016, 09:48 PM
  4. Add parameters.parameters to button parameters.
    By MrStump in forum Suggestions
    Replies: 0
    Last Post: 07-03-2016, 11:23 AM
  5. Replies: 2
    Last Post: 04-01-2016, 07:25 AM

Posting Permissions

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