oakes / play-clj

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

:on-timer only passes initial entities? #32

Closed Misophistful closed 10 years ago

Misophistful commented 10 years ago

I'm using 0.3.5-SNAPSHOT and have a problem where the :on-timer function appears to be passing the initial entities vector, rather than the current state (like I was expecting). Is this a bug with :on-timer, or a misunderstanding on my part?

oakes commented 10 years ago

Can you explain this further? I did a quick test and my :on-timer function gives me the latest copy of my entity.

Misophistful commented 10 years ago

The only thing I can think that I'm doing differently is adding the timer when a button is pushed, as opposed to :on-show.

I've pushed the code to my BitBucket project, if you want to take a look. The timer is added in turn-button-pushed in the turn-button namespace, and you can see the initial entities being passed into the next-battle-step function in the battles namespace.

I think it's probably something that I'm doing wrong, but I just can't see what it is.

oakes commented 10 years ago

I've tried setting a timer from a button click and it also worked for me. I cloned the repo and tried it out, but I'm not sure how to determine whether or not it is the initial entities vector. How are you determining that?

oakes commented 10 years ago

Also, does the behavior change if you revert to 0.3.4?

Misophistful commented 10 years ago

Light Table has live watches, so I'm using one to see the value of entities in the next-battle-step function.

However, now I think the problem might be that I'm not clearing the timer properly when reseting the screen, which might be causing the :on-timer to trigger based on the initial game state.

I'm going to look into it further. Sorry if this turns out to be a wild goose chase.

Misophistful commented 10 years ago

:on-timer is receiving an up-to-date entities vector, it was me who was misreading the watch output, due to the timer not being cleared when I restarted the game.

Which leads me to a follow-up question... here's my :on-key-down function in my an-exception-has-happened blank-screen:

:on-key-down
  (fn [screen entities]
    (cond
     (key-pressed? :r) (do
                         (remove-timer! screen :battle-step)
                         (on-gl (set-screen! elemental main-screen)))))

When I press R the game restarts, but triggers an :on-timer event immediately. I'm pretty sure the timer id is correct, so what else might I be doing wrong that the timer isn't getting removed?

oakes commented 10 years ago

One thing you can do is check the return value of the remove-timer! function. It should return the timer object when it succeeds, and nil if it fails to find a timer with that id.

Misophistful commented 10 years ago

It's nil, but I see why now, I'm trying to remove it from the wrong screen, i.e. I add the timer to the main-screen, but I'm trying to remove it from blank-screen. D'oh!

I've taken up too much of your time with this non-issue, so I'm closing it now. Thanks for your help.