Games Programmer, Anime fan, General nerd.

 

Super awkward but trying to improve~ n_n

 

(Have been advised to add: All views my own)


tef
@tef

instead of writing things like this

if event == E_LEFT {
    player.x += 10
} else if event == E_RIGHT {
    player.x -= 10
} else if .... {
  ...
}

split it up into "decide what to do" and "do the thing", and don't overwrite the current state until you're done, either:

if event == E_LEFT {
    action = ACTION_MOVE_LEFT
} else if ... {
    ....
}

var player_next = player

if action == ACTION_MOVE_LEFT {
    player_next.x += 10
} else if .... {
    ...
}

player = player_next

why?



You must log in to comment.

in reply to @tef's post:

as you can see, i'm not entirely calling for fully immutable state, just updating the state in one place, well after any code can read it, but yeah you will trip down similar patterns in a fp style

I love this subtle attempt to get programmers to internalize how DFAs work. Bravo. Next hot tip for game devs: make your next_action and next_state functions const (or whatever keyword your language uses to express 'pure', as in having no side-effects on global state).

it's not so much how dfas work, and yeah, it's more about expressing things in an immutable, pure style

but it is also about expressing changes as atomic like updates, in that the handlers do not see partial new state as the event loop runs