Update state question

24 views
Skip to first unread message

Jason Fabris

unread,
Jul 15, 2019, 2:04:27 AM7/15/19
to clj-processing
Hi,

I am trying to learn clojure and quil to replace my current Processing workflow. So, apologies if I am asking obvious questions.

I am not getting why this sketch isn't working. When I make a fake "state" map in the REPL and test the functions they seem to produce what I want...

I define :pts in setup (get-pts returns a vector of tuples like [[100 100] [200 200] [123 456]])

(defn setup []
  (q/frame-rate 30)
  (q/color-mode :hsb)
  {:color 0
   :angle 0
   :pts get-pts})

Then in update-state, I try to update :pts by mapping an update function to all the pts. (update-pt adds to a passed-in tuple)

(defn update-state [state]
   {:color (mod (+ (:color state) 0.7) 255)
    :angle (+ (:angle state) 0.1)
    :pts (update-in state [:pts] #(mapv update-pt %1))})

Then, down in the draw-state function, I have this:
(let [pts (:pts state)]
   (doseq [pt pts]
    (apply #(q/rect %1 %2 50 50) pt)))

I don't get any rectangles. If I replace the (:pts state) with a manual vector of pts, or even the (get-pts) function, I get the rectangles. But then they obviously don't get updated as the state changes...

Also, if I comment out the :pts part in the update-state function, I still don't get rectangles. It's like the draw function doesn't even see the (:pts state) result.

So, what am I doing wrong? Any help would be appreciated!

Jason

Mikita Belahlazau

unread,
Jul 15, 2019, 8:45:15 PM7/15/19
to clj-processing
Hi Jason

Have you tried printlng state inside draw function to see if it looks correct? You can add (q/no-loop) in the draw so it runs once and doesn't print hundres of states in console every second. 

Mikita

--
You received this message because you are subscribed to the Google Groups "clj-processing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clj-processin...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clj-processing/8f423159-cdbd-4f94-b2b3-e83088d0d4d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Fabris

unread,
Jul 20, 2019, 5:07:48 PM7/20/19
to clj-processing

Thanks!
One more question -- I can print state like this...
(q/text (str state) 100 100)
But when I use no-loop, nothing prints. (And otherwise it obviously becomes a mess pretty quickly.)

I tried printing using str and prn, but nothing showed up in the output. Is that normal? Is there a "best" way you would recommend printing state?

Mikita Belahlazau

unread,
Jul 21, 2019, 11:10:22 PM7/21/19
to clj-processing
I'd just use println and look in console (on cljs in devtools console). To avoid spamming console with hundreds of logs every second you can add logic to show the first 10 for example:

(when (< (q/frame-count) 10)
  (println state))

Example: http://quil.info/sketches/show/3e659f13265f4ce466f44edccd091362ac360bca2d3e96abf580c11a994bf7e2 (if you check devtools console you'll see 10 states printed).

Unfortunately Quil is lacking functionality like this even though it's pretty common thing to do and would be useful. Filed a feature request: https://github.com/quil/quil/issues/315

--
You received this message because you are subscribed to the Google Groups "clj-processing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clj-processin...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages