Custom code

; This story demonstrates session storage a bit more ; ; (save KEY VALUE) ; replace KEY with whatever you plan to use as a common lookup ; you can think of a key like a locker number, it is also known as an identifier ; replace VALUE with whatever you plan to remember or store under that key ; ; For example if in a scene I do (save money 1000) ; and later I say (load money), the result will be 1000. ; However, (load "money") in this case will return nil, ; as money and "money" are distinct values. ; ; Helper function can also be defined for use in stories (def purchase ; purchase will be a function, which returns true if the account could be charged ; by a certain amount (fn (amount) (let ; load the money once under the local name: current (current (load money)) ; Check if the (current - amount) > 0 ; That is, if we charge them, will we still have money left? (if (>= (- current amount) 0) ; When we need to do multiple actions, use do (do ; First, save the new account amount (save money (- current amount)) ; And return that we successfully charged the account true) ; They did not have enough moneys to purchase the item. false) ; Remember to close the parenthesis ))) ; You can also use your own functions inside of other ones you define. (def attempt-purchase (fn (thing amount inventory) (if (purchase amount) (do (save-deref inventory (+ 1 (load-deref inventory))) (text '("You purchase the " thing " for $" amount " with $" (load money) " leftover.")) ) (text '("Oops, you don't have enough left to buy the " thing " for $" amount "! You only have $" (load money) " leftover.")) ))) (def inventory-muffins 'inventory-muffins) (def inventory-giftcard 'inventory-giftcard) (defscene start ; Since we are starting, it is best to save safe default values for ; what the player starts off with. For example, health as 100. ; Save 100 to the session value identified by: money (save inventory-muffins 0) (save inventory-giftcard 0) (save money 100) ; Jump to the main interaction (include store)) ; We will return to the store often after each purchase (since we never left) (defscene store (text "You stand at the store kiosk, thinking about what to buy") (text "You have " (load inventory-muffins) " muffins") (text "You have " (load inventory-giftcard) " gift cards") (prompt leave "Leave") (prompt buy-muffin "Muffin") (prompt buy-giftcard "Giftcard") ; You can choose to have the parenthesis at the end on its own line ; so you don't have to edit the last line every time. ) (defscene leave (text '("You leave the store kiosk with only $" (load money) " left.")) (the-end)) (defscene buy-muffin (attempt-purchase "Muffin" 10 inventory-muffins) (include store)) (defscene buy-giftcard (attempt-purchase "Gift card" 90 inventory-giftcard) (include store))