Comments (2)
@ProgrammingLife so couldn't you instead store the matrix as a flat array?
// to flatten:
const matrix = [[1,2,3],[4,5,6],[7,8,9]];
matrix.flat();
// and unflat:
const flat = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const newArray = [], size = 3;
while (flat.length > 0) newArray.push(flat.splice(0, size));
I'd assume that a flattened list is easier to store mem-wise.
from bitecs.
A multidimensional array wouldn't help here. No matter what, a Component's data in bitECS has always a defined size.
My two cents is that bitECS's design is fundamentally incompatible with data of variable/unknown length, so this type of data generally does not belong in the ECS. If you really need variable data, you should store it outside of the ECS:
const Waypoints = defineComponent();
const hasWaypoints = defineQuery([Waypoints])
const oldWaypoints = exitQuery(hasWaypoints);
const waypointPositions = new Map<number, Float32Array[]>();
function waypointsGCSystem(world) {
const exited = oldWaypoints(world)
for (let i = 0; i < exited.length; i++) {
const eID = exited[i];
// When the component is removed, i.a. when the entity stops existing, we can remove its waypoints
waypointPositions.delete(eID);
}
}
function addWaypoint(world, eID, x, y) {
// Ensure the GC system knows to check this entity
if(!hasComponent(world, Waypoints, eID) addComponent(world, Waypoints, eID)
// Ensure the allEntityWaypoints array exists
if(!waypointPositions.has(eID)) waypointPositions.set(eID, []);
const allEntityWaypoints = waypointPositions.get(eID)!;
const position = new Float32Array(2);
position[0] = x;
position[1] = y;
allEntityWaypoints.push(position);
}
function getWaypoints(eID) {
return waypointPositions.get(eID);
}
Note that you do not even need a Component here: The Waypoints
tag is just so that we know we can remove all waypoints when the entity is removed, but you could do that deletion manually somewhere else, or not at all depending on your use case.
Of course, this is less performant that bitECS's structs of arrays, but their performance comes from the fact that they are fixed-size and can therefore be represented through ArrayBuffers. Making the choice of what tradeoffs to make is up to you: a limited amount of waypoints that use up more memory, but are very fast, or a slower approach that can have an unlimited amount of waypoints? The choice is yours.
from bitecs.
Related Issues (20)
- Query operators HOT 2
- addComponent with initial value HOT 2
- null entity id HOT 1
- Serialize/Deserialize a single entity with components? HOT 1
- query for newly added Component
- Resizing smaller than the defaultSize always errors
- getEntityComponents does not return component name HOT 1
- How to push / pop from a list? HOT 1
- Query for mixed composition with Change HOT 1
- Could not find declaration file for module 'bitecs' HOT 1
- Is it necessary to make a query on every frame tick or are there any optimization tricks to avoid it if nothing changed? HOT 2
- defineComponent size not respected
- How to get MAX number for the current type? HOT 1
- Please publish a new version HOT 2
- Missing `readonly` in ComponentType
- Component sizes, multiple worlds HOT 1
- provide ablity to check whether component field addresses eid HOT 2
- Using `Changed` on tag components breaks deserialization HOT 1
- hasComponent issue when component was added through a Proxy
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bitecs.