Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resetting a world doesn't clear queries properly #180

Open
Olaroll opened this issue Nov 5, 2022 · 1 comment
Open

Resetting a world doesn't clear queries properly #180

Olaroll opened this issue Nov 5, 2022 · 1 comment

Comments

@Olaroll
Copy link

Olaroll commented Nov 5, 2022

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
}
@Olaroll Olaroll changed the title Resetting a world doesnt clear queries properly Resetting a world doesn't clear queries properly Nov 7, 2022
@3mcd
Copy link
Owner

3mcd commented Nov 8, 2022

Hey @Olaroll, thank you for reporting this issue! It's something I've been meaning to fix. I'll take a stab at it this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants