How to get state values as type ‘Maybe Value’?

I want to judge if a state matches a certain value.

The getState "i" command will write Just 2.0f to the console. However, when (getState "i") == (Just $ VF 2) is executed, the output is as follows.

    • Couldn't match expected type ‘IO (Maybe Value)’
                  with actual type ‘Maybe Value’
    • In the second argument of ‘(==)’, namely ‘(Just $ VF 2)’
      In the expression: (getState "i") == (Just $ VF 2)
      In an equation for ‘it’: it = (getState "i") == (Just $ VF 2)

How to get state values as type ‘Maybe Value’ instead of ‘IO (Maybe Value)’?

Or, just as the ‘when function’ can apply an effect to the number of cycles for a given condition, it would be great if a similar function could be defined for state values.

You need something like

do r <- getState "i"
   return $ r == (Just $ VF 2)

or if you want to do something with the state value, something like

do r <- getState "i"
  if (r == (Just $ VF 2)) then (d1 $ s "foo") else (d1 $ s "bar")

But keep in mind that this just checks the state once, right when you execute the "do". I'm not sure if that's what you're trying to do.

1 Like

This is exactly what I want to do! Thank you so much!

I have found a way to dynamically compare state value and will share it.

capply func condpat effectpat = every
    (fmap func (segment 1 condpat)) (effectpat)
do
  d2 $ s "hc" # nCount "test"
  d1
    $ capply (\x -> if x >= 10 then 1 else 0) (cF 0 "test") (const $ s "808*4")
    $ s "bd*4"

If the state value "test" over 10, "808" is played instead of "bd".