Page 1 of 1

[Done] Preventing Multiple Table Entries

Posted: Thu Jul 12, 2018 5:41 pm
by TheSAguy
Hi,

I'm trying to add a resistance type "ne_fire" if the current entity already had the resistance type "fire".
I was able to accomplish that, by using the below code:

Code: Select all


for i = 1, ent_count do
		
	--- Add the same amount of "ne_fire" Resistances as the current "fire" Resistances
	for _, ent_type in pairs(data.raw[entity_types_list[i]]) do
			
		if ent_type.resistances then
			
			for _, r_type in pairs(ent_type.resistances) do	
				if r_type.type == "fire" and r_type.percent then
					table.insert(ent_type.resistances, {type = "ne_fire", percent = r_type.percent})
				end
			end
			
		end
		
	end
		
end

This works, but then I ran into the following situation. It does not check if the entity already has "ne_fire" as a resistance and if it has, it should see if that "ne_fire" is higher or lower than "fire" and if lower, update.

So in the below example, I added 50% "ne_fire", though the entity already had it.

Code: Select all

data.raw["unit-spawner"]["ne-spawner-pink"].resistances[3].type = "fire" 
data.raw["unit-spawner"]["ne-spawner-pink"].resistances[3].decrease = 5 
data.raw["unit-spawner"]["ne-spawner-pink"].resistances[3].percent = 50 
data.raw["unit-spawner"]["ne-spawner-pink"].resistances[4].type = "ne_fire" 
data.raw["unit-spawner"]["ne-spawner-pink"].resistances[4].percent = 100 
data.raw["unit-spawner"]["ne-spawner-pink"].resistances[5].type = "ne_fire" 
data.raw["unit-spawner"]["ne-spawner-pink"].resistances[5].percent = 50
So I need to check if "ne_fire" is present and then check if that is higher or lower than "fire" and update if lower.

Not sure how to add this check in my code.
Thanks.

Re: Preventing Multiple Table Entries

Posted: Thu Jul 12, 2018 6:13 pm
by eradicator

Code: Select all

local fire, nefire --store resistance subtables
for _,res in pairs(resistances) do
  if res.type == 'fire' then
    if res.percent then
      fire = res --has fire and percent, store subtable for later use
      end
  elseif res.type == 'nefire' then
    nefire = res --has nefire, store for later use
    end
  end
if fire then
  if nefire then 
    if nefire < NEWVALUE then
      nefire.percent = NEWVALUE --update old nefire
      end
  else
    table.insert(resistances,{name='nefire',percent=NEWVALUE}) --add new nefire
    end
  end

Re: Preventing Multiple Table Entries

Posted: Fri Jul 13, 2018 1:33 am
by TheSAguy
Thanks Eradicator! below code works great!

Code: Select all

			local fire, nefire --store resistance subtables
			for _,res in pairs(ent_type.resistances) do
				  if res.type == 'fire' then
					if res.percent then
					  fire = res --has fire and percent, store subtable for later use
					  NEWVALUE = res.percent
					  end
				  elseif res.type == 'ne_fire' then
					nefire = res --has nefire, store for later use
					end
				  end
			if fire then
			  if nefire then 
				if nefire.percent < NEWVALUE then
				  nefire.percent = NEWVALUE --update old nefire
				  end
			  else
				table.insert(ent_type.resistances,{type='ne_fire',percent=NEWVALUE}) --add new nefire
				end
			  end

Re: Preventing Multiple Table Entries

Posted: Fri Jul 13, 2018 9:10 am
by eradicator
TheSAguy wrote:

Code: Select all

NEWVALUE = res.percent
Are..you..is..that...a..global...no, i don't even wanna know...
:sadface:

But in the sole interest of your users. *sigh*...
Please replace all occurances of "NEWVALUE" (it's written in caps, do you even see that?) by "res.percent".

And please..before bothering to ask "why"... think about it. Just. Think about it.