Results 1 to 8 of 8

Thread: Input boxes and Buttons

  1. #1

    Input boxes and Buttons

    I suspect I'm missing something basic and would like some help please.

    I've been able to create inputboxes and buttons.
    However, I'd like to be able to click a button and have it change the value of an inputbox.
    But I haven't been able to figure out how to get a reference to the input box to update it in any manner and haven't been able to find anything online.

    Code:
    function onload()
        params = {}
        params.input_function='setName'
        [Bunch of values here]
    
        btnParams = {}
        btnParams.click_function = 'cycleName'
        [Bunch more values]
    
        self.createInput(params)
        self.createButton(btnClass)
    end
    
    function setName()
    end
    
    function cycleName()
        -- I want this to do something like this:
        if name == 'Bob'
             name = 'Bill'
        else
             name = 'Bob'
        end
    end

  2. #2
    It's not quite as straightforwards as you (we all) would want it to be.

    First, you can use object.getInputs() to get a table of input existing on it. Then, you have to choose the one you want to edit and extract its index. Finally, you can use object.editInput(...), passing the index there along with any values you want to modify. If you only have one input it's fine because you don't have to look for it. It gets more complicate with more inputs as you have to identify them through one of their properties, e.g. input_function. If you have some with identical ones, tough luck, gotta look at other parameters too.

    Code:
    -- Let's say we simply want to set a value of an input to newString when there's only one on an object
    function SetInputValue(newString)
        local inputs = self.getInputs()
        local editIndex = inputs[1].index or error('No input found!')
        self.editInput( {index = editIndex, value = newString} )
    end
    
    -- If we have many inputs, let's say they all have different input_function
    -- This will set value to newString for an input that has input_function set to whatever you pass as inputFcnName
    function SetParticularInputValue(inputFcnName, newString)
        local inputs = self.getInputs()
        for _,data in ipairs(inputs) do
            if data.input_function == inputFcnName then
                self.editInput( {index = data.index, value = newString} )
                return
            end
        end
        error('No input with function_name \'' .. inputFcnName '\' found!')
    end
    You can of course delete the error functions if you want it to simply not do anything if there was no valid input. But IMO explicit erroring is better than failing silently.

  3. #3
    Quote Originally Posted by dzikakulka View Post
    It's not quite as straightforwards as you (we all) would want it to be.

    First, you can use object.getInputs() to get a table of input existing on it. Then, you have to choose the one you want to edit and extract its index. Finally, you can use object.editInput(...), passing the index there along with any values you want to modify. If you only have one input it's fine because you don't have to look for it. It gets more complicate with more inputs as you have to identify them through one of their properties, e.g. input_function. If you have some with identical ones, tough luck, gotta look at other parameters too.

    Code:
    -- Let's say we simply want to set a value of an input to newString when there's only one on an object
    function SetInputValue(newString)
        local inputs = self.getInputs()
        local editIndex = inputs[1].index or error('No input found!')
        self.editInput( {index = editIndex, value = newString} )
    end
    
    -- If we have many inputs, let's say they all have different input_function
    -- This will set value to newString for an input that has input_function set to whatever you pass as inputFcnName
    function SetParticularInputValue(inputFcnName, newString)
        local inputs = self.getInputs()
        for _,data in ipairs(inputs) do
            if data.input_function == inputFcnName then
                self.editInput( {index = data.index, value = newString} )
                return
            end
        end
        error('No input with function_name \'' .. inputFcnName '\' found!')
    end
    You can of course delete the error functions if you want it to simply not do anything if there was no valid input. But IMO explicit erroring is better than failing silently.
    self.getInputs() and the examples on its use are what I spent all day trying to find.
    Thank you very much.
    You saved what's left of my hair.
    From what I did see, it looks like these are new and they may not have updated the documentation yet.

  4. #4
    [url]http://berserk-games.com/knowledgebase/object/#getInputs[/url]

  5. #5
    Join Date
    Sep 2017
    Posts
    397
    I am forming the habit of creating a "field table" in any object where I need to manage buttons or inputs.
    The advantage really comes out when your code is in flux, and you may be adding or removing things often
    enough to make tracking the indices annoying. You sacrifice a small amount of memory for easy to follow
    code structure and improved efficiency - no looking through all your inputs each time you want to work with one.

    Code:
    fields = {} -- list of input indices by label
    
    function onLoad()
    
    -- build your inputs here.
    
    
        for _, inp in ipairs(self.getInputs()) do
            if inp.label then
                fields[inp.label] = inp.index
            else
                print(self.getName(), ": missing label, ", inp.click_function)
            end
        end
    end
    
    function showField(params)
        local labl = params[1]
        local val = params[2]
        local input_parameters = {}
        input_parameters.index = fields[labl]
        input_parameters.value = val
        self.editInput(input_parameters)
    end
    Examples
    local call:
    showField({"label", value})

    -- external call:
    object.call("showField", {"label", value})

  6. #6
    How can we make use of the input function? Can it be triggered on hitting return after typing? It seems the input function triggers when the word for the function is reached and then again when you click out of the field.

  7. #7
    Join Date
    May 2016
    Posts
    1,072
    Whenever you edit a string, each key you press is activating that function. So if one of them is a page break ("\n") then you can detect that an enter was submitted.

    Now, input boxes allow for enter, since they allow multi-line input. But I made a feature in Last Retort (available in the workshop if you want to hunt down my input code) which detected if the last key pressed was "\n" and if it was, it "submitted" the edit by removing and instantly respawning the input box with the text input it had at the time \n was submitted without the \n

    A bit more complicated than you were likely looking for, but it is doable

  8. #8
    Cool I will look for it. Here is the reason why I want to have a "return" to activate the search instead of having to draw



    here is the code for the above too

    [url]https://pastebin.com/wtv6tfm1[/url]

Similar Threads

  1. Easy way to navigate between text boxes
    By bbacher in forum Suggestions
    Replies: 4
    Last Post: 09-25-2017, 03:53 PM
  2. Input Boxes oversight (bug)
    By Flolania in forum Scripting Bug Reports
    Replies: 0
    Last Post: 08-25-2017, 10:37 PM
  3. Invisible hide boxes
    By iPotatis in forum Suggestions
    Replies: 3
    Last Post: 11-30-2016, 07:59 PM
  4. Replies: 2
    Last Post: 03-20-2016, 06:20 AM
  5. Delete characters in text boxes (in Linux)
    By SmileyChris in forum Bug Reports
    Replies: 1
    Last Post: 02-21-2016, 09:53 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
  •