When is a factorio-object "invalid"?

Place to get help with not working mods / modding interface.
Post Reply
Xyfi
Inserter
Inserter
Posts: 40
Joined: Tue May 20, 2014 7:10 am
Contact:

When is a factorio-object "invalid"?

Post by Xyfi »

In 11.14 there was the following addition to lua-objects in factorio:
Reworked all LuaObjects that can be invalid to support the "valid" readable property.
What does this property mean? I've been trying to fix up a mod (Filtered splitters by ThaPear) for 0.12.1 to check how it works but for some reason if it check whether a entityis valid it returns false.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: When is a factorio-object "invalid"?

Post by Klonan »

I think a object if valid if it still exists.

So in a few mods i made, i would add them to a global index when they are created. If they were then deconstructed, the global entry would become invalid for that entity.
If i didn't check if the entity was valid, then i would get an nil reference error

Xyfi
Inserter
Inserter
Posts: 40
Joined: Tue May 20, 2014 7:10 am
Contact:

Re: When is a factorio-object "invalid"?

Post by Xyfi »

So pretty straight forward, I think I just made a mistake then. Thank you.

johanwanderer
Fast Inserter
Fast Inserter
Posts: 157
Joined: Fri Jun 26, 2015 11:13 pm

Re: When is a factorio-object "invalid"?

Post by johanwanderer »

Xyfi wrote:So pretty straight forward, I think I just made a mistake then. Thank you.
Think of invalid objects as dangling pointer references. There are quite a few way an object can be removed from the game (and not all of it will be under your control, so it might not be your fault*.) so the ".valid" attribute allows you to do some defensive programming. Obviously, it's best if you don't have to deal with it, but sometimes the cost of checking ".valid" may be less than handling on_destroyed() for everything in the game.

* For example, if I write a "bomb" mod that destroys everything in a given radius, including your object, you might not know that your object is removed from the game until you check its valid status.

Post Reply

Return to “Modding help”