Skip to content

Conversation

@BMagnu
Copy link
Member

@BMagnu BMagnu commented Jan 10, 2026

Consider the following script:

table = {mn.Teams["Friendly"] = 0}
print(table[mn.Teams["Friendly"]]) -- will print nil

This rather unintuitive behaviour is because lua table lookups are by rawequal. As teams, and basically all FSO objects, are userdata, their rawequal check does not call metamethods, and thus does not respect the built-in equality operators for FSO userdata. Instead, userdata is just checked against same instance, which is not the case for the example script.
This PR adds a metatable which changes this by explicitly calling == instead, overwriting or querying entries that are semantically equal but not rawequal.
When using FSO-userdata as table keys, this is almost always the intended behaviour (though with correct design in some circumstances rawequal can suffice as well, but semantically equal would work just as well).

So with this PR, the upper example could be made to work as follows:

table = {mn.Teams["Friendly"] = 0}
setmetatable(table, Globals.FSOObjectsTableMetatable)
print(table[mn.Teams["Friendly"]]) -- will print 0

Of course, a user could write / paste the entire metatable and not need this PR, but its such a common usecase and footgun that I'd like to include it in our globals.

@BMagnu BMagnu added scripting A feature or issue related to LUA scripting Waiting for Stable Marks a pull request that is to be merged after the next stable release, due to a release cycle labels Jan 10, 2026
Copy link
Contributor

@Goober5000 Goober5000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be very useful 👍

@BMagnu BMagnu marked this pull request as draft January 12, 2026 23:17
@BMagnu
Copy link
Member Author

BMagnu commented Jan 12, 2026

Found a possible bug

@BMagnu BMagnu marked this pull request as ready for review January 12, 2026 23:22
@BMagnu
Copy link
Member Author

BMagnu commented Jan 12, 2026

Fixed, dunno how this didn't came up in the initial tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scripting A feature or issue related to LUA scripting Waiting for Stable Marks a pull request that is to be merged after the next stable release, due to a release cycle

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants