Page 1 of 1

[Done] How to Add an additional flag property

Posted: Wed Jun 06, 2018 5:48 pm
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

Re: How to Add an additional flag property

Posted: Wed Jun 06, 2018 5:58 pm
by Klonan

Code: Select all

table.insert(entity.flags, "not-flammable")

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

Posted: Wed Jun 06, 2018 6:39 pm
by TheSAguy
Thanks!

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

Posted: Wed Jun 06, 2018 6:46 pm
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.

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

Posted: Wed Jun 06, 2018 9:02 pm
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.

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

Posted: Thu Jun 07, 2018 6:19 am
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.

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

Posted: Thu Jun 07, 2018 5:26 pm
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.

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

Posted: Thu Jun 07, 2018 5:43 pm
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.

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

Posted: Fri Jun 08, 2018 3:53 pm
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.

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

Posted: Fri Jun 08, 2018 7:02 pm
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

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

Posted: Fri Jun 08, 2018 9:25 pm
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

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

Posted: Mon Jun 11, 2018 8:34 am
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.

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

Posted: Mon Jun 11, 2018 1:16 pm
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.

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

Posted: Tue Jun 12, 2018 3:48 pm
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.

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

Posted: Tue Jun 12, 2018 4:50 pm
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.

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

Posted: Tue Jun 12, 2018 4:54 pm
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

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

Posted: Tue Jun 12, 2018 5:39 pm
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