Improve Lua ECS #29
Labels
No Label
Kind/Breaking
Kind/Bug
Kind/Documentation
Kind/Enhancement
Kind/Feature
Kind/Security
Kind/Testing
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Reviewed
Confirmed
Reviewed
Duplicate
Reviewed
Invalid
Reviewed
Won't Fix
Status
Abandoned
Status
Blocked
Status
Need More Info
No Milestone
No project
No Assignees
1 Participants
Notifications
Total Time Spent: 17 hours 30 minutes
Due Date
SeanOMik
17 hours 30 minutes
No due date set.
Dependencies
No dependencies set.
Reference: SeanOMik/lyra-engine#29
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Currently ECS in Lua scripts is a bit wonky. The engine's script api is very different than the rust api. To query for the
Transform
andWorldTransform
of entities:This limits the api to not support filters, like
Changed<T>
and resources. It would be better if the API was more like the Rust's one, where you have to create a view and then use that view, or like nidorx/ecs-lua which has a similar method.Figured out the majority of the new ecs api. Now I need to add support for querying resources from the world, alongside the components of an entity.
Here's a short code example that does the same thing as the code snippet above:
Querying resources from Views is now possible:
The function
Res
, is a helper function written in Lua that just runsResQuery.new(resource)
.The
ResQuery
is implemented in Rust as UserData. The way the queries work is that when theViewResult
is created, it iterates through the args and checks if it finds an internal function on the Lua table or userdata. The function is__lyra_internal_ecs_query_result
(or the constFN_NAME_INTERNAL_ECS_QUERY_RESULT
).The internal function must return a Lua value, and take in a single argument:
ScriptWorldPtr
. Ifnil
is returned, the query in aView
will not provide any results. If it returns a boolean, the result will act as a filter, and the value will not be in the result. Any other value will be included in the result.Now I want to create some filters, like
Changed<T>
!I finished writing the
ChangedQuery
for Lua. In the process of writing it, I had a difficult to diagnose issue where the query wouldn't detect changes like 50% of the time. I eventually figured it out, it was because thenext
function fromViewResult
would return if the query for ANY entity returned nothing, even if another entity would return something.Fixing that also made it so I had to change the behavior of
FN_NAME_INTERNAL_ECS_QUERY_RESULT
a bit. First is that the signature of the function changed; its now:fn(ScriptWorldPtr, Entity) -> LuaValue
. Now for the behavior changes:nil
is returned, its considered that the query will not result in anything for thisView
, no matter the entity. When the query is used in aView
and returnsnil
, it will NOT check for other entities. This is used in theResQuery
Lua query. If theresource is missing, it will ALWAYS be missing for the
View
, no matter the entity.false
, other entities will be checked by theView
.I may create a
QueryResult
enum that could make this a bit simpler and less ambiguous.Here's a Lua code example using the new query!
Now I need to expose a couple of other ECS things to Lua:
ViewOne
for querying from a specific entity.Has
Not
Or
Optional
TickOf
world:current_tick()
)QueryResult
enum for simplifying the implementation of queriesAll the todo's I left in the previous comment have been completed! I also wrote lua annotations for everything that was added. Here's some code using most of the new features:
I think this will mark the completion of this issue. A lot was done and this makes the ECS a lot nicer to use in Lua.
Merged with #30