More efficient ways to JECS Query
I have an query using JECS that runs my NPC behavior. The goal is to select a random point on the map to roam too.
this seems very slow, is it possible to just grab a table of transform components with a TycoonTags.floor tag?
My immediate thought for now is to implement cacheing because right now I am doing this 20 times a second š
Suggestions would be appreciated š !
Solution:Jump to solution
```ts
const archetypes = query.archetypes()
for(const archetype of archetypes){
const index = archetype.records[transform]...
123 Replies
yes, you can directly get the archetypes
and then you'd get them as a simple array which is cool
I dont know of roblox-ts has any of these types though
it's more internal
oh is that what .archetypes() does?
not quite, you would get the archetypes

then you iterate all the archetypes
for each archetype, you can get where the transform is by doing
archetype.records[transform]
then you can directly access the array in colums[archetype.records[transform]]
I see, something like this:
it would cut out the extra iteration
Solution
I see
you would still get different arrays
thats really cool
yeah so after that I guess you can iterate all the arrays, get the combined size of all of them
and use that for the random
and to access the random you will need some simple maths, should be straight forward
didnt realize there was also a counts proeprty too, thats going to really help speed up the counting I do
Thank you!!! Hope this will fix the long execution times in script profiler
I'd recommend printing the archetype, I believe this is how the internals are setup but I might be wrong
XD
but the idea is the same
I ended up having to do const index = archetype.records[sharedComponents.Transform - 1]; because in lua its adding +1, however the reuslt is not a list of CFrames its actually a different component, one that I never spcified in the query:
do you have any thoughts on what might cause this?
shouldnt it be transform + 1?
no + 1 ended up making index = nil
this is what records looks like
and sharedComponents.Transform === 2 but in lua it +1 so I have had to -1 in typescript, but the [2] = 1 gets a different value
oh I see, records is a map I imagine right
in types records is a number[]
its an array yeah, so, when getting the records you'd do
+1
and to get the colum you would do -1
I think
actually no, when getting the records you do +1, but when getting the colum you wouldnt do anything
because that's a real arraybut when getting the record with +1, it ends up being nil which causes the column look up to fail
what's the transform id?
2
but when i print out the columns its actually on 1? i think
{
[1] = ā¼ {
[1] = -825.800049, -267.129486, -1344.05005, 1, 0, 0, 0, 1, 0, 0, 0, 1,
}
theres more than 1 transform, just truncated for message
then I think you shouldnt add anything
or substract anything
then that leads to lua adding +1 and the index will be undefined
https://github.com/Ukendio/jecs/blob/main/jecs.luau#L477
I think it's just roblox-ts arrays messing it up
ah
yeah so I got confused, this seems correct, its pointing to 1, and the colums for transform are in 1
but here after trying to index 1, you would index 2
because it would add one, so you need to substract one in both cases
š that was it! thank you so much
typescript arrays are confusing
or actually luau arrays are confusing
yeah I used to be a lua lover, then I learned typescript š¤£
i cant stand typing or using lua
queries went from 0.5ms to 0.005ms and below so massive difference when I have like 500 npcs running at once š
0.5ms is quite a lot š
well yeah when you multiply it by every npc, now I cant wait to revisit every query and test the script profiler
learning jecs while making the game so theres plenty of agony to go arround
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
What specifically do you want to see?
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
Iām at work
Give me a couple hours
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
isnt this cloning the arrays anyway?
I thought you would just randomly grab one from the current arrays
not like before, i was interating unnecessarily to clone the arrays and filter out things
this way has way less iterations involved
hmm I see
I think you could do it without cloning the arrays
How so? I still have to piece them all together
something like this
needs two iterations but it wont clone arrays
There could be over like 5k floor entities so iterating isnt the best
you are not iterating
you are just iterating the archetypes
note that you are cloning arrays
Oh wait I see
which is iterating
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
second of all?
did I give bad advice
:Babbyblob:
:WOAH:
ahh, just by assigning it to a local variable?
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
like this:
thanks!
i appreciate the advice ya'll, Im still learning this ECS stuff š
same
my game is a little ambitious so i have to put a lot of thought into optimization
yeah this should have almost a constant time complexity
so it should be pretty fast
doesnt matter how many you have
I think
#array
is constant in luau because it gets cachedUnknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
that must be why stuff like this is really expensive in the profiler:
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
ayy! thanks for the heads up!\
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
I have like 10 others just like this š thank you lol!
so something like this would be better practice?
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
oh, well that might be an issue because I am using Planck as a scheduler and it relies on that world
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
does returning a function work with planck add systems? I wasnt aware of that in any documentation
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
switching over now would be time consuming so hopefully there is a way
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
ah, got it
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
i have a little bit of a weird use case. When players load a tycoon it creates a new world every time and then cleans it up
new world + scheduler ^
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
so relying on a shared world in a module wouldn't work for me right now without restructuring
i could potential pass in a table into the system like I do with a Maid already
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
If you can be more specific about what you want to see, sure
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
What about the transform? I am confused
if this is what your asking for, this is how I update workspace based on transform
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
it does pathfinding
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
yeah sure- just lead with that next time
its a helper function
and then it calls a pathfinding module
i built it copying mostly from this awesome algorithm https://love2d.org/forums/viewtopic.php?t=90395.
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
.
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
Was working last night
Their host must be down
I used an a star algorithm
GitHub
GitHub - GlorifiedPig/Luafinding: Fast & easy-to-use A* module writ...
Fast & easy-to-use A* module written in Lua. Contribute to GlorifiedPig/Luafinding development by creating an account on GitHub.
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
Idk
Different probably
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
Here it is
Literally copied it š
All i did is generate some types with chat gpt
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
I have a question about this, I just tried using cache with a query using with and without, the behavior I noticed is that if an entity has those dependents changes, say the query is world.query(transform).cached() if transform is removed, the query still runs as it has not removed. Does this mean I need to perodically (when something worthy changes), re-make the cache? Do I have that right?
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
it would not be the first time I did something wrong!
it was probably an issue with me passing in a big table of cached queries into planck, I will probably slowly move over to a different scheduler
which scheduler do you use? is it the same as the one in the demo?
I use that one
very recommended š
no planck bloat
ill check it out, is it jabby's scheduler? Last time I saw thats what it looked like it called :run on jabby
no jabby scheduler is just an interface
used so jabby can time/disable your schedulers
but it supports returning a function as the main system?
so instead of calling your systems once per frame
you do jabby.scheduler:run(system)
but jabby itself is not a scheduler, its a bad name
you'd still need to call this per frame
but I could do something like:
and be fine?
ya
actually I use the old scheduler of the demo
this is the new one
I'll look at switching over then, at this rate my game is gonna die due to the server lag š¤£
yeah, the old one didnt pass the world in the scheduler
there is always a single world in my game
so I dont need that
that is crucial to me but I would assume it would be easy to do
additionally I need a maid passed in as well to cleanup startup functions
observers, etc
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
understood! I will not
how many times should cache be created anyways?
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
so if an entitiy's components change the cached query will still iterate the changes
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
got it
i appreciate it
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
in OSS?
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
no, this was horrible and its what I did to begin with. made it really hard to scale.
I just use a list of behavior tasks and then check a test to see if its success,failed, or running
Here is my behavior runner that basically just queries over BehaviorStep which stores the current step and the sequence. Think sequence = "Going to Eat" and a step would be "Walking to Cafeteria Table"
The main thing is I return StepStatus.Running if the task needs more time (yields) or is waiting for pathfinding response or waiting for the NPC to actually move to the area
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
Its just a table of functions that return a step status, its very long and has many dependencies on other functions. I pass in the NPC entity ID and world to make changes
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View
Unknown Userā¢3mo ago
Message Not Public
Sign In & Join Server To View