Skip to content

Resetting a world doesn't clear queries properly #180

Description

@Olaroll

I'm making a game and trying to reset the world between matches. When I used world.reset(), all my queries don't seem to get the memo and keep iterating over non-existing entities.

Try out the example below to see what I mean. You can add entities with a button and see their count increase in the console. When using world.reset() you can see that the count doesn't decrease at all. You can see how the button that destroys all entities doesn't have this issue.

For now, I've managed to bypass the issue by destroying all my entities manually.

Other than that though, I've really enjoyed using this library! Thanks for the great work on it :)

Example

import {component, createQuery, createWorld, number, registerSchema, World} from "@javelin/ecs";

const systems = [
	countStuff
]

const world = createWorld({systems})
setInterval(world.step, 1000)


const Stuff = {value: number}
registerSchema(Stuff, 1)

const stuffs = createQuery(Stuff)
function countStuff(world: World) {
	let count = 0
	stuffs((eid, [stuff]) => {
		count++
	})
	console.log("Number of stuffs", count)
}


const body = document.body

body.append(createButton("Add stuff", () => {
	console.log("Added stuff")
	world.create(
		component(Stuff)
	)
}))

body.append(createButton("Destroy all", () => {
	console.log("Destroyed all")
	stuffs.bind(world)((eid) => {
		world.destroy(eid)
	})
}))

body.append(createButton("Reset world", () => {
	console.log("World reset!")
	world.reset()
	world.addSystem(countStuff)
}))


function createButton(text: string, callback: ()=>void) {
	const button = document.createElement("button")
	button.addEventListener("click", callback)
	button.innerText = text
	return button
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions