Skip to content

Commit

Permalink
Add back most world operation validators
Browse files Browse the repository at this point in the history
  • Loading branch information
memorycode committed Oct 20, 2024
1 parent b5d68f9 commit a2cf6cb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 40 deletions.
3 changes: 0 additions & 3 deletions benchmarks/query.bench.luau
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ for i = 1, 10_000 do
pinnedWorld:spawnAt(i, pinnedA({}), pinnedB({}))
end

print(world.archetypes, world.archetypes:count())
return {
ParameterGenerator = function()
return
Expand All @@ -28,8 +27,6 @@ return {
for _ in world:query(A, B) do
count += 1
end

print("new", count)
end,
["Old Matter"] = function()
local count = 0
Expand Down
20 changes: 4 additions & 16 deletions benchmarks/stress.bench.luau
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,23 @@ end
print("archetypes:", archetypes)
print(total, "different archetypes")

print("tree:", world.archetypes:count())
local total = 0
for _, count in world.archetypes:count() do
total += count
end
print(total, "archetypes")

return {
ParameterGenerator = function()
return
end,

Functions = {
["Old Matter"] = function()
--local count = 0
local count = 0
for _ in pinnedWorld:query(pinnedB, pinnedA) do
--count += 1
count += 1
end

--print("old:", count)
end,
["New Matter"] = function()
--local count = 0
local count = 0
for _ in world:query(B, A) do
--count += 1
--print("entt", a, b)
count += 1
end

--print("new:", count)
end,
},
}
4 changes: 0 additions & 4 deletions benchmarks/without.bench.luau
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,12 @@ return {
for _ in pinnedWorld:query(pinnedB):without(pinnedC) do
count += 1
end

--print("old:", count)
end,
["New Matter"] = function()
local count = 0
for _ in world:query(B):without(C) do
count += 1
end

print("new:", count)
end,
},
}
8 changes: 0 additions & 8 deletions example/src/shared/start.luau
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,6 @@ local function start(containers)
end)
end

if RunService:IsServer() then
task.spawn(function()
while task.wait(1) do
print(world.archetypes)
end
end)
end

return world, state
end

Expand Down
23 changes: 15 additions & 8 deletions lib/World.luau
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local topoRuntime = require(script.Parent.topoRuntime)

local assertValidComponentInstances = Component.assertValidComponentInstances
local assertValidComponent = Component.assertValidComponent
local assertComponentArgsProvided = Component.assertComponentArgsProvided

type EntityId = Archetype.EntityId
type ComponentId = Archetype.ComponentId
Expand Down Expand Up @@ -55,6 +56,15 @@ type ReplaceCommand = {

type Command = DespawnCommand | InsertCommand | RemoveCommand | ReplaceCommand

local function assertEntityExists(world, id: number)
assert(world:contains(id), "Entity doesn't exist, use world:contains to check if needed")
end

local function assertWorldOperationIsValid(world, id: number, ...)
assertEntityExists(world, id)
assertComponentArgsProvided(...)
end

--[=[
@class World
Expand Down Expand Up @@ -500,6 +510,7 @@ end
function World:replace(id, ...)
local componentInstances = { ... }
assertValidComponentInstances(componentInstances)

bufferCommand(self, { type = "replace", entityId = id, componentInstances = componentInstances })
end

Expand Down Expand Up @@ -551,15 +562,13 @@ end
@param ... Component -- The components to fetch
@return ... -- Returns the component values in the same order they were passed in
]=]
function World.get(self: World, entityId, ...: Component)
local entityRecord = self.allEntities[entityId]
if entityRecord == nil then
error(ERROR_NO_ENTITY, 2)
end
function World:get(entityId, ...: Component)
assertWorldOperationIsValid(self, entityId, ...)

local length = select("#", ...)
local componentInstances = table.create(length, nil)

local entityRecord = self.allEntities[entityId]
local archetype = entityRecord.archetype
local idToIndex = archetype.idToIndex
for i = 1, length do
Expand Down Expand Up @@ -1244,9 +1253,7 @@ end
@param ... ComponentInstance -- The component values to insert
]=]
function World:insert(id, ...)
if not self:contains(id) then
error(ERROR_NO_ENTITY, 2)
end
assertWorldOperationIsValid(self, id, ...)

local componentInstances = { ... }
assertValidComponentInstances(componentInstances)
Expand Down
2 changes: 1 addition & 1 deletion lib/component.luau
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,6 @@ return {
newComponent = newComponent,
assertValidComponentInstance = assertValidComponentInstance,
assertValidComponentInstances = assertValidComponentInstances,
assertValidComponentArgsProvided = assertValidComponentArgsProvided,
assertComponentArgsProvided = assertComponentArgsProvided,
assertValidComponent = assertValidComponent,
}

0 comments on commit a2cf6cb

Please sign in to comment.