help with factorio and lua (functions and if)
-
- Burner Inserter
- Posts: 17
- Joined: Sun Apr 10, 2016 9:18 am
- Contact:
help with factorio and lua (functions and if)
I am trying to renew our mod with an shorte code. Therefore i write functions to call them if i want to make new items/recepies. Core of this are optional parameters in lua. But i have a problem with the syntax, i hae uploades the following code. It is the maincode nothing more gamechanging atm. But factorio stoped loading with the error "unexpected symbol near if(lua 10)". May you can help me?
function item (name, stack_size, subgroup, icon, icon_size, order, flag1, flag2, flag3 )
data:extend({{
type = "item",
name = name,
stack_size = stack_size,
subgroup = subgroup,
-- icon Pfadangabe innerhalb des Icon-Ordners
icon = string.gsub("__bobplates__/graphics/icons/subpath/icon.png", "subpath/icon.png", icon),
-- icon_size
if not icon_size then
icon_size = 32,
else
icon = icon_size,
end
-- order
if not order then
order = string.gsub("b[order]", "order", name),
else
order = order,
end
-- flags (1 - 3)
if not flag1 then
flags = {"goes-to-main-inventory"},
else
flags = {flag1},
end
if not flag2 then
flags = {flag1, flag2},
else
flag2 = 0,
end
if not flag3 then
flags = {flag1, flag2, flag3},
else
flag3 = 0,
end
},})
end --var1 bis var3 necesarry, var4 bis var7 optional
item("zinc-ore", 200, "raw-resource", "ore/zinc-ore.png", 32, "b[zinc-ore]", "goes-to-main-inventory" ),
function item (name, stack_size, subgroup, icon, icon_size, order, flag1, flag2, flag3 )
data:extend({{
type = "item",
name = name,
stack_size = stack_size,
subgroup = subgroup,
-- icon Pfadangabe innerhalb des Icon-Ordners
icon = string.gsub("__bobplates__/graphics/icons/subpath/icon.png", "subpath/icon.png", icon),
-- icon_size
if not icon_size then
icon_size = 32,
else
icon = icon_size,
end
-- order
if not order then
order = string.gsub("b[order]", "order", name),
else
order = order,
end
-- flags (1 - 3)
if not flag1 then
flags = {"goes-to-main-inventory"},
else
flags = {flag1},
end
if not flag2 then
flags = {flag1, flag2},
else
flag2 = 0,
end
if not flag3 then
flags = {flag1, flag2, flag3},
else
flag3 = 0,
end
},})
end --var1 bis var3 necesarry, var4 bis var7 optional
item("zinc-ore", 200, "raw-resource", "ore/zinc-ore.png", 32, "b[zinc-ore]", "goes-to-main-inventory" ),
Re: help with factorio and lua (functions and if)
You cannot put if statements inside a table.
It should work if you move the if statements before data:extend.
It should work if you move the if statements before data:extend.
- bobingabout
- Smart Inserter
- Posts: 7352
- Joined: Fri May 09, 2014 1:01 pm
- Contact:
Re: help with factorio and lua (functions and if)
Alternatively, you could make the if statement a function that returns a value.
look at demo_enemies.lua at how the spawner works, the biters spawn table contains an if demo check, and they used a function to make it work.
look at demo_enemies.lua at how the spawner works, the biters spawn table contains an if demo check, and they used a function to make it work.
-
- Burner Inserter
- Posts: 17
- Joined: Sun Apr 10, 2016 9:18 am
- Contact:
Re: help with factorio and lua (functions and if)
Thank you for your fast help, i will try it right now. And a special thanks to bob, i am realy happy about your attetion. I love your mods and your code, its the only thing we used since ever ^^ And the libary-mod is... wow ^^
PS: Maybe you can awnser me one question, to an other topic:
How i can work with items from your mod in our recipies, like zinc ore, without implement them in our mod. How can i cross it?
PS: Maybe you can awnser me one question, to an other topic:
How i can work with items from your mod in our recipies, like zinc ore, without implement them in our mod. How can i cross it?
-
- Burner Inserter
- Posts: 17
- Joined: Sun Apr 10, 2016 9:18 am
- Contact:
Re: help with factorio and lua (functions and if)
i have made a new version, shorter and without in the data.extend, but same problem here
Problem line 3 if statement, other symbol expected after if
Code: Select all
function item (i_name, i_stack_size, i_subgroup, i_icon_path, i_icon_size, i_order, i_flags )
local icon_size_number = 0,
if not i_icon_size then
icon_size_number = 32,
else
icon_size_number = i_icon_size,
end
local order_string = {},
if not i_order then
order_string = string.gsub("b[order]", "order", i_name),
else
order_string = i_order,
end
local flag_array = {},
if not i_flags then
flag_array = {"goes-to-main-inventory"},
else
flags = i_flags,
end
return data:extend({{
type = "item",
name = i_name,
stack_size = i_stack_size,
-- group for sorting item in inventory
subgroup = i_subgroup,
-- icon path within the icon-folder
icon = string.gsub("__bobplates__/graphics/icons/subpath/icon.png", "subpath/icon.png", i_icon_path),
-- how big is the picture
icon_size = icon_size_number,
-- wich comes first and wich comes next
order = order_string,
-- flags
flags = flag_array,
},})
end --var1 bis var4 necesarry, var5 bis var7 optional
item("zinc-ore", 200, "raw-resource", "ore/zinc-ore.png", 32, "b[zinc-ore]", {"goes-to-main-inventory"}),
Re: help with factorio and lua (functions and if)
You cannot put commas at the end of if statements.
-
- Burner Inserter
- Posts: 17
- Joined: Sun Apr 10, 2016 9:18 am
- Contact:
Re: help with factorio and lua (functions and if)
Wow, that was the problem, thats realy stupid. in lua you dont need any commas? If have programmed with pascal and php and i have never seen such an unlogical crap ^^ I realy thank you, if have been watched over for ours and doesnt think this could be the problem ^^
Re: help with factorio and lua (functions and if)
Pascal and php would also complain if you put commas at the end of every line.
Lua does accept semicolons at the end of lines;
but it is not required.
Lua does accept semicolons at the end of lines;
but it is not required.
- bobingabout
- Smart Inserter
- Posts: 7352
- Joined: Fri May 09, 2014 1:01 pm
- Contact:
Re: help with factorio and lua (functions and if)
The easiest way is if you set my mod's name as a dependancy in the info.json file, that will force my mod to load before yours.BlackEagles wrote: ↑Wed Dec 12, 2018 3:30 pmHow i can work with items from your mod in our recipies, like zinc ore, without implement them in our mod. How can i cross it?
A safe way to make sure it exists before doing anything that uses it is to use an if block.
if data.raw.item["lead-plate"] then
-- do stuff with lead plate here
end
Re: help with factorio and lua (functions and if)
I'd use a check for a specific mod instead of checking item.name.bobingabout wrote: ↑Thu Dec 13, 2018 10:13 amA safe way to make sure it exists before doing anything that uses it is to use an if block.
if data.raw.item["lead-plate"] then
-- do stuff with lead plate here
end
Code: Select all
if mods["bobplates"] then
-- do stuff when this mod exists
end
Checking for Item.name allows your mod to make use of "lead-plate" generated by any other mod.
Checking for a specific mod ensures you get the prototype you expect and not some weird variant. It can get really nasty if two mods use the same name for different entity types.
My Mods: mods.factorio.com
-
- Burner Inserter
- Posts: 17
- Joined: Sun Apr 10, 2016 9:18 am
- Contact:
-
- Burner Inserter
- Posts: 17
- Joined: Sun Apr 10, 2016 9:18 am
- Contact:
Re: help with factorio and lua (functions and if)
I thank you all for your help all work fine, good christmas days until the next question XD or the mod being updated to 0.16 in a faster and nicer code system.