oakes / play-clj

A Clojure game library
The Unlicense
939 stars 73 forks source link

(box-2d) taking too long ? #48

Closed DjebbZ closed 10 years ago

DjebbZ commented 10 years ago

We're initiliazing a game with a Box2d world, then creating a body. Problem : when creating the body with (add-body screen (body-def :dynamic)), this function needs a :world in the screen map, but (box-2d) did not return one yet and the following exception occurs java.lang.Exception: The keyword :world is not found.. Only when our screen-wrapper function launches, sleeps a bit then re-run our the game the :world key is not empty.

I tried playing with the third param of (box-2d) (sleep) with no success. Adding a dumb (Thread/sleep 5000) changes nothing.

Strange, changing an atom is synchronous, but it appears to be asynchronous and taking too long, so the code following executes with an empty :world key.

Code that produces the bug : https://github.com/LD30-Lions/con-world/blob/bf505ca0ffebeb4ea9618bbff9c6604c31350605/desktop/src-common/con_world/core.clj#L17 Bug happens in 2 machines, one OSX, on Linux Arch. Both on IntelliJ with Cursive.

oakes commented 10 years ago

The screen map is immutable, so when you call update! it returns a new map with the added values. Try catching the return value like this:

(let [screen (update! screen ...)]
  (cell/create-cell-entity! screen))
DjebbZ commented 10 years ago

It appears that we need to get back the return value of (update! screen ... :world (box-2d 0 0)) in a let binding then work with the returned screen. This is what you did in the breakout example.

Mutability is making us crazy...

DjebbZ commented 10 years ago

We wrote the message at the same time. Thanks for the answer !

The update! is confusing since it ends with a bang. But we got it now. It's also because we're not comfortable enough with Clojure. Learning the hard way :)

Closing.