Skip to content

Commit

Permalink
Allow nil indexing in world:target (#131)
Browse files Browse the repository at this point in the history
* Allow nil index in world:target

* Fix the fun type at the bottom

* Update tests
  • Loading branch information
EncodedVenom authored Sep 23, 2024
1 parent 6872944 commit 16e8055
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,21 @@ export class World {
* Gets the target of a relationship. For example, when a user calls
* `world.target(entity, ChildOf(parent))`, you will obtain the parent entity.
* @param entity Entity
* @param index Target index
* @param relation The Relationship
* @returns The Parent Entity if it exists
*/
target(entity: Entity, relation: Entity, index: number): Entity | undefined;

/**
* Gets the target of a relationship. For example, when a user calls
* `world.target(entity, ChildOf(parent))`, you will obtain the parent entity.
* @param entity Entity
* @param relation The Relationship
* @returns The Parent Entity if it exists
*/
target(entity: Entity, relation: Entity): Entity | undefined;

/**
* Clears an entity from the world.
* @param entity Entity to be cleared
Expand Down
5 changes: 3 additions & 2 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ local function world_has(world: World, entity: number, ...: i53): boolean
return true
end

local function world_target(world: World, entity: i53, relation: i24, index: number): i24?
local function world_target(world: World, entity: i53, relation: i24, index_opt: number?): i24?
local index = if index_opt then index_opt else 0
local record = world.entityIndex.sparse[entity]
local archetype = record.archetype
if not archetype then
Expand Down Expand Up @@ -1812,7 +1813,7 @@ export type World = {
component: <T>(self: World) -> Entity<T>,
--- Gets the target of an relationship. For example, when a user calls
--- `world:target(id, ChildOf(parent), 0)`, you will obtain the parent entity.
target: (self: World, id: Entity, relation: Entity, nth: number) -> Entity?,
target: (self: World, id: Entity, relation: Entity, nth: number?) -> Entity?,
--- Deletes an entity and all it's related components and relationships.
delete: (self: World, id: Entity) -> (),

Expand Down
19 changes: 19 additions & 0 deletions test/tests.luau
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,25 @@ TEST("world:target", function()
CHECK(world:target(e, C, 1) == nil)
end

do CASE "infer index when unspecified"
local world = world_new()
local A = world:component()
local B = world:component()
local C = world:component()
local D = world:component()
local e = world:entity()

world:add(e, pair(A, B))
world:add(e, pair(A, C))
world:add(e, pair(B, C))
world:add(e, pair(B, D))
world:add(e, pair(C, D))

CHECK(world:target(e, A) == world:target(e, A, 0))
CHECK(world:target(e, B) == world:target(e, B, 0))
CHECK(world:target(e, C) == world:target(e, C, 0))
end

do CASE "loop until no target"
local world = world_new()

Expand Down

0 comments on commit 16e8055

Please sign in to comment.