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.
[Solved] Cycle through all damage types
[Solved] Cycle through all damage types
Last edited by TheSAguy on Fri Jul 29, 2016 7:29 pm, edited 1 time in total.
- aubergine18
- Smart Inserter
- Posts: 1264
- Joined: Fri Jul 22, 2016 8:51 pm
- Contact:
Re: Cycle through all damage types
Here's a list of all damage types if that helps?
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.
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"
}
}
)
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.
Re: Cycle through all damage types
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.
I feed the above with:
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
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.
Re: Cycle through all damage types
You should be able to use for with pairs normally on data.raw["damage-type"].
So this should work:
In v there is a table which also contains name and type as values.
So this should work:
Code: Select all
for k, v in pairs(data.raw["damage-type"]) do
-- k is a damage type name
end
- bobingabout
- Smart Inserter
- Posts: 7352
- Joined: Fri May 09, 2014 1:01 pm
- Contact:
Re: Cycle through all damage types
You should be able to iterate through the damage types with...
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.
Code: Select all
for i, dtype in pairs(data.raw["damage-type"]) do
end
EDIT: I'm a bit slow there, but looks like from the previous post that in my example it would be i.
Re: Cycle through all damage types
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:
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

- Attachments
-
- error
- error.png (6 KiB) Viewed 2221 times
Re: Cycle through all damage types
The damage name is the key, not the value in the damage-type table. Try
(Or v.name would also work. Just not v alone.)
Code: Select all
local Resist = {type = k, percent = Percent}
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Re: Cycle through all damage types
That did it,prg wrote:The damage name is the key, not the value in the damage-type table. Try
(Or v.name would also work. Just not v alone.)Code: Select all
local Resist = {type = k, percent = Percent}
Thanks.