[Done] How to Add an additional flag property

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Done] How to Add an additional flag property

Post by TheSAguy »

Hi,

I'd like to add "not-flammable" to some entity.

So I don't know what the current flags might be, and need to just add the additional flag, to the existing flags.
If I do the below I'll remove the existing flags.

Code: Select all

local types = {"something"}
local name = {"something"}

for _, entity in pairs(types) do
    for _, x in pairs(data.raw[entity]) do
        if entity == name  then
            x.flags = {"not-flammable"}
        end
    end
end
Thanks
Last edited by TheSAguy on Wed Jun 06, 2018 6:39 pm, edited 1 time in total.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5412
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: How to Add an additional flag property

Post by Klonan »

Code: Select all

table.insert(entity.flags, "not-flammable")
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: [Done] How to Add an additional flag property

Post by TheSAguy »

Thanks!
Bilka
Factorio Staff
Factorio Staff
Posts: 3470
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: [Done] How to Add an additional flag property

Post by Bilka »

Note that "not-flammable" only means that fire stickers do not apply to the entity, so it cannot catch fire. It still takes fire damage.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: [Done] How to Add an additional flag property

Post by TheSAguy »

Bilka wrote:Note that "not-flammable" only means that fire stickers do not apply to the entity, so it cannot catch fire. It still takes fire damage.
Ah, thanks for the tip, did not realize that.
So resistance fire = 100 is the way to go.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Done] How to Add an additional flag property

Post by eradicator »

TheSAguy wrote:
Bilka wrote:Note that "not-flammable" only means that fire stickers do not apply to the entity, so it cannot catch fire. It still takes fire damage.
Ah, thanks for the tip, did not realize that.
So resistance fire = 100 is the way to go.
Probably resistance and flag. Not sure how stickers work, but a 100% resistant entity might still spread fire even if it takes no damage itself.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: [Done] How to Add an additional flag property

Post by TheSAguy »

What's wrong with my code below?

Code: Select all

	--Construction bots can't catch fire and not Minable
	local types = {"construction-robot"}
	
	for _, entity in pairs(types) do
		for _, x in pairs(data.raw[entity]) do
			if entity == "construction-robot"  then
				table.insert(x.flags, "not-flammable")
				table.insert(x.resistances, { { type = "fire", percent = 100 } })
				x.minable = nil
			end
		end
	end
Thanks,.

EDIT: I think it was the extra "{}" in the Resistances that caused it.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Done] How to Add an additional flag property

Post by eradicator »

Code: Select all

local function immunify(bot)
  table.insert(bot.flags,"not-flammable")
  table.insert(bot.resistances, {type = "fire", percent = 100})
  end

--catches modded bots too
for _,bot in pairs(data.raw['construction-robot']) do immunify(bot) end
for _,bot in pairs(data.raw['logistic-robot'])          do immunify(bot) end
Edit: Less loops, more clarity.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: [Done] How to Add an additional flag property

Post by darkfrei »

eradicator wrote:

Code: Select all

local function immunify(bot)
  table.insert(bot.flags,"not-flammable")
  table.insert(bot.resistances, {type = "fire", percent = 100})
  end

--catches modded bots too
for _,bot in pairs(data.raw['construction-robot']) do immunify(bot) end
for _,bot in pairs(data.raw['logistic-robot'])          do immunify(bot) end
Edit: Less loops, more clarity.
Please keep in mind that another mod can add the same flag too. So, just easy function, which checks that you haven't this flag before.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Done] How to Add an additional flag property

Post by eradicator »

darkfrei wrote:Please keep in mind that another mod can add the same flag too. So, just easy function, which checks that you haven't this flag before.
Please keep in mind that the engine doesn't care about double flags. It'll just ignore the others. Adding checks for something the engine already handles only bloats your code.

Code: Select all

for i=1,100 do
 table.insert(data.raw.player.player.flags,'not-flammable')
 end
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: [Done] How to Add an additional flag property

Post by TheSAguy »

eradicator wrote:
darkfrei wrote:Please keep in mind that another mod can add the same flag too. So, just easy function, which checks that you haven't this flag before.
Please keep in mind that the engine doesn't care about double flags. It'll just ignore the others. Adding checks for something the engine already handles only bloats your code.

Code: Select all

for i=1,100 do
 table.insert(data.raw.player.player.flags,'not-flammable')
 end
Image
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: [Done] How to Add an additional flag property

Post by bobingabout »

eradicator wrote:
darkfrei wrote:Please keep in mind that another mod can add the same flag too. So, just easy function, which checks that you haven't this flag before.
Please keep in mind that the engine doesn't care about double flags. It'll just ignore the others. Adding checks for something the engine already handles only bloats your code.

Code: Select all

for i=1,100 do
 table.insert(data.raw.player.player.flags,'not-flammable')
 end
Now you tell me.

I wanted to add a flag to belts when I was doing updates just yesterday. I didn't have a function to check flags in my library already, and since I'm not in much of a modding mood right now, I just hard set all the flags from the beginning. If multiple entries of the same flag doesn't matter, I could have just done a table.insert of the relevant flag.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Done] How to Add an additional flag property

Post by eradicator »

bobingabout wrote: Now you tell me.
[...]I could have just done a table.insert of the relevant flag.
You never asked. But you're welcome of course :P. And it's never too late to fix your code =). It's just the same principle that's all over factorio, data the engine doesn't look for is ignored. Like additional data in prototypes etc.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: [Done] How to Add an additional flag property

Post by TheSAguy »

Eradicator,

Someone just posted this crash, the only one from a lot of downloads:
23.038 Error ModManager.cpp:1024: Failed to load mod "Bio_Industries": __Bio_Industries__/data-final-fixes.lua:380: bad argument #1 to 'insert' (table expected, got nil)

Line 380 is: "table.insert(bot.flags,"not-flammable")" from below


My code is:

Code: Select all

-- Logistic & Construction bots can't catch fire or be Mined
	local function immunify(bot)
	  table.insert(bot.flags,"not-flammable")
	  table.insert(bot.resistances, {type = "fire", percent = 100})
	  bot.minable = nil
	  end

	--catches modded bots too
	for _,bot in pairs(data.raw['construction-robot']) do
		immunify(bot)
	end

	for _,bot in pairs(data.raw['logistic-robot']) do
		immunify(bot)
	end
	
I don't see anything wrong, was wondering if you do.
Thanks.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: [Done] How to Add an additional flag property

Post by darkfrei »

TheSAguy wrote: bad argument #1 to 'insert' (table expected, got nil)

Code: Select all

table.insert(bot.flags,"not-flammable")
Your bot.flags is nil.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Done] How to Add an additional flag property

Post by eradicator »

The error message clearly states that "bot.flags" is nil. Probably a modded bot that doesn't have any flags, you'll need to initialize the table.

Code: Select all

if not bot.flags then bot.flags = {} end
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: [Done] How to Add an additional flag property

Post by TheSAguy »

Got it!
Thanks.

New Code:

Code: Select all

-- Logistic & Construction bots can't catch fire or be Mined
	local function immunify(bot)
	  if not bot.flags then bot.flags = {} end
	  if not bot.resistances then bot.resistances = {} end
	  table.insert(bot.flags,"not-flammable")
	  table.insert(bot.resistances, {type = "fire", percent = 100})
	  bot.minable = nil
	  end

	--catches modded bots too
	for _,bot in pairs(data.raw['construction-robot']) do
		immunify(bot)
	end

	for _,bot in pairs(data.raw['logistic-robot']) do
		immunify(bot)
	end
	
Post Reply

Return to “Modding help”