help with factorio and lua (functions and if)

Place to get help with not working mods / modding interface.
Post Reply
BlackEagles
Burner Inserter
Burner Inserter
Posts: 17
Joined: Sun Apr 10, 2016 9:18 am
Contact:

help with factorio and lua (functions and if)

Post by BlackEagles »

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" ),

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: help with factorio and lua (functions and if)

Post by DaveMcW »

You cannot put if statements inside a table.

It should work if you move the if statements before data:extend.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: help with factorio and lua (functions and if)

Post by bobingabout »

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.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

BlackEagles
Burner Inserter
Burner Inserter
Posts: 17
Joined: Sun Apr 10, 2016 9:18 am
Contact:

Re: help with factorio and lua (functions and if)

Post by BlackEagles »

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. :D 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?

BlackEagles
Burner Inserter
Burner Inserter
Posts: 17
Joined: Sun Apr 10, 2016 9:18 am
Contact:

Re: help with factorio and lua (functions and if)

Post by BlackEagles »

i have made a new version, shorter and without in the data.extend, but same problem here

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"}),
Problem line 3 if statement, other symbol expected after if

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: help with factorio and lua (functions and if)

Post by DaveMcW »

You cannot put commas at the end of if statements.

BlackEagles
Burner Inserter
Burner Inserter
Posts: 17
Joined: Sun Apr 10, 2016 9:18 am
Contact:

Re: help with factorio and lua (functions and if)

Post by BlackEagles »

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 ^^

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: help with factorio and lua (functions and if)

Post by DaveMcW »

Pascal and php would also complain if you put commas at the end of every line. :P

Lua does accept semicolons at the end of lines;
but it is not required.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: help with factorio and lua (functions and if)

Post by bobingabout »

BlackEagles wrote:
Wed Dec 12, 2018 3:30 pm
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?
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.

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
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: help with factorio and lua (functions and if)

Post by Optera »

bobingabout wrote:
Thu Dec 13, 2018 10:13 am
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
I'd use a check for a specific mod instead of checking item.name.

Code: Select all

if mods["bobplates"] then
  -- do stuff when this mod exists
end
Both ways have their advantages.
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.

BlackEagles
Burner Inserter
Burner Inserter
Posts: 17
Joined: Sun Apr 10, 2016 9:18 am
Contact:

Re: help with factorio and lua (functions and if)

Post by BlackEagles »

DaveMcW wrote:
Wed Dec 12, 2018 10:23 pm
Pascal and php would also complain if you put commas at the end of every line. :P
YES ^^ you know what i mean XD

BlackEagles
Burner Inserter
Burner Inserter
Posts: 17
Joined: Sun Apr 10, 2016 9:18 am
Contact:

Re: help with factorio and lua (functions and if)

Post by BlackEagles »

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. :D

Post Reply

Return to “Modding help”