0

I am creating a caching system for complex shapes in Lua and I want it to be backwards compatible as a plug and play replacement for the default, non cached system, to increase performance. I've successfully rendered an object with 6.4 million points which gets turned into 3.1 million trapezoids to make a shape instead of using 6.4 million triangles, cache it and continue rendering without any performance hit beyond initial caching.

I want to set up a cached data table which uses the reference to the original table as the index. The problem is, if I use tostring, I end up with the entire table as a nicely formatted human-readable string which wouldn't be nice to use as a short index to reference the table.

I do not want to require the dev to assign an ID to the object because the default system doesn't do this, and it is an extra step which can be avoided by simply using the address which is unique to that object anyways..

I can not find a Lua implementation of lua_topointer(L, idx)... but maybe I'm missing something?

A few issues - my tostring no longer shows table: 0x12345678

I have rewritten a lot of meta-functions and __tostrings to output the data for debugging purposes. But, on Lua demo I can't find the exact function which replaces this behavior, and I didn't replace print in my latest version. So print is default, etc...

On Lua demo I have also tried getmetatable table, function m:__tostring( ) return 'blah' end

and print( { } ), print( tostring( { } ) )

The first prints the address. The second prints blah. So because print isn't using tostring, by default, what is it using? Is that accessible via Lua?

If all else fails, I'll probably have to get rid of my __tostring table metamethod and make a secondary function to output that particular data type which defaults the purpose of having a __tostring method for everything ( and the fact that print ignores it; one of the reasons I rewrote print in an old version... but I reverted to keep it default )

So, does anyone know a Lua function I can call, which is not tostring, which can get the address of a table in Lua? It must return the value so it can be used.

Acecool
  • 682
  • 9
  • 12
  • In short: I can use tostring, but that means getting rid of my __tostring function or creating a reference, nulling it, then grabbing the reference and restoring it in a custom function. I'd rather have something less hacky with the ability to keep the __tostring function. – Acecool Jun 01 '20 at 12:27
  • You can add field `id` in your objects (if not exists yet) when you need it for the first time. – Egor Skriptunoff Jun 01 '20 at 12:43
  • Also note: I overwrote tostring so that it would work on a wider variety of objects.. I forgot I added a readonly:Set( 'globals', 'tostring' ) reference to the original which I can use to grab it which lets me keep the extended functionality and a way to call it. – Acecool Jun 01 '20 at 12:45
  • @Egor, thanks, but this would mean doing it for each and every single table created using { }... This extends overhead, but with current memory it wouldn't be terrible even with a lot of tables... but it would still mean assigning them, and keeping track of them or just continuously incrementing and keeping track of that. I forgot that I replaced tostring, so I have a reference to the original with my readonly object which lets me set a keyed value exactly once so I can use that. – Acecool Jun 01 '20 at 12:47
  • I am going to keep this open to see if there is an alternate to tostring. – Acecool Jun 01 '20 at 12:48
  • Or you can store all IDs in a separate weak table. – Egor Skriptunoff Jun 01 '20 at 14:23
  • 3
    Why not use the table itself as a key? If you need to see the table to generate a unique address to use as a key, then using the table itself should work just as well. – luther Jun 01 '20 at 14:24
  • Interesting. Which Lua version are you using? I've checked Lua 5.1, 5.3, and LuaJIT 2.1, and everywhere `print` works as it should, that is, uses `__tostring` metamethod. – un.def Jun 01 '20 at 16:56
  • Just tried it on the Lua demo website. As for the Lua version, it is whichever one is embedded in Garry's Mod. As for using the table as a reference, I am aware in Lua you can use many different data-types as keys, but with the table I wasn't sure if it'd use the reference or the string, or something else.. I'll investigate, if it uses the reference then great... but if it converts to string then not so great. I'll set up an index and newindex function to see what is used. – Acecool Jun 01 '20 at 22:01
  • If you do getmetatable table on Lua Demo, and create a __tostring method which returns 'blah', and you print { } and print tostring { } then only one prints blah, not both. – Acecool Jun 05 '20 at 19:30

0 Answers0