Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Fluue
Jan 2, 2008
I've been toying around with Elm lately and it's my first introduction to FP-style programming. I'm working on making a very basic Cookie Clicker clone to practice with Signals, but I'm hung up on how I should merge my model update (i.e. increment by 1) on both: a) every click (already have this handled as an onClick in a View element) and b) every 1 second that passes.

These actions are doing the same thing, so where would I add in a Signal handler to perform the "Increment" action every 1 second? Would it be a foldp or Signal.map that I'm thinking too hard about?

Adbot
ADBOT LOVES YOU

Fluue
Jan 2, 2008

fart simpson posted:

Post some code? The answer is probably to Signal.map your input signals into an Increment action and merge them, then foldp the resulting action signal

Game model just defines clicks, level, and cps_multiplier.

code:
-- Update

type Action = Increment

doClick action game =
    case action of
        Increment -> { game | clicks = game.clicks + (1 * game.cps_multiplier) }

-- Where things get hazy for me: determining how to handle an every second Time update
tickerClick : Signal Action
tickerClick = Signal.map Increment (Time.every Time.second)

-- View

view : Signal.Address Action -> Game -> Html
view address game = 

    div []
        [ img [ src "assets/cookie.jpeg", onClick address Increment ] []
        , div [] [text (toString game.clicks)]
        ]

I am using StartApp but until now I've been using StartApp.simple which abstracts away some of the signal processing.

Fluue
Jan 2, 2008

fart simpson posted:

Try this, is it what you want? You basically just pass the mapped Increment action from the every second signal into the inputs field of the StartApp config

Perfect! I was about to post back with something similar (using input=[]). Is there a resource you have that explains why Effects.Effects needs to be used when dealing with this kind of signal? Is it because the ticker is constantly running?

  • Locked thread