[Done] Preventing Multiple Table Entries

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:

[Done] Preventing Multiple Table Entries

Post 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.
Last edited by TheSAguy on Fri Jul 13, 2018 1:33 am, edited 1 time in total.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Preventing Multiple Table Entries

Post 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
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Preventing Multiple Table Entries

Post 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
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Preventing Multiple Table Entries

Post 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Post Reply

Return to “Modding help”