Entity without name?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Entity without name?

Post by L0771 »

Hi, i have a problem, here's my code

Code: Select all

			local del = {-1}
			for i = 1, #blood do
				if blood[i] ~= nil and blood[i].entity ~= nil then
					if blood[i].time <= 0 then
						if blood[i].entity then
							if blood[i].entity.name == "cursed-blood" then <-- LuaEntity API call when entity target is zero.
								local pos = blood[i].entity.position
								blood[i].entity.destroy()
								blood[i].entity = game.createentity {name="cursed-blood-steam",position=pos, force=game.forces.neutral}
								blood[i].entity.destructible = false
								blood[i].total = blood[i].total * 0.75
								blood[i].time = 1
							else
								del[#del + 1] = i
							end
						else
							del[#del + 1] = i
						end
					else
						blood[i].time = blood[i].time - 1
					end
				else
					del[#del + 1] = i
				end
			end
I have a problem with this, the 6th line sometimes return "LuaEntity API call when entity target is zero."
i can't reproduce this error, but i have differents players with same error.

i check if the entity exist, and i don't delete a entity on the for, i have no idea for what is it.
Someone have any idea?

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Entity without name?

Post by FreeER »

L0771 wrote:i check if the entity exist
To be technical (and programming is ALL about technicalities), you check that the reference to an entity exists with "blood.entity ~= nil" and a couple lines down with "if blood.entity then" but you never check that the entity that reference is for still exists within the game with "blood.entity.valid". Just because you have a reference to an entity does not mean that that entity has not died or been mined (when it has though the reference's valid method will return false). Valid is the only method you can call on a reference to an entity that has been destroyed/mined, anything else tries to access the actual C++ entity for information and when that entity doesn't exist anymore in the game, well, you've seen the error :)
Most of the time the difference between the reference and the actual entity is unimportant to us, but this is one of those times that it makes a difference :)

P.S. if del is for deleting entities from the table you could also loop through blood backwards and just use table.remove from inside the loop (looping backwards doesn't have the problem of skipping the next entry when you remove one).

User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Entity without name?

Post by L0771 »

Sorry for taking so long to reply.
Thanks, now I try this.

Post Reply

Return to “Modding help”