Skip to content

implement __pairs for entity, entity_components, etc#256

Merged
daid merged 2 commits intodaid:masterfrom
GinjaNinja32:component-pairs
Jun 12, 2025
Merged

implement __pairs for entity, entity_components, etc#256
daid merged 2 commits intodaid:masterfrom
GinjaNinja32:component-pairs

Conversation

@GinjaNinja32
Copy link
Contributor

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 to next with 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.

@GinjaNinja32
Copy link
Contributor Author

GinjaNinja32 commented Apr 7, 2025

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))
end

prints:

entity{
	components=entity_components{
		hull=hull{
			damaged_by_energy=true,
			max=250.0,
			damaged_by_emp=false,
			allow_destruction=false,
			current=200.0,
			damaged_by_kinetic=true
		},
		callsign=callsign{
			callsign=Test Entity
		},
		shields=shields{
			1=shields_array{
				max=100.0,
				length=4,
				level=100.0
			},
			2=shields_array{
				max=100.0,
				length=4,
				level=99.0
			},
			3=shields_array{
				max=100.0,
				length=4,
				level=98.0
			},
			4=shields_array{
				max=100.0,
				length=4,
				level=97.0
			},
			front_can_be_hacked=true,
			active=true,
			front_heat_add_rate_per_second=0.050000000745058,
                        [... the rest of the shields component fields]
			rear_coolant_level=0.0
		}
	},
	foo=42,
	bar={
		z=3,
		y=2,
		x=1
	}
}

@daid
Copy link
Owner

daid commented Apr 8, 2025

Masterful! I need to give it a proper review, but this was on my wishlist!

@daid daid merged commit 6d05175 into daid:master Jun 12, 2025
6 of 9 checks passed
@GinjaNinja32 GinjaNinja32 deleted the component-pairs branch June 15, 2025 09:45
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

Successfully merging this pull request may close these issues.

2 participants