Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a sample of some code I've been working on that uses the approach of returning a copy rather than mutating state directly. The nice thing about this kind of approach, on the server side at least, is that it enables me to easily implement transaction-type semantics. If something goes wrong while doing an update, it's trivial for me to still have everything that was updated in a consistent state.</p> <p>The code below is from a game server I'm working on, which does something similar to what you're doing, it's for tracking objects that are moving around in time slices. This approach isn't as spectacular as what Dave Griffith suggests, but it may be of some use to you for contemplation.</p> <pre><code>case class PosController( pos: Vector3 = Vector3.zero, maxSpeed: Int = 90, velocity: Vector3 = Vector3.zero, target: Vector3 = Vector3.zero ) { def moving = !velocity.isZero def update(elapsed: Double) = { if (!moving) this else { val proposedMove = velocity * elapsed // If we're about to overshoot, then stop at the exact position. if (proposedMove.mag2 &gt; pos.dist2(target)) copy(velocity = Vector3.zero, pos = target) else copy(pos = pos + proposedMove) } } def setTarget(p: Vector3) = { if (p == pos) this else { // For now, go immediately to max velocity in the correct direction. val direction = (p - pos).norm val newVel = direction * maxSpeed copy(velocity = direction * maxSpeed, target = p) } } def setTargetRange(p: Vector3, range: Double) = { val delta = p - pos // Already in range? if (delta.mag2 &lt; range * range) this else { // We're not in range. Select a spot on a line between them and us, at max range. val d = delta.norm * range setTarget(p - d) } } def eta = if (!moving) 0.0 else pos.dist(target) / maxSpeed } </code></pre> <p>One nice thing about case classes in Scala is that they create the copy() method for you-- you just pass in which parameters have changed, and the others retain the same value. You can code this by hand if you're not using case classes, but you need to remember to update the copy method whenever you change what values are present in the class.</p> <p>Regarding resources, what really made a difference for me was spending some time doing things in Erlang, where there is basically no choice but to use immutable state. I have two Erlang books I worked through and studied every example carefully. That, plus forcing myself to get some things done in Erlang made me a lot more comfortable with working with immutable data.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. COWeirdly enough, Erlang is the next language I was going to attempt to pick up (or maybe Haskell), although my feeble brain has still got a lot of work to do with Scala first. I like your examples! I still think there should be a more general way to represent how variables change rather than just making copies of the objects every iteration. Have you ever experimented with representing position as a function of position over time rather than as a var?
      singulars
    2. COI haven't. I started by keeping all state for an actor within a single var, composed of multiple val's. For example, the position controller is one val, and the brain state is another val. All of those are rolled into a single state variable. If I have a method that changes one, changes another, and possibly throws an exception in the middle, I'm guaranteed that either all 3 ops work or all 3 fail, and the state is preserved. The reason I haven't expressed it as a function of time is that the target positions are controlled sometimes by a brain class, and sometimes by a player.
      singulars
    3. COYou mentioned that there's a lot to wrap your brain around in Scala first. For me, I found Scala far more difficult than Erlang-- at first. Other people feel differently, but I wrote up thoughts about Erlang vs. Scala during the learning process here http://tango11.com/news/scala-complexity-vs-erlang-complexity/ which may very well interest you.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload