Hard Light Productions Forums

Modding, Mission Design, and Coding => The FRED Workshop => Topic started by: AdmiralRalwood on April 17, 2015, 10:10:15 pm

Title: Example Event: Setting cargo at random
Post by: AdmiralRalwood on April 17, 2015, 10:10:15 pm
After helping somebody with this idea, I thought I'd paste this little snippet here in case anybody else could use it.

Code: [Select]
$Formula: ( when-argument
   ( any-of "TC 1" "TC 2" "TC 3" "TC 4" )
   ( true )
   ( when
      ( true )
      ( modify-variable
         @tempRandomVar[0]
         ( rand-multiple 0 5 )
      )
      ( when
         ( = @tempRandomVar[0] 0 )
         ( set-cargo "Nothing" "<argument>" )
      )
      ( when
         ( = @tempRandomVar[0] 1 )
         ( set-cargo "Calipers" "<argument>" )
      )
      ( when
         ( = @tempRandomVar[0] 2 )
         ( set-cargo "Monkeys" "<argument>" )
      )
      ( when
         ( = @tempRandomVar[0] 3 )
         ( set-cargo "Weapons" "<argument>" )
      )
      ( when
         ( = @tempRandomVar[0] 4 )
         ( set-cargo "Spice" "<argument>" )
      )
      ( when
         ( = @tempRandomVar[0] 5 )
         ( set-cargo
            "Medical Supplies"
            "<argument>"
         )
      )
   )
)
+Name: Set random cargo
+Repeat Count: 1
+Interval: 1

"Why are all the actions under a sub-when that just has a condition of ( true )?" you might ask. "Don't we have a '$Loop SEXPs Then Arguments:' mod table setting for that?" Well, funny story: if you try doing it without the sub-when, it doesn't work even with Loop SEXPs Then Arguments set. The reason is that the modify-variable will only get evaluated once, because it doesn't have "<argument>" in it; with the sub-when, the whole when (including modify-variable) gets evaluated once per argument.

Worth noting that the 0-"Nothing" branch is usually going to be redundant (default cargo is "Nothing"), but included for the sake of completeness.

For fun, you can give the random number a wider range of values and then change the conditionals to check to see if the variable is between two values, so that some cargoes are more likely than others.
Title: Re: Example Event: Setting cargo at random
Post by: niffiwan on April 18, 2015, 12:23:04 am
Hey cool, I think I have a future use for this :)

/me bookmarks post
Title: Re: Example Event: Setting cargo at random
Post by: karajorma on April 18, 2015, 01:57:13 am
This is the sort of thing the new container code excels at. But this is a nice solution until that code is in trunk.

Quote
"Why are all the actions under a sub-when that just has a condition of ( true )?" you might ask. "Don't we have a '$Loop SEXPs Then Arguments:' mod table setting for that?" Well, funny story: if you try doing it without the sub-when, it doesn't work even with Loop SEXPs Then Arguments set. The reason is that the modify-variable will only get evaluated once, because it doesn't have "<argument>" in it; with the sub-when, the whole when (including modify-variable) gets evaluated once per argument.

Actually that's what the do-for-valid-arguments SEXP is for. It basically makes any SEXP in a branch below it trigger once for every argument that is valid. Which means that as long as you have already used '$Loop SEXPs Then Arguments in mod.tbl you no longer need the inner when and can simplify the code to this



when-argument
- any-of
-- TC 1
-- etc
- Do-for-valid-arguments
-- modify-variable
etc.
Title: Re: Example Event: Setting cargo at random
Post by: AdmiralRalwood on April 18, 2015, 02:54:20 am
Actually that's what the do-for-valid-arguments SEXP is for. It basically makes any SEXP in a branch below it trigger once for every argument that is valid. Which means that as long as you have already used '$Loop SEXPs Then Arguments in mod.tbl you no longer need the inner when and can simplify the code to this
Yes, that's another option. However, doing it this way makes it easier to replace the when's "true" condition with something else (campaign-persistent variable from a previous mission, for example). A bit of future-proofing, if you will (side effect that it also works properly with Loop SEXPs Then Arguments not set/set to false is just a bonus).

Yes, you can just change the condition of the when-argument, or wrap the whole thing in another when. It's purely a personal preference thing.