Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

bitsy

a little engine for little games, worlds, and stories · By adam le doux

Tutorial: Nests in Nests - Subtracting items within IF/WHEN THEN dialog branches & printing how many items you have

A topic by Akaki created Jan 23, 2024 Views: 72
Viewing posts 1 to 1
(13 edits)

This tutorial is useful for people wanting to make a basic RPG inventory system, a buy/sell system, etc.

Example scenario: You talk to the cat. If you have at least 1 fish it says "Thanks for the fish!" then it subtracts one fish from your inventory, and then it shows you how many fish you have left before moving on to the end of the IF/THEN statement. If you don't have any fish it just says "The cat looks hungry". 

This is called a "nested" statement, and Bitsy can't do it by using its default "branching / conditional" buttons. You can do it easily, but not by using the existing buttons within Bitsy, and not by typing the code directly into the normal dialog box!

Click "edit code" and type it in before the "- else ?" that starts the next option. In my example "berry" is {item "0"} in the code, and subtracting one berry is just the normal code like this: 

{item "0" {{item "0"} -1}}

Again, doing this same exact thing in the normal dialog box menu will make it not work!

 
When you click "hide code" to move back to the normal dialog screen, suddenly it will correctly recognize the subtraction and have it pop up in a special section like this:

If yours looks like anything else, such as has the code as normal text within the dialog box... it will probably not work. If it SHOULD all be working and it isn't, try refreshing the page (copy your game code first).

Now say you want to tell the player how many items they have left.

1) If you want to insert text between the subtraction and the print, for example: "Fish left: 0".

    "Thanks for the fish!"{item "0" {{item "0"} -1}}
    Fish left
: {print {item "0"}}

Will accurately show that you have 0 fish left, only because you inserted text before the PRINT.

If you print the number of fish directly after the subtraction with no text in between, sometimes Bitsy messes up and tells the player they still have 1 fish left. In that case use the normal PRINT command but use a nest to subtract 1 during the PRINT to show an accurate count. Here is the code: 

{print {{item "0"} - 1}}

Remember that in CSS and JavaScript, {} creates a "nest" just like () does in math. So the above code says: 

(((get item 0) subtract 1) print the result}. 

Warning!

Sometimes Bitsy won't visually recognize this 2nd or 3rd nest and it will be completely invisible (not in the dialog box, nor popping up like the subtraction command). It may or may not work in-game. If at any time it doesn't work, in the most unfortunate scenario Bitsy tried to run it and then ate all of your ELSE statements that came after the IF/THEN. So you shouldn't use this technique to create tons of nests inside one dialog box that Bitsy can't visually recognize, it's too risky.

Troubleshooting:

1) If Bitsy merged your two seperate pieces of code into one and now it doesn't work, it means you forgot 1 closing } mark at the end of the first piece of code. Example:

{item "0" {{item "0"} -1} {print {{item "0"} - 1}}

Should be:

{item "0" {{item "0"} -1}} {print {{item "0"} - 1}}

2) Bitsy can't recognize nests that don't have a command right after the opening { mark. So while this is the same code as in 1) just grouped together, Bitsy won't recognize it:

{{item "0" {{item "0"} -1}} {print {{item "0"} - 1}}}

You need to add a command after the first { (this is a nonsense command but it illustrates my point):

{print {item "0" {{item "0"} -1}} {print {{item "0"} - 1}}}

The exception is when are using {} in place of a variable, item, etc. name as in {item "0"}. Then Bitsy can read the {{ .