Results 1 to 5 of 5

Thread: Help out an Amatuer?: Dynamic Buttons

  1. #1

    Help out an Amatuer?: Dynamic Buttons

    I've come up with a script.... well, roughly a script, to create multiple buttons in just one go, mostly for the lulz, but also for much more weirder projects I want to attempt to do in the near future.

    HOWEVER; The script I've come up with doesn't exactly work as fully intended at first glance. It will create the buttons and line them up in both rows and columns based on the parameters I setup, but the first row is always one button short at the end aside from all of it's following kin. I'm probably missing something here, can someone give me a hand?

    Code:
    function onload()
    
        i = 0 --Loop Variable
        ax = -7.20 --Button Position X
        ay = -6.70 --Button Position Y
        y = 8 --Row Control
    
        while ( i < 40 ) --40 buttons: First 4 have functions, the rest won't for a while.
        do
            i = i+1
            if ( i == y ) -- If statement if the Y variable matches up with the I on buttons created; Actions follow
            then
                ay = ay + 2.20 -- Changes Button Position Down a Row
                ax = -7.20 -- Resets the X where the button shows up
                y = y + 8 -- Prepares for the next row
            end
            TestString = "test"..i
            local button = {}
            button.width = 775
            button.height = 450
            button.position = {ax, 0.98, ay}
            button.rotation = {0, 0, 0}
            button.click_function = TestString
            button.function_owner = self
            button.label = 'Click'..i
            self.createButton(button)
            ax = ax + 2
        end
    end
    
    
    function test1()
        print('There is no Cake')
    end
    
    function test2()
        print('There is no Cake')
    end
    
    function test3()
        print('There is no Cake')
    end
    
    function test4()
        print('THERE IS CAKE!')
    end

  2. #2
    It's probably because you're immediately incrementing x, so it works as if it started at 1. Try moving the "i=i+1" to the very end of the "while" loop.

    But all in all, I think stuff like that is much clearer with for loops:
    Code:
    function onLoad()
        local x_init = -7.2  -- initial x pos
        local y_init = -6.7  -- initial y pos
        local x_step = 2     -- row spacing
        local y_step = 2.2   -- column spacing
        local row_len = 8    -- amount of buttons in a row
        local col_len = 5    -- amount of buttons in a column
    
        for col=0,(col_len-1) do
            local y_pos = y_init + col*y_step
    
            for row=0,(row_len-1) do
                local x_pos = x_init + row*x_step
                local i = (row+1) + col*row_len
    
                local button = {}
                button.width = 775
                button.height = 450
                button.position = {x_pos, 0.98, y_pos}
                button.rotation = {0, 0, 0}
                button.click_function = 'test' .. i
                button.function_owner = self
                button.label = 'Click' .. i
                self.createButton(button)
    
            end
        end
    end
    (i probably mixed up columns and rows but didn't have time to test it properly, sorry)

  3. #3
    This script works - I appreciate it!

  4. #4
    I was not sure what you are asking and I know this post will not be much help.. but (I ca not find the lnk for some reason) there is a popular tool mod that allows you to build button code dynamically inside TTS. Maybe you can use that, or pick the code apart for how it works to produce yoru dynamic stuff.

  5. #5
    Quote Originally Posted by Tragic View Post
    I was not sure what you are asking and I know this post will not be much help.. but (I ca not find the lnk for some reason) there is a popular tool mod that allows you to build button code dynamically inside TTS. Maybe you can use that, or pick the code apart for how it works to produce yoru dynamic stuff.

    The code dzikakulka produced was sufficient enough. I was looking for a method to make buttons with 1 "for" loop or two of them if I wanted to control it a lot better.
    Side's, I'm into learning coding languages like Lua or Python or C# and Java; relying on other mod' or waiting for other mods to do something easier than learning to do it yourself just seems like it would be lazy work.

Similar Threads

  1. Dynamic Lighting/Shadow for RPGs
    By georgatos7 in forum General Discussion
    Replies: 11
    Last Post: 06-09-2018, 02:10 AM
  2. [ADDED] Buttons: do not allow click on Back of buttons
    By FoaS in forum Scripting Suggestions
    Replies: 3
    Last Post: 04-01-2018, 08:03 AM
  3. Suggestion: natural, dynamic resizing
    By Unreal_Ed in forum VR Support
    Replies: 0
    Last Post: 07-21-2017, 10:57 AM
  4. Dynamic button oddities
    By GiantDwarf in forum Scripting
    Replies: 1
    Last Post: 04-26-2017, 08:15 PM
  5. Replies: 0
    Last Post: 10-07-2015, 03:56 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
  •