Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique table identifier? #1555

Open
UltraEngine opened this issue Nov 17, 2023 · 2 comments
Open

Unique table identifier? #1555

UltraEngine opened this issue Nov 17, 2023 · 2 comments

Comments

@UltraEngine
Copy link

I am writing some code to convert a sol::table into a C++ structure, and I want to prevent infinite recursive loops from forming when a table has itself somewhere in one of its own keys, or in the sub-structure.

What can I use as a unique identifier for the table? I tried registry_index() but I thing this value changes as I am traversing through the data...

@UltraEngine
Copy link
Author

Maybe sol::table::pointer does this?

@Rochet2
Copy link

Rochet2 commented Nov 17, 2023

Not 100% sure, but fairly sure there is no unique value you can get from the lua API.
You would need to compare the values using lua equality operation to determine if two tables are equal (lua_rawequal). You can then generate a running number yourself to keep track of the references.

Another way is to instead keep track of the data in lua. With a lua table you can store the table itself as the key and then later when you want to check if a table was already seen, you just look it from the table (example).

A quick and dirty alternative might be to use tostring() on the table. This will return a string similar to table: 0x1b85730 where the value is the address of the table I recon. However, a table can override the tostring operation with a metafield __tostring .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants