Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Soda 0.7] How to set .text in a list after initialization?

edited September 2017 in Questions Posts: 121

I have several lists i use to display a json. When you select an item on the names list, i set .text in other lists to list the keys and values. But it doesn't do anything.

The pastebin includes all utility functions used here, but they're not the problem, i just don't want someone to say it's those that don't work (i know they work because i use them for initializing .text)
https://pastebin.com/ywYspBJ6

My Json file
https://pastebin.com/HymeciZ8

Main
https://pastebin.com/9TuE3gqz

function listRax()
    local panel = Soda.Window{
        title = "Unit Designer",
        hidden = false,
        x=0, y=0.5, w=0, h=0.8, 
        blurred = true, --style = Soda.style.darkBlurred, --gaussian blurs what is underneath it
        shadow = true,
        shapeArgs = { corners = 1 | 3 | ~4 } 
    }
    local keys =
        Soda.List{
            parent = panel,
            x = .6, y = 20, w = .251, h = .8,
            text = getJsonKeys(rax, 1)
        }
    local values =
        Soda.List{
            parent = panel,
            x = .78, y = 20, w = .1, h = .8,
            text = getJsonValues(rax, 1)
        }    
    local unit_names = Soda.List{
        parent = panel,
        x = .325, y = 20, w = .25, h = .8,
        text = getJsonNames(rax),
        default = 1,
        callback = function(_, selected, __)

            keys.text = getJsonKeys(rax, selected.idNo)
            values.text = getJsonValues(rax, selected.idNo)
        end
    }
    --local textEntry = Soda.TextEntry{ --callback in values list (the one with text=getJsonValues)
        --parent = panel

end

Comments

  • AnatolyAnatoly Mod
    Posts: 830

    They should add a big category for Soda...

  • edited September 2017 Posts: 121

    probably, that would be nice haha @TokOut

    edit: So I CAN alter an items text in selected,title, so if I can find its parent table I should be able to change everything in a list at once

        local a = Soda.List{
            x=.4,y=.1,w=.8,h=.8,
            text={'Example','String'},
            callback = function(self, selected, txt)
            local s = ''
            for k,v in pairs(selected) do
                --print(k,v)
                s = s..'\n'..k..', '..tostring(v)
                --for a,b in pairs(v) do
                    --print('   '..a,b)
                --end
    
            end
            selected.title = 'Aardvark'
            print(s)
        end
    
  • edited September 2017 Posts: 121
        function SearchTable(t, e)  
        --searches table t for value e
        --e can be a table
        local KeepGoing = true
            for k,v in pairs(t) do
                if v == e then
                    print(t, v)
                    return false
                elseif type(v) == 'table' and KeepGoing then
                    KeepGoing = SearchTable(v, e)
                end
            end
        return true
        end
    

    Was trying to find the parent of selected in the callback. Alas, I get a stack overflow error. Maybe if I was better at making recursive loops they wouldn't go blah on me.

    Update: I put KeepGoing variable to be outside the function. and instead of calling itself, call a tail call function that calls back on SearchTable. In theory. I've never actually used tail calls before so i dont know if this is right. Still sometimes gets stack overflows

    local KeepGoing = true
    
    local function tail(t,e)
        return SearchTable(t,e)
    end
    
    function ResetSearch()
        KeepGoing = true
    end
    
    
        function SearchTable(t, e)  
        --searches table t for value e
        --e can be a table
            for k,v in pairs(t) do
                if v == e then
                    KeepGoing = false
                    print(t)
                elseif type(v) == 'table' and KeepGoing then
                    tail(v, e)
                end
            end
        end
    
    
  • edited October 2017 Posts: 557

    @xThomas if you don't mind my asking

    • what is the reason that you need to identify the parent in code? Unless I'm missing something wouldn't you be creating that parent in code yourself? Is there a reason the parent can't be manually passed to the function when needed?
    • if I understand correctly, lua supplies no default implementation for comparing two tables with an == operator, and doing so has to be specifically handled by metatables in both tables. Without a metatable I assume [someTable] == [someOtherTable] always evaluates to false. Have you defined metatables for the tables that are being compared?

    I've never really mucked about with metatables, sorry to say I couldn't be of help there...

  • edited October 2017 Posts: 121

    @UberGoober Basically I want.. ahh i dont know the right terms but I want to display some tables as a series of lists

    list1 is made of the names (each table has a Name key, and i throw those in their own table)
    list2 is made of the keys, corresponding to the name
    list3 is made of the values, corresponding to the key to the name. eventually it will be a list with textentry enabled, if i can figure that out. (ill think ill just draw transparent textentry boxes)

    Example, where list1 strings is {'apple', 'cow'}, if you select apple then

    list2 --> {'weight', 'color', 'kind'}
    list3 --> {'.5 lbs', 'red', 'fuji'}

    but if you select cow then

    list2 --> {'weight', 'height', 'age'}
    list3 --> {'500 lbs?', '4 ft 11"', 'young.. maybe.'}

  • I do think those situations call for metatables, especially if you're comparing tables that contain sub-tables.

    So in your example, what's the situation where a Soda element needs to know its parent?

  • Posts: 2,020

    You can use the parent parameter (you have to set this yourself when you create the object). You're doing this in the code you post above.

  • Posts: 121

    Ahh.

    [quote]So in your example, what's the situation where a Soda element needs to know its parent?[/quote]

    I might not have needed that in the first place, actually. I'm just trying to find out how to change all the text in a list and have it lookdifferent, but I'm really new to Soda. I plan to take a few weeks learning soda before asking again :neutral:

Sign In or Register to comment.