implement __pairs for entity, entity_components, etc#256
Merged
daid merged 2 commits intodaid:masterfrom Jun 12, 2025
Merged
Conversation
Contributor
Author
|
with this PR: -- basic table dump function
function dump(t, level)
if type(t) ~= "table" and type(t) ~= "userdata" then
return tostring(t)
end
level = level or 0
local entries = {}
for k, v in pairs(t) do
table.insert(entries, dump(k, level+1) .. "=" .. dump(v, level+1))
end
local pfx = ""
if type(t) == "userdata" then
pfx = tostring(t):gsub(":.*", "")
end
local indent = ("\t"):rep(level)
return pfx .. "{\n\t" .. indent .. table.concat(entries, ",\n\t" .. indent) .. "\n" .. indent .. "}"
end
function init()
entity = createEntity()
entity.foo = 42
entity.bar = {x=1, y=2, z=3}
entity.components.callsign = { callsign = "Test Entity" }
entity.components.hull = {
current = 200,
max = 250,
allow_destruction = false,
}
entity.components.shields = {
{ max = 100, level = 100 },
{ max = 100, level = 99 },
{ max = 100, level = 98 },
{ max = 100, level = 97 },
}
print(dump(entity))
endprints: |
Owner
|
Masterful! I need to give it a proper review, but this was on my wishlist! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
most of the implementations are just "make a table with all the values and delegate to
next", with the exception of entity which is "return the components key then delegate tonextwith the LTC table".if the C++ had a deterministically-ordered list of the keys, the functions could be smarter, but this works and the tables should be sufficiently temporary for it to not matter.