Basic Variable/Attribute code for gamebooks where page options are limited by an attribute/variable number.

I'm wondering if there is some way to work in code with gamebooks a variable that, depending on its value, will change what page a link sends you to.
Say there was a variable for ammunition, and you have 28. If there is a page option for, "try to blast down the baddies", what I would want to happen is on clicking, the variable is compared to a required ammunition count for the page. If the ammo count is less than 30, it takes you to a page declaring you have failed to defeat the baddies, and your ammo is dropped to zero.
But, if your ammo was more than 30, it would take you to a page where you did defeat the baddies but you have 30 less ammo.

How would one achieve this? I am somewhat unfamiliar with coding, and it seems a lot of the codes people are working with are for text-adventures, not gamebooks.

thank you!


If that doesn't help, just post again and let us know!

Create a variable by attaching an attribute to the 'player' object, like so:


This can even be declared on the page where the player first gets some ammo:

player.ammo = player.ammo + 48

Now that your player has ammo, they can use it on the bad guys:

player.ammo = player.ammo - 12

In the text area of the page where you want to test the amount of ammo, use curly brace notation:

{if player.ammo<30:page:Failed:Try to blast the baddies!}}{if player.ammo>29:page:Success:Try to blast the baddies!}}

On the page called "Failed", add a script:

player.ammo = 0

On the page called "Success", add a script:

player.ammo = player.ammo - 30

On the failed page, the player looses all the ammo, regardless of the value, but they only get directed there if they have 28 or less ammo. On the Success page, they only lose 30 ammo, regardless of the value. They only get directed to the success page if they have 30 or more (ammo>29).

Good luck!

Oh thank you ! I will try out.
Coding is definitely not my forte.

This topic is now closed. Topics are closed after 60 days of inactivity.