[Solved] Cycle through all damage types

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:

[Solved] Cycle through all damage types

Post by TheSAguy »

Is there an easy way to cycle through all damage types and apply a resistance to an entity?

Currently I'm having to add each damage type at a time.

Thanks.
Last edited by TheSAguy on Fri Jul 29, 2016 7:29 pm, edited 1 time in total.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Cycle through all damage types

Post by aubergine18 »

Here's a list of all damage types if that helps?

Code: Select all

data:extend(
{
  {
    type = "damage-type",
    name = "physical"
  },
  {
    type = "damage-type",
    name = "impact"
  },
  {
    type = "damage-type",
    name = "poison"
  },
  {
    type = "damage-type",
    name = "explosion"
  },
  {
    type = "damage-type",
    name = "fire"
  },
  {
    type = "damage-type",
    name = "laser"
  },
  {
    type = "damage-type",
    name = "acid"
  },
  {
    type = "damage-type",
    name = "electric"
  }
}
)
If you could post some code (or pastebin.com it) to make it clearer what you're trying to achieve, it will be easier to post more specific infos.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Cycle through all damage types

Post by TheSAguy »

I have the below function that can add resistance to items, but I need to specify each damage type.
Was just wondering if there was a way to auto do all types of damage.

Code: Select all

-- Adds a resitance to an entity
function NE_Functions.Add_Damage_Resists(D_Type,Raw,Percent)
	if data.raw["damage-type"][D_Type] ~= nil then
		local Resist = {type = D_Type,percent = Percent}
		for i,d in pairs(Raw) do
			if d.resistances ==nil then d.resistances={} end
			table.insert(d.resistances, Resist)
		end
	end
end
I feed the above with:

Code: Select all

	NE_Functions.Add_Damage_Resists("poison",data.raw["wall"],(25/NE_Difficulty))
	NE_Functions.Add_Damage_Resists("poison",data.raw["gate"],(25/NE_Difficulty))
	NE_Functions.Add_Damage_Resists("poison",data.raw["car"],(25/NE_Difficulty))
	NE_Functions.Add_Damage_Resists("poison",data.raw["electric-pole"],100)
.....

	NE_Functions.Add_Damage_Resists("acid",data.raw["wall"],(25/NE_Difficulty))
	NE_Functions.Add_Damage_Resists("acid",data.raw["gate"],(25/NE_Difficulty))
	NE_Functions.Add_Damage_Resists("acid",data.raw["car"],(25/NE_Difficulty))

....
and so on.
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Cycle through all damage types

Post by orzelek »

You should be able to use for with pairs normally on data.raw["damage-type"].
So this should work:

Code: Select all

for k, v in pairs(data.raw["damage-type"]) do
-- k is a damage type name
end
In v there is a table which also contains name and type as values.
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Cycle through all damage types

Post by bobingabout »

You should be able to iterate through the damage types with...

Code: Select all

for i, dtype in pairs(data.raw["damage-type"]) do
end
Though without trying it, I can't remember if i or dtype holds the damage type's name, I think i.

EDIT: I'm a bit slow there, but looks like from the previous post that in my example it would be i.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Cycle through all damage types

Post by TheSAguy »

Okay, getting an error:
I'm feeding the below function with this: NE_Functions.Add_ALL_Damage_Resists(data.raw["straight-rail"],100)

This is my code:

Code: Select all

-- Adds a resitance of all damage types to an entity
function NE_Functions.Add_ALL_Damage_Resists(Raw,Percent)
	if Raw ~= nil then	
		for k, v in pairs(data.raw["damage-type"]) do	
			local Resist = {type = v, percent = Percent}
			for i,d in pairs(Raw) do
				if d.resistances == nil then d.resistances={} end
				table.insert(d.resistances, Resist)
			end
		end
	end
end

Error:
Image
Attachments
error
error
error.png (6 KiB) Viewed 2214 times
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Cycle through all damage types

Post by prg »

The damage name is the key, not the value in the damage-type table. Try

Code: Select all

local Resist = {type = k, percent = Percent}
(Or v.name would also work. Just not v alone.)
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Cycle through all damage types

Post by TheSAguy »

prg wrote:The damage name is the key, not the value in the damage-type table. Try

Code: Select all

local Resist = {type = k, percent = Percent}
(Or v.name would also work. Just not v alone.)
That did it,
Thanks.
Post Reply

Return to “Modding help”