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

Place to get help with not working mods / modding interface.
Phoniez
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sun Jul 10, 2022 1:49 pm
Contact:

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

Post 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
Attachments
highfleet-ships_0.0.1.zip
Full Mod.
(70.29 KiB) Downloaded 49 times
Last edited by Phoniez on Wed Dec 21, 2022 10:57 am, edited 1 time in total.
robot256
Smart Inserter
Smart Inserter
Posts: 1248
Joined: Sun Mar 17, 2019 1:52 am
Contact:

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

Post 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.
My mods: Multiple Unit Train Control, RGB Pipes, Shipping Containers, Rocket Log, Smart Artillery Wagons.
Maintainer of Auto Deconstruct, Cargo Ships, Vehicle Wagon, Honk, Shortwave.
Phoniez
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sun Jul 10, 2022 1:49 pm
Contact:

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

Post 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
Post Reply

Return to “Modding help”