LuaSurface points to a surface at index, and not to unique surface

Place to report issues and suggest improvements to the API documentation.
Post Reply
User avatar
Stringweasel
Filter Inserter
Filter Inserter
Posts: 320
Joined: Thu Apr 27, 2017 8:22 pm
Contact:

LuaSurface points to a surface at index, and not to unique surface

Post by Stringweasel »

What
A variable holding a LuaSurface does not point to that unique created surface, but instead seems to point to whichever surface has that specific index. For example if a the variable's surface is destroyed, and a new different surface created later, it might have the same index. And in that case the original variable will point to the newer surface.

This can easily be seen using the following commands (on a fresh vanilla save). Create initial surface

Code: Select all

/c foo = game.create_surface("foo")
/c game.print("Foo: "..(foo.valid and foo.name.." "..foo.index or "invalid"))
this will print "Foo: foo 2" as expected.

Now delete the surface

Code: Select all

/c game.delete_surface(foo)
/c game.print("Foo: "..(foo.valid and foo.name.." "..foo.index or "invalid"))
which will then of course print "Foo: invalid". Meaning a variable that was previously invalid became valid again.

Now for the unintuitive part, create what the modder might believe is a different surface

Code: Select all

/c bar = game.create_surface("bar")
/c game.print("Foo: "..(foo.valid and foo.name.." "..foo.index or "invalid"))
This will print: "Foo: bar 2"

Why
Modders can incorrectly assume (like I did :lol: ) that a LuaSurface acts just like a LuaEntity, and when destroyed it will remain invalid. And this can create hard to track down cache-invalidation bugs (like one I caused :? ). I know this is probably due to how the underlying C++ surface implementation works and is probably not a bug. Might just be useful to explicitly state it in the docs, especially since mods are really starting to embrace the multiple surfaces.
Alt-F4 Author | Factorio Modder
Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock | Weasel's Demolition Derby

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2252
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: LuaSurface points to a surface at index, and not to unique surface

Post by boskid »

You are right, LuaSurface uses SurfaceIndex for binding and grabs surface by index every time surface is needed. For 2.0 i am changing LuaSurface to use a targeter to surface which will change this behavior to the one you expected: when a surface is deleted, LuaSurface that was pointing at it will become invalid permanently.

User avatar
Stringweasel
Filter Inserter
Filter Inserter
Posts: 320
Joined: Thu Apr 27, 2017 8:22 pm
Contact:

Re: LuaSurface points to a surface at index, and not to unique surface

Post by Stringweasel »

Great! That would definitely make it easier to work with.

Does this mean indexes will always be unique too, like unit numbers? Or will they still be re-used? It would be nice if they were unique to use as an identifier. Can't use the surface name because that can change.
Alt-F4 Author | Factorio Modder
Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock | Weasel's Demolition Derby

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2252
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: LuaSurface points to a surface at index, and not to unique surface

Post by boskid »

Stringweasel wrote:
Sun Apr 21, 2024 7:26 am
Does this mean indexes will always be unique too, like unit numbers? Or will they still be re-used? It would be nice if they were unique to use as an identifier. Can't use the surface name because that can change.
No. It means that LuaSurface will get invalid permanently when surface gets deleted. I am not touching surface indexes logic. There is a vector of surfaces indexed by surface index so in order for this vector to not grow unreasonably when surfaces are created and destroyed repeatedly, indexes are reused.

Post Reply

Return to “Documentation Improvement Requests”