Page 1 of 1

[Solved] Cycle through all damage types

Posted: Tue Jul 26, 2016 8:45 pm
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.

Re: Cycle through all damage types

Posted: Tue Jul 26, 2016 11:33 pm
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.

Re: Cycle through all damage types

Posted: Wed Jul 27, 2016 5:16 am
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.

Re: Cycle through all damage types

Posted: Wed Jul 27, 2016 8:13 am
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.

Re: Cycle through all damage types

Posted: Wed Jul 27, 2016 8:24 am
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.

Re: Cycle through all damage types

Posted: Fri Jul 29, 2016 1:30 pm
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

Re: Cycle through all damage types

Posted: Fri Jul 29, 2016 1:41 pm
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.)

Re: Cycle through all damage types

Posted: Fri Jul 29, 2016 7:30 pm
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.