Page 1 of 1

Checking emissions of an entity (for Worm Attack mod)

Posted: Sat Apr 02, 2016 3:08 pm
by iamseph
Hi I'm working on my Worm Attack mod and I want to make it so worms will prioritise structures which produce emissions. I haven't been able to work out how to do this without an error so am hoping someone could help.

This was my last attempt:

Code: Select all

							if targets.energy_source ~= nil then
								if targets.energy_source.emissions ~= nil then
									signature = signature + (targets.energy_source.emissions * 50000)
									-- eg a mining drill produces 0.15/15=0.01 per tick , so it gets (100 health + 50000*0.01) = 600 signature
									end
								end
But it keeps throwing this error when it tries to check if the energy_source is nil
LuaEntity doesn't contain key energy_source.
and I don't know how else to check whether it exists within the entity before trying to get the value.

Re: Checking emissions of an entity (for Worm Attack mod)

Posted: Sat Apr 02, 2016 3:39 pm
by prg
Maybe entity.prototype.emissions_per_tick?

Re: Checking emissions of an entity (for Worm Attack mod)

Posted: Sat Apr 02, 2016 4:02 pm
by iamseph
That works, but that's just for trees. Furnaces etc don't use it.

Re: Checking emissions of an entity (for Worm Attack mod)

Posted: Fri Apr 15, 2016 11:07 am
by Darloth
I can't test it at the moment so this might just be garbage I've pieced together from remembered Lua knowledge and some internet crosschecking...

But, does

Code: Select all

if targets["energy_source"] ~= nil then
change anything? I don't really see why it should, but maybe it's worth trying.

Re: Checking emissions of an entity (for Worm Attack mod)

Posted: Sat Apr 16, 2016 10:07 am
by Darloth
If nothing else works, you could also try putting the nil check in its own function (possibly inline) and just using pcall to see whether it worked. I'm not sure how expensive that sort of try-catch behaviour is in Lua, but if nothing else works it could be a useful fallback. Of course, I'm not sure if Factorio's lua implementation supports it at all, but I imagine it should do.