Page 2 of 2 FirstFirst 12
Results 26 to 30 of 30

Thread: Scripting Examples

  1. #26
    Quote Originally Posted by onelivesleft View Post
    Your shuffle mutates the input table *and* returns a copy of it; it should really only do one or the other.
    I have replaced it with another one form stackExchange... is this correct, if not can you post some working code?
    My Boardgame uTube chan - Tragic's Table Top
    BGG Guild of BoardGame uTubers - Tube Tables

  2. #27

    Turn Button

    I finally gave up on the integrated Turns system. Too hard to use unless you don't really need it all.

    This code goes in the script of a handy custom tile named "Turn Button". (or whatever else you have around to which you want to attach it, but fix the onLoad code in the global script accordingly...)
    If you want to use a fixed GUID, you can just skip the search, and set turnButton.obj to getObjectFromGUID(yourGUID).
    I spent many hours trying to find a way to make the button insert its GUID into the Global script. I was unsuccessful.

    Then you take the code in the initial comment and paste it into your global script, adjust sizes and shapes to your heart's content.
    If you just want either the big global button or the one in the button itself, just comment out the one you don't want...

    I made a test bed: https://steamcommunity.com/sharedfil...?id=1754765267

    Code:
    --[[
        -- turn button
        -- Insert this block of code into your global script:
        turnButton = {
            obj = nil, -- filled in in the onLoad script.  Could just use getObjectFromGUID("constant gid") instead.
            init = function() turnButton.obj.call("initTurns") end, -- initialize the turn system
            start = function() turnButton.obj.call("startTurns") end, -- sets order to getSeatedPlayers and chooses a random first player
            curPlayer = function() return turnButton.obj.call("getCurPlayer") end, -- returns the current player
            setOrder = function(list) turnButton.obj.setTable(order, list) end, -- change the order.  This is an array of player colors.  There are no seating requirements.
            order = function() return turnButton.obj.getTable("order") end, -- returns the current order
            passTurn = function(color) turnButton.obj.call("passTurn", {color = color}) end, -- ends the current player's turn as if the end turn button was pressed.  Useful for AIs
            setTurn = function(color) turnButton.obj.call("setTurn", {color = color}) end, -- sets the current player who must be in the order array.
            previous = function() return turnButton.obj.call("prev") end, -- returns the previous entry in order.
            next = function() return turnButton.obj.call("next") end, -- returns the next entry in order.
            reset = function(stat) turnButton.obj.call("onLoad", stat) end, -- clears all turns status
            toggleVerbose = function() turnButton.obj.call("toggleVerbose") end, -- changes whether the button reports on turn changes.
        }
    
        function onLoad()
            for _, obj in ipairs(getAllObjects()) do -- find the Turn Button.  
                if obj.getName() == "Turn Button" then
                    turnButton.obj = obj
                end
            end
            if not turnButton.obj then
                print("turnButton not initialized.")
            else
                print("TurnButton " .. turnButton.obj.getGUID() .. " Initialized")
            end
        end
    
    ]]
    
    order = {}
    curIndex = 0 -- control variable, indexed into order for curent player; 0 means turns disabled
    verbose = true -- set false to turn off information messages
    
    buttonXmlTable = {
        tag = "Button",
        attributes = { id="turnButton",
            width="250", height="120", position="0 0 -11", colors="#cccccc|#dddddd|#444444|#00000000",
            text="Start the Game", fontSize="40", fontStyle="Bold", textColor="Black", onClick = "initTurns",
        }
    }
    
    globalXmlTable = {
        tag = "Button",
        attributes = { id = "turnButton", position = "-500 500 0", width = "300", height = "50",
            text = "Start the Game", fontSize = "40", textColor = "Black",
            colors="#cccccc|#dddddd|#444444|#00000000", onClick = self.getGUID() .. "initTurns",
        }
    }
    
    function startTurns(player)
        if toggleVerbose then
            print("Game started by ", player.steam_name)
        end
        order = getSeatedPlayers()
        curIndex = math.random(#order)
        local curPlayer = order[curIndex]
        sayTo(curPlayer, curPlayer .. ", it is your turn.")
        showTurns({color = curPlayer, text = "End " .. curPlayer .. "'s turn"})
    end
    
    function initTurns(player) -- set up the buttons with "start the game".
        showTurns({color = "Black", text = "Start the game"})
    end
    
    function showTurns(params)
        -- print("showTurns: ", params.color, " ", params.text)
        local baseUI = Global.UI.getXmlTable()
        local ui = {}
        for i, item in ipairs(baseUI) do
            if item.attributes.id ~= "turnButton" then
                table.insert(ui, item)
            end
        end
        buttonXmlTable.attributes.textColor = params.color
        buttonXmlTable.attributes.text = params.text
        globalXmlTable.attributes.textColor = params.color
        globalXmlTable.attributes.text = params.text
        if params.color == "Black" then
            globalXmlTable.attributes.onClick = self.getGUID() .. "/startTurns"
            buttonXmlTable.attributes.onClick = "startTurns"
        else
            globalXmlTable.attributes.onClick = self.getGUID() .. "/endTurn"
            buttonXmlTable.attributes.onClick = "endTurn"
        end
        table.insert(ui, globalXmlTable)
        Global.UI.setXmlTable(ui)
        -- print(Global.UI.getXml())
        ui = {}
        baseUI = self.UI.getXmlTable()
        for i, item in ipairs(baseUI) do
            if item.attributes.id ~= "turnButton" then
                table.insert(ui, item)
            end
        end
        table.insert(ui, buttonXmlTable)
        self.UI.setXmlTable(ui)
        -- print(self.UI.getXml())
    end
    
    function onLoad(JSONTurnState)
        if JSONTurnState and JSONTurnState ~= "" then
            local turnState = JSON.decode(JSONTurnState)
            -- print(JSONTurnState)
            curIndex = turnState.curIndex
            if curIndex ~= 0 then
                order = turnState.order
                verbose = turnState.verbose or true
                print("The game continues.")
                showTurns({color = order[curIndex], text = "End " .. order[curIndex] .. "'s turn"})
            else
                print("No game")
                initTurns()
            end
        end
    end
    
    function onSave()
        turnState = { curIndex = curIndex, order = order, verbose = verbose }
        return JSON.encode(turnState)
    end
    
    function sayTo(color, message)
        broadcastToAll(message, stringColorToRGB(color))
    end
    
    function getCurPlayer()
        return order[curIndex]
    end
    
    function next()
        local ix = curIndex + 1
        if ix > #order then
            ix = 1
        end
        return order[ix]
    end
    
    function prev()
        local ix = curIndex - 1
        if ix < 1 then
            ix = #order
        end
        return order[ix]
    end -- of prev
    
    function passTurn(params)
        endTurn(Player[params.color])
    end
    
    function setTurn(params)
        local curPlayer = params.color
        for i, color in ipairs(order) do
            if color == curPlayer then
                sayTo(curPlayer, Player[curPlayer].steam_name .. ", it is your turn.")
                curIndex = i
                return
            end
        end
        print(curPlayer, " is not in the player list (order)")
    end
    
    function toggleVerbose()
        verbose = not verbose
        return verbose
    end
    
    function endTurn(player)
        local curPlayer = order[curIndex]
        if player.color ~= curPlayer then
            sayTo(player.color, "cannot end another player's turn (" .. (curPlayer or "nil") .. ")")
            return
        end
        if verbose then
            print("End of ", curPlayer, "'s turn")
        end
        if Global.getVar("onTurnEnd") then
            Global.call("onTurnEnd")
        end
        curIndex = curIndex + 1
        if curIndex > #order then
            curIndex = 1
        end
        curPlayer = order[curIndex]
        if verbose then
            print("Start of " .. curPlayer .. "'s turn.")
        end
        showTurns({color = curPlayer, text = "End " .. curPlayer .. "'s turn"})
        if Global.getVar("onTurnStart") then
            Global.call("onTurnStart")
        end
    end

  3. #28
    While this seems to work ok, I have run into severe problems with data consistency between different scripts.
    I am making a variation that has all functions in the global script. This seems to be working better. Will post when testing done.

  4. #29
    Been a while... here is a new script!

    Custom Name and Description based on rotation of object
    (Original Idea by Mark)

    The custom rotation value system allows you to set your own NAME tooltip very easily. Simply leave your default name space blank and then name your rotations. Now everytime you flip it, it will have the new name. Unfortunately this does not work at the moment for Descriptions as well.

    This is a little hack code that allows this behaviour but it requires the script be placed on all objects. I have no idea how this will affect performance if you have a billion objects with this script.

    **Leave All Rotation name Blank and leave Name and Description of object Blank.**

    The simply copy this code onto your object
    Code:
    data = {}
        data[1]={n = 'City Garrison', d = ''}
        data[2]={n = 'Ice Catapult', d = 'Attack : 9 (Ice / Cumbersome)\nDefence : 6 (Fortified)\nFame : 7\n\n(Lost Legion)'}
        --data[3]={n='', d=''}
        --data[4]={n='', d=''}
        --etc
    
    function onCollisionEnter()
        local face = self.getValue()
        self.setName(data[face].n)
        self.setDescription(data[face].d)
    end
    How does it work?

    • What this does is everytime a object collides it will test the Rotation Value. This works on drop and flip and w/e.
    • You set these with the rotations via the "value gizmo"
    • Set your rotations but do not name them. This way they will return the numeric value for the rotation.
    • In the script there is a data variable at the top for easy editing. In my example I have set it up for 2 values (a coin) but it should work for any amount like say a 12 sided die. Just follow the template. Use LUA string stuff to add line breaks like in my example.
    • On collision it finds the current rotation's numeric value and then uses that as a key to find the correct data
    • Then it just assigns that data to the name and description
    Last edited by Tragic; 07-09-2019 at 03:18 AM.
    My Boardgame uTube chan - Tragic's Table Top
    BGG Guild of BoardGame uTubers - Tube Tables

  5. #30
    HandPosition Error (Work Around)

    As any TTS modder knows there is a annoying bug that is there but kind of trivial so it gets passed over for more important things in the dev cycle. I'm talking about, of course, the hand zones kind of "dropping" downward. So if you put a card in the handzone it falls under the default tables. I have no idea why this happens, but it does ALL the time. It is super anoying, as you constantly have to adjust the Y postion of the hands.

    No longer!

    Here is a small hack that can fix this up for you. Currently this particular code dose not handle multible hand zone, but that is just as I did not need this functionality when I wrote the hack. It would be easy to adjust it for multipule hand zones per colour, and even hand zones of diffrent hights.

    The Code!
    Code:
    function onLoad()
        FixHandZones()
    end
    
    function FixHandZones()
        local Y = 3.91
        local playerList = Player.getAvailableColors()
        for _, colour in ipairs(playerList) do
            local pramsOLD = Player[colour].getHandTransform()
            pramsNew = {position={pramsOLD.position.x, Y, pramsOLD.position.z}
                       ,rotation={0,0,0}
                       ,scale={15.50, 5.40, 0.82}
                       }
            Player[colour].setHandTransform(pramsNew, 1)
        end
    end
    How does this work

    Place this code inside the GOLBAL script of your mod and call in in your onLoad(), you can place it in any onLoad really, I actualy like using global control objects to store variables and functions nowadays. Anyway, by having it in GLOBAL onLoad() the function is called every time the mod is loaded. What it does is simply look through the avaliable player hands and adjust the Y height.

    All you need to do is paste this into your code and then changed the 'Y' variable to your desiered height.

    You can change the numeric value in setHandTransform() to some kind of variable and use if statments and stuff to make this code more complex so it can handle multipul hand zones and heights.

    Hope this helps!!
    My Boardgame uTube chan - Tragic's Table Top
    BGG Guild of BoardGame uTubers - Tube Tables

Page 2 of 2 FirstFirst 12

Posting Permissions

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