## HP and Game over (Gamebook)

So I am finally progressing in this game that I am working on. Half way done with the tutorial mode and the second half I will be introducing the fighting scenario. The HP part I am going to need help with.

*I am using GAMEBOOK

1. How do I set that the maximum HP is 100? That even if the player takes pills or uses first aid kit, the player's life will not pass 100.

Ex. player has `HP: 95` and player uses first aid kit that adds 10 to player life. My current code `set (player, "HP", player.HP +10)` will automatically add 10 and the HP will be 105. How do I fix this problem? Set the maximum HP to 100 so items that add HP do not exceed the max HP?

2. How do I code it that the player will be Game Over when the player is killed or if the survivors that the player is supposed to protect dies?*

Ex. If the player's HP reaches 0 or if the player's team mates reaches 0, the game is over and they should start all over again. How do I do that?

3. Is it possible to remove all existence of a teammate if he/she is dead?

In the game that I am working on, the player has a team. They also have a HP and they can die within the game if the player has bad choices. Is it possible that if they die, there is a trigger that will erase all their dialogue and probably they're entire existence in the future scenarios of the game? If that is so, how do I do that? If not, I will stick with the player having to play it over again and end with a Game Over.

Thank you very much in advance.

1. How do I set that the maximum HP is 100? That even if the player takes pills or uses first aid kit, the player's life will not pass 100.

One common way is to test if the HP is over 100, and if it is, reduce it. For example:

``````set (player, "HP", player.HP + 10)
if (player.HP > 100) {
set (player, "HP", 100)
}
``````

Or, you could do `set (player, "HP", Max (100, player.HP + 10))` (I assume this function is available in gamebook mode, but haven't tested it)

2. How do I code it that the player will be Game Over when the player is killed or if the survivors that the player is supposed to protect dies?

Test if the HP is less than zero, and if it is, send them to the game over.
In a text adventure, it may be beneficial to have this test every time they take damage.

Alternatively you can use a changescript. This will be run automatically every time the HP changes.
If you give the player a script attribute named `changedHP`, it will run every time their HP changes.

So you could make the changescript something like:

``````if (this.HP > 100) {
this.HP = 100
}
if (this.HP <= 0) {
// or whatever code you use to display a game over
msg ("You died!")
finish()
}
``````

This changescript makes sure the HP can't go over 100, and also triggers a game over if it goes to 0 or less. It means you don't need to put the checks in every time the HP changes.

BUT in this case, if the player takes damage and there is more text on the page, it will display after the game over message.
So if a page has the player taking damage in the middle, you should use an `if` condition to ensure that the remainder of the page is only displayed if they're still alive.

3. Is it possible to remove all existence of a teammate if he/she is dead?

Quest has no concept of "characters" or "dead". Those are in-game concepts.
I assume that you have a counter for the NPC's HP, or a flag to indicate that they've died, or some other kind of attribute to keep track of their life/death status?
If so, you can use an `if` around their dialogue to check if they're still alive, and display something different if they're dead.

``````set (player, "HP", player.HP + 10)
if (player.HP > 100) {
set (player, "HP", 100)
}
``````
``````Or, you could do set (player, "HP", Max (100, player.HP + 10)) (I assume this function is available in gamebook mode, but haven't tested it)
``````

I will test the solution you provided. Thank you very much!

`````` if (this.HP > 100) {
this.HP = 100
}
if (this.HP <= 0) {
// or whatever code you use to display a game over
msg ("You died!")
finish()
}
``````

I haven't used a `changescript` yet, this will be my first. I will use your example as my guideline to test it out.

And okay noted. I will use the IF in that case.

``````I assume that you have a counter for the NPC's HP, or a flag to indicate that they've died, or some other kind of attribute to keep track of their life/death status?
``````

I will apply the same HP code of the player to the teammates. And I guess I will also use the IF code on them just in case.

Thank you again!

This appears when I try to play the game.
`Error running script: Error compiling expression 'player.HP +10': ArithmeticElement: Operation 'Add' is not defined for types 'Object' and 'Int32'`

``````set (player, "HP", player.HP +10)
if (player.HP > 100) {
set (player, "HP", 100)
}
if (player.HP > 100) {
player.HP = 100
}
if (player.HP <= 0) {
msg ("You died!")
finish()
}
``````

This is the code I put in one page.

How do I fix it?

That message roughly translates to English as "You're trying to add together 'unknown' and '10', and I only know how to add up numbers."

You can only add 10 to the player's HP if you have already set it to be a number.

I'd suggest something like:

``````set (player, "HP", 100)
``````

or

``````player.HP = 100
``````

on the first page, so that you know the HP will have a value when you need it.

(Those two lines are exactly the same thing. The first one is how the scripting GUI would create it, the second is how a programmer would normally write it)

Speaking of which, you have these two blocks:

`````` if (player.HP > 100) {
set (player, "HP", 100)
}
``````

and

`````` if (player.HP > 100) {
player.HP = 100
}
``````

These are exactly the same thing; you don't need to do it twice

Oh I see okay.
This is what I did so far. The "greater than" "less than" worked. But the print message to redirect player to the nearest checkpoint does not appear or the print message that tells player they are injured.

I haven't tried the maximum HP yet, I want to fix this first. Thank you.

``````<!--Saved by Quest 5.7.6606.27193-->
<asl version="550">
<include ref="GamebookCore.aslx" />
<game name="Sample Training">
<gameid>9873d63f-0d25-4b0b-8c0c-7d3b089a15f4</gameid>
<version>1.0</version>
<firstpublished>2018</firstpublished>
<category>Simulation</category>
<description type="string"></description>
<defaultbackground>Black</defaultbackground>
<defaultforeground>Cornsilk</defaultforeground>
<clearlastpage />
<roomenter type="script">
HP = 100
</roomenter>
</game>
<object name="Page1">
<inherit name="scripttext" />
<options type="stringdictionary">
<item>
<key>Chapter 1</key>
<value>Go here lose</value>
</item>
</options>
<script type="script">
set (player, "HP", 100)
</script>
<object name="player">
<inherit name="defaultplayer" />
</object>
</object>
<object name="Chapter 1">
<inherit name="scripttext" />
<script type="script"><![CDATA[
set (player, "HP", player.HP -100)
if (player.HP > 100) {
msg ("You Lose. Go to Last checkpoint. {Page:Checkpoint:Checkpoint}")
set (player, "HP", 100)
}
else if (player.HP <0) {
msg ("You are injured [-50]")
}
]]></script>
</object>
<object name="Checkpoint">
<inherit name="scripttext" />
<description>Chapter 2.Checkpoint</description>
<script type="script">
</script>
</object>
</asl>
``````

You've got:

``````       if (player.HP > 100) {
msg ("You Lose. Go to Last checkpoint. {Page:Checkpoint:Checkpoint}")
set (player, "HP", 100)
}
else if (player.HP <0) {
msg ("You are injured [-50]")
}
``````

So if the HP is more than 100, it displays the message "You Lose. Go to Last checkpoint. Checkpoint"

And if the HP is less than zero, it displays the message "You are injured [-50]".

When you get to that page, the HP will be 0, because you set it to 100 and then subtracted 100 from it. The "You lose" message isn't printed because 0 isn't greater that 100; and the "You are injured" message isn't displayed because 0 isn't less than 0.

Try this.

``````if (player.HP < 100 and player.HP >= 0) {
msg ("You Lose. Go to Last checkpoint. {Page:Checkpoint:Checkpoint}")
player.HP = 100
}
else if (player.HP < 0) {
msg ("You are injured [-50]")
}
``````

Edit: Had to edit the code a little.

Hi this is what I did.

``````if (player.HP < 100 and player.HP >= 0) {
msg ("You Lose. Go to Last checkpoint. {Page:Checkpoint:Checkpoint}")
}
else if (player.HP < 0) {
msg ("You are injured [-50]")
}
``````

But the `{Page:Checkpoint:Checkpoint}` does not work. It does not appear as a link if I write it in the Script part. It only works if I write it in the Description. If I use the Move Player Script, it automatically moves the player to the Checkpoint without giving the player the chance to process the "loss" (when the player died) and click Go to Checkpoint. How do I fix this?

I tried to play around with it and this is what I did.

``````if (player.HP = 0 and player.HP < 0) {
msg ("You Lose. Go to Last checkpoint.")
}
else if (player.HP > 0) {
msg ("You are injured.")
}
``````

So it means that all I have to do is copy this in all fight scenes so in case the player dies, they will be redirected to a checkpoint or be notified if they are injured, right?

Thank you for helping me solve this problem. I have one last problem though.

How do I set a maximum for health?
So if the maximum health is `player.HP = 100` then how do I make it that whenever they use a med kit their life does not go over 100?

(filler for getting my edited post, updated/posted)
(again, filler for getting my edited post, updated/posted)

this is for the Text Adventure, but it should be the same (I think*) for the Game Book (just have to access the scripting differently and have limited types of Objects to work with):

hopefully, you can at least use this to understand the concepts of: how 'current/maximum' life (and its displayment) works or how to do the 'current/maximum' life (and its displayment)

*I think the Game Book has the 'status attributes' and the 'change' Scripts, Features/Functionalities, otherwise this will not work for Game Book, as my code uses these things

or, just using basic scripting, here's a quick example:

``````// creating the 'current_life' and 'maximum_life' Integer (int) Attributes, and setting their initial Values:

set a variable player.maximum_life = [EXPRESSION] 999
set a variable player.current_life = [EXPRESSION] 999

// creating the 'life' String Attribute, and setting its initial Value:
// (this 'life' String Attribute, is for the displayment of the: current/maximum life)

set a variable player.life = [EXPRESSION] "Life: " + player.current_life + "/" + player.maximum_life
// example of it being displayed:
// Life: 999/999

// ----------
// or if you prefer this design of formatting/displayment:
// -------------

set a variable player.life = [EXPRESSION] player.current_life + "/" + player.maximum_life + " life"
// example of it being displayed:
// 999/999 life

------------

// changing the 'current_life' and/or 'maximum_life' Integer (int) Attribute's Values:

examples (using subtraction):

set a variable player.maximum_life = [EXPRESSION] player.maximum_life - 900
// player.maximum_life = 99 // (999-900 = 99)

set a variable player.current_life = [EXPRESSION] player.current_life - 949
// player.current_life = 50 // (999-949 = 50)

-----------

// Setting/Changing (and thus what will be the displayment of) the 'life' String Attribute's Values:

set a variable player.life = [EXPRESSION] "Life: " + player.current_life + "/" + player.maximum_life

// displaying the 'life' String Attribute:

print [EXPRESSION] player.life
// example displayments:
// Life: 999/999
// Life: 50/99

// -----------------------------
// or if you prefer this design of formatting/displayment:
// ----------------------------

set a variable player.life = [EXPRESSION] player.current_life + "/" + player.maximum_life + " life"

// displaying the 'life' String Attribute:

print [EXPRESSION] player.life
// example displayments:
// 999/999 life
// 50/99 life
``````

ask if you need help with anything (and/or got questions about anything or need anything explained)

(filler for getting my edited post, updated/posted)

then how do I make it that whenever they use a med kit their life does not go over 100? (karinchan999)

whenever you change their 'current_life' Integer Attribute, an example:

``````// creating/setting this Integer Attribute and its Value (such as at game start: 'game' -> 'start' Script)
// (Do NOT have this as a script when you change your 'maximum_life' Integer Attribute)

game.minimum_life = 0

// -----------------

// this IS the scripting (scripts) that you do when you change your 'current_life' Integer Attribute:

if (player.current_life > player.maximum_life) {
player.current_life = player.maximum_life
} else if (player.current_life < game.minimum_life) {
player.current_life = game.minimum_life
}

player.life = "Life: " + player.current_life + "/" + player.maximum_life
msg (player.life)

// optional:

if (player.current_life = game.minimum_life) {
msg ("You were killed or you've died")
msg ("GAME OVER")
finish
}
``````

whenever you change their 'maximum_life' Integer Attribute, an example:

``````// creating/setting this Integer Attribute and its Value (such as at game start: 'game' -> 'start' Script)
// (Do NOT have this as a script when you change your 'maximum_life' Integer Attribute)

game.maximum_life = 999

// -----------------

// this IS the scripting (scripts) that you do when you change your 'maximum_life' Integer Attribute:

if (player.maximum_life > game.maximum_life) {
player.maximum_life = game.maximum_life
}

if (player.current_life > player.maximum_life) {
player.current_life = player.maximum_life
}

player.life = "Life: " + player.current_life + "/" + player.maximum_life
msg (player.life)
``````

if (player.HP = 0 and player.HP < 0) { --- (karinchan999)

this is logically impossible:

(life < 0) ---> (life is not 0)

if (life is 0 and life is not 0)

your life can't be both, '0' and 'not 0', at the same time: logically impossible (you can't be and not be something)

this bad code line of yours "will" (should hopefully, lol) cause an ERROR in quest

(see my post above, for an example of the syntax and logic that you want)

How do I set a maximum for health?
So if the maximum health is player.HP = 100 then how do I make it that whenever they use a med kit their life does not go over 100?

This is how I do it.

Player hitpoints.

``````player.hitpoints = 100
player.maxhitpoints = 100
``````

Rest or heal function or command

``````if (player.hitpoints < player.maxhitpoints) {
player.hitpoints = player.maxhitpoints
msg ("Your Pokemon are at full health!")
}
else {
msg ("Your Pokemon are at full health!")
}
if (player.ammo < player.ammomax) {
player.ammo = player.ammo = player.ammomax
msg ("Your Pokemon are well rested!")
}
else {
msg ("Your Pokemon are well rested!")
}
``````

I tried setting the current and maximum HP but I can't seem to get it right.

``````<!--Saved by Quest 5.7.6606.27193-->
<asl version="550">
<include ref="GamebookCore.aslx" />
<game name="Sample Training">
<gameid>9873d63f-0d25-4b0b-8c0c-7d3b089a15f4</gameid>
<version>1.0</version>
<firstpublished>2018</firstpublished>
<category>Simulation</category>
<description type="string"></description>
<defaultbackground>Black</defaultbackground>
<defaultforeground>Cornsilk</defaultforeground>
<clearlastpage />
<roomenter type="script">
maximum_life = 100
current_life = 100
</roomenter>
</game>
<object name="Page1">
<inherit name="scripttext" />
<description type="string"></description>
<options type="stringdictionary">
<item>
<key>HIDE</key>
<value>Go here lose</value>
</item>
</options>
<script type="script">
player.life = "Life" + player.current_life + "/" + player.maximum_life
set (player, "current_life", player.current_life 100)
msg (player.life)
</script>
<object name="player">
<inherit name="defaultplayer" />
</object>
</object>
<object name="HIDE">
<inherit name="scripttext" />
<script type="script"><![CDATA[
set (player, "HP", player.HP -50)
if (player.HP = 0 and player.HP < 0) {
msg ("You Lose. Go to Last checkpoint.")
}
else if (player.HP > 0) {
msg ("You are injured.")
}
]]></script>
</object>
<object name="Checkpoint">
<inherit name="scripttext" />
<description>Chapter 2.Checkpoint</description>
<script type="script">
</script>
</object>
</asl>
``````

(taking a little break, as I responded to your posts in the other threads, a bit tired from writing my responses, but soon as I get a little bit of a break, I'll open up a game book, and get working code for you, to see an example of it, for you to study, I'll have a post here helping you within this very day, just need a little break at the moment and then some time to work with game book figuring out how to do this stuff for you, so I can have actual code help that fully works for you, lol)

OK, going through possible issues I can see in this game.

In your `roomenter` script, you have this:

``````  maximum_life = 100
current_life = 100
``````

That will set the player's life to 100 every time they view a page. I think you probably want this to be in the game's "start" script.

In this case, however, it won't do anything. Variable names without a dot in will disappear as soon as that script ends.

You probably wanted something like:

``````player.HP = 100
player.maxHP = 100
``````

(Attaching the attributes to `player` or to `game` allows Quest to find them again later on)

On `Page1` you have:

`set (player, "current_life", player.current_life 100)`

I'd expect that to just give you an error message.
I can't see what you're trying to do there. Set the player's current_life to be equal to their current_life?

In the page `HIDE` you have:

`if (player.HP = 0 and player.HP < 0) {`

This will never be true. You're asking if the player's HP is exactly zero, and less than zero at the same time.

If you want the player to die when their HP hits zero, this should be:

`if (player.HP <= 0) {`

(that's the `<=` operator, "less than or equal to")

If you want them to be alive at 0 but die when it drops below that.

I also notice that on Page1 you are using an attribute called `player.current_life` and on HIDE you are using one called `player.HP`.
Are these supposed to be the same attribute?

`set (player, "current_life", player.current_life 100)`

I have no idea what you are trying to do here.

It should be this:
`player.current_life = 100`

just quickly about doing it via using the 'set' helper Function/Script or doing it directly in-code scripting or doing it via using the GUI/Editor's scripting:

// directly in-code scripting:

NAME_OF_OBJECT.NAME_OF_ATTRIBUTE = VALUE_OR_EXPRESSION

// example:
player.strength = 100

// using the GUI/Editor:

add new script -> 'variables' section/category -> 'set a variable or attribute' Script -> (see below)

set variable NAME_OF_OBJECT.NAME_OF_ATTRIBUTE = [EXPRESSION] VALUE_OR_EXPRESSION

// example:
set variable player.strength = [EXPRESSION] 100

// using the 'set' helper Function/Script (as a 'helper' Function/Script, it's just doing the above stuff for you):

set (NAME_OF_OBJECT, "NAME_OF_ATTRIBUTE", VALUE_OR_EXPRESSION)

// example:
set (player, "strength", 100)

quick example of doing simple addition:

// directly in-code scripting:

NAME_OF_OBJECT.NAME_OF_ATTRIBUTE = VALUE_OR_EXPRESSION

// example:
player.strength = player.strength + 5

// using the GUI/Editor:

add new script -> 'variables' section/category -> 'set a variable or attribute' Script -> (see below)

set variable NAME_OF_OBJECT.NAME_OF_ATTRIBUTE = [EXPRESSION] VALUE_OR_EXPRESSION

// example:
set variable player.strength = [EXPRESSION] player.strength + 5

// using the 'set' helper Function/Script (as a 'helper' Function/Script, it's just doing the above stuff for you):

set (NAME_OF_OBJECT, "NAME_OF_ATTRIBUTE", VALUE_OR_EXPRESSION)

// example:
set (player, "strength", player.strength + 5)

(filler for getting my edited post, updated/posted)

@ karinchan999:

here's a working example for you:

1. create a new Game Book
2. save it somewhere you can easily find it (like the desktop)
3. right click on your 'WHATEVER.aslx' Game Book game file
5. what you see is your entire game code: highlight all of it, and delete all of it
6. highlight, copy, and paste my code below into your (blank/deleted) game file, and save it, close/exit out of the game file
7. click (or double click, whatever) on your game file (open it up in the GUI/Editor, and study it, and also you can play it, to see it in action -- not much to see, it just displays your life, wasn't trying to do anything more advanced, but it shows how stuff can be done)

(I'm using an older version of Quest, so you may need to change the 'asl version number' below, to match up with yours, and also hopefully all my code below otherwise works in the current version of quest, but it might not, let me know if it doesn't work or if you get any errors)

``````<asl version="550">

<include ref="GamebookCore.aslx" />

<game name="karinchan999">

<attr name="gameid" type="string">2e475718-3650-42b4-8c7e-9f847834149d</attr>
<attr name="version" type="string">1.0</attr>
<attr name="firstpublished" type="string">2019</attr>

</game>

<object name="player">

<inherit name="defaultplayer" />

<attr name="parent" type="object">Page1</attr>

</object>

<object name="Page1">

<inherit name="scripttext" />

<attr name="description" type="string">This is page 1. Type a description here, and then create links to other pages below.</attr>

<options type="stringdictionary">

<item>

<key>Page2</key>
<value>This link goes to page 2</value>

</item>

<item>

<key>Page3</key>
<value>This link goes to page 3</value>

</item>

</options>

<attr name="script" type="script">

<![CDATA[

game.minimum_life_integer_attribute = 0
game.player_maximum_life_integer_attribute = 999

player.current_life_integer_attribute = 500
player.maximum_life_integer_attribute = 500

player.life_string_attribute = "Life: " + player.current_life_integer_attribute + "/" + player.maximum_life_integer_attribute

msg (player.life_string_attribute)

player.changedcurrent_life_integer_attribute => {

if (player.current_life_integer_attribute > player.maximum_life_integer_attribute) {

player.current_life_integer_attribute = player.maximum_life_integer_attribute

} else if (player.current_life_integer_attribute <= game.minimum_life_integer_attribute) {

msg ("You died or were killed")
msg ("GAME OVER")

finish

}

player.life_string_attribute = "Life: " + player.current_life_integer_attribute + "/" + player.maximum_life_integer_attribute

msg (player.life_string_attribute)

}

player.changedmaximum_life_integer_attribute => {

if (player.maximum_life_integer_attribute > game.player_maximum_life_integer_attribute) {

player.maximum_life_integer_attribute = game.player_maximum_life_integer_attribute

}

if (player.current_life_integer_attribute > player.maximum_life_integer_attribute) {

player.current_life_integer_attribute = player.maximum_life_integer_attribute

}

player.life_string_attribute = "Life: " + player.current_life_integer_attribute + "/" + player.maximum_life_integer_attribute

msg (player.life_string_attribute)

}

// testing:

player.current_life_integer_attribute = 750 // this also causes a double msg/displayment of the life... argh... both of my changed scripts (current and maximum life changed scripts)... need fixing... argh... meh...

player.maximum_life_integer_attribute = 1000 // this causes a double msg/displayment of the life, from within the 'player.changedmaximum_life_integer_attribute' Script Attribute, as the change to '1000' (player.maximum_life_integer_attribute) activates it this changed script the first time (the first msg/displayment of the life), and then within the changed script's scripting, it changes to '999' (player.maximum_life_integer_attribute), which activates the changed script for a second time, and thus the 2nd msg/displayment of the life ---- I'm not sure how to fix this without putting a lot more thinking/thought into it

]]>

</attr>

</object>

<object name="Page2">

<attr name="description" type="string">This is page 2. Type a description here, and then create links to other pages below.</attr>

</object>

<object name="Page3">

<attr name="description" type="string">This is page 3. Type a description here, and then create links to other pages below.</attr>

</object>

</asl>
``````

P.S.

the Game Book doesn't seem to have Turnscripts implemented/added to it, but please correct me if I'm wrong about it

(you can also use the page and moving to it, for doing whatever actions/displayment of your life and/or whatever else, instead of using the special 'changed' Script Attributes)

(I didn't get around to seeing if the Game Book has the 'statusattributes' String Dictionary Displayments functionality or not, so don't know if these are implemented/added-to the Game Book or not)

If your using gamebook, set the page type to 'text and script', and set the counter 'health' or 'hp' to 100.

Thank you. I will try it by tomorrow after office hours.

I did it but this error appears.

``````System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at System.Windows.Forms.TreeView.OnAfterSelect(TreeViewEventArgs e)
at System.Windows.Forms.TreeView.TvnSelected(NMTREEVIEW* nmtv)
at System.Windows.Forms.TreeView.WmNotify(Message& m)
at System.Windows.Forms.TreeView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
``````

That error indicates that there's something wrong with the editor itself. If it's only doing it when you make this particular change, then try to describe exactly what you're changing in as much detail as possible, so that the developers can maybe fix the error.

If the same error appears whenever you try to edit a script, it might be worth uninstalling and reinstalling Quest.

I see. I will try reinstalling if I still have the old version installer. I'm afraid if the new version may not be incompatible with what I have. But thank you.

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

Quest
Squiffy
ActiveLit

Documentation
Blog
GitHub

Forums