Page 1 of 1

[Solved] lua:15: unexpected symbol near 'for'

Posted: Tue Dec 20, 2022 9:49 pm
by Phoniez
So I am trying to write a function, which given some inputs automatically creates a valid fluid prototype, however whenever I run my mod with this code I get the error in the title, and I am unable to pinpoint its source. Also I have attached the full mod below.

Code In Question:

Code: Select all

--Functions
function create_gas(parameters)
    return--Takes gas name,flow color,default temperature and when it turns into a gas and 
    {     --puts this into a form that can be passed into data:extend
        icon="__highfleet-ships__/graphics/icons/"..parameters.name..".png",
        icon_mipmaps=4,
        icon_size=64,
        type="fluid",
        name=parameters.name,
        subgroup="fluid",
        base_color={},
        flow_color=parameters.flow_color,
        default_temperature=parameters.default_temperature,
        gas_temperature=parameters.gas_temperature,
        for keys,values in ipairs(flow_color) do
            table.insert(base_color,math.floor(0.5*values))
        end
    }
end

Re: lua:15: unexpected symbol near 'for'

Posted: Wed Dec 21, 2022 12:57 am
by robot256
You...can't put for loops inside tables. Or if you sometimes can, you really shouldn't ever. Try creating the table as a local variable with all the constants, modify it with the for loop, and then return the table variable.

Re: lua:15: unexpected symbol near 'for'

Posted: Wed Dec 21, 2022 10:57 am
by Phoniez
robot256 wrote: Wed Dec 21, 2022 12:57 am You...can't put for loops inside tables. Or if you sometimes can, you really shouldn't ever. Try creating the table as a local variable with all the constants, modify it with the for loop, and then return the table variable.
Thanks! The code is working now, I will now leave the fixed code here for anyone who needs it:

Code: Select all

function create_gas(parameters)
    local fluid_prototype=--Takes gas name,flow color,default temperature and when it turns into a gas and 
    {                     --puts this into a form that can be passed into data:extend
        icon="__highfleet-ships__/graphics/icons/"..parameters.name..".png",
        icon_mipmaps=4,
        icon_size=64,
        type="fluid",
        name=parameters.name,
        subgroup="fluid",
        base_color={},
        flow_color=parameters.flow_color,
        default_temperature=parameters.default_temperature,
        gas_temperature=parameters.gas_temperature
    }

    for current_key,current_value in ipairs(parameters.flow_color) do
        table.insert(fluid_prototype.base_color,math.floor(0.5*current_value))
    end

    return fluid_prototype
end