Handling water

The Pixie
There is more than one way to do this, but this is what I recommend. This will involve setting attributes, so you will struggle with the on-line editor.

What we will do is to create a waterskin that can be filled and drunk from, and then set up a room with a pool of water and another with a tap.


The waterskin

So first create the waterskin, in the normal way. Give it two integer attributes, "full" and "capacity". Set them to 0 and 10 respectively. The full attribute will track how much water is in the waterskin, the capacity will be the maximum. You may want to play around with these values.

Then go to the verbs tab, and create a new verb, "fill". Paste in this code:
if (this.full = this.capacity) {
msg ("It is already full.")
}
else if (not GetBoolean(game.pov.parent, "watersource")) {
msg ("No water here.")
}
else {
msg ("You fill it.")
this.full = this.capacity
}

All this does is check if the waterskin is already full, then check if there is water. If all is okay, the waterskin gets filled.

You might want to give it a description like this:
The waterskin {if waterskin.full=0:is empty}{if waterskin.full<>0:contains some water}.

The text processor is a bit limited, and you might prefer to use a script so you can say how full it is.
if (this.full = 0) {
msg ("The waterskin.")
}
else if (this.full = this.capacity) {
msg ("The waterskin is full.")
}
else {
msg ("The waterskin is about " + (waterskin.full * 10) + "% full.")
}


A Source of Water

Now we will do a room with a pool of clear, fresh water. Create a room, give it a Boolean attribute, "watersource", and set it to true.

Now you should be able to go in-game and fill the waterskin with water.

For a room with a tap, create the room, then create the tap as an object in it. For the tap, on the Features tab tick Switchable. On the Switchable tab set it to "Can be switched...". Fill in the text boxes as you like. For the first script, paste in this:
this.parent.watersource = true

And for the second:
this.parent.watersource = false


Playing With water

What else do you want to do? Well, you could empty it. Create an "empty" verb, and paste this in.
if (this.full = 0) {
msg ("It is already empty.")
}
else {
msg ("You empty it.")
this.full = 0
}

Is there some point to emptying it? Perhaps there is a room with a fire, and emptying the waterskin will put the fire out. You could do that like this:
if (this.full = 0) {
msg ("It is already empty.")
}
else if (game.pov.parent = room_with_fire) {
msg ("You empty it over the fire, which sizzles and spits, then dies.")
this.full = 0
room_with_fire.fireout = true
}
else {
msg ("You empty it.")
this.full = 0
}


You might want to drink from the waterskin. Add a "drink from" verb, and paste this in:
if (this.full = 0) {
msg ("It is empty.")
}
else {
msg ("You take a drink from it.")
this.full = this.full - 1
}

Water is does is check the waterskin is not empty, and if not reduces its contents by 1. You might want to do some other things in the second block, if the player is going to die of thirst; that is up to you to sort out!

You might want to use the waterskin, i.e., (I guess) drink from it. Use is kind of built-in, so takes a bit of setting up. Go to the Features tab, and tick Use/Give. Then go to the Use/Give tab, and at the top, for "Use (on its own)", set the action to "Run script". This does the same thing as "drink from", so we can use that script (note that spaces are removed from the verb to make the attribute name).
do(this, "drinkfrom")

You could have pasted the script from "drink from" in here, but this way is better in the long term. If you later decide you want to change the effects of drinking water, you only have to change it in one place, rather than remember to do both. This is a principle in software engineering called DRY (Don't Repeat Yourself).


A Working Example

Here is a full game code that does all that, to prove it really works.
<!--Saved by Quest 5.6.5783.24153-->
<asl version="550">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="water">
<gameid>ceb08755-71c8-4656-b83f-e2003aebc0b9</gameid>
<version>1.0</version>
<firstpublished>2016</firstpublished>
</game>
<object name="room">
<inherit name="editor_room" />
<object name="player">
<inherit name="editor_object" />
<inherit name="editor_player" />
<object name="waterskin">
<inherit name="editor_object" />
<full type="int">0</full>
<capacity type="int">10</capacity>
<fill type="script">
if (this.full = this.capacity) {
msg ("It is already full.")
}
else if (not GetBoolean(game.pov.parent, "watersource")) {
msg ("No water here.")
}
else {
msg ("You fill it.")
this.full = this.capacity
}
</fill>
<look type="script">
if (this.full = 0) {
msg ("The waterskin is empty.")
}
else if (this.full = this.capacity) {
msg ("The waterskin is full.")
}
else {
msg ("The waterskin is about " + (waterskin.full * 10) + "% full.")
}
</look>
<empty type="script">
if (this.full = 0) {
msg ("It is already empty.")
}
else if (game.pov.parent = room_with_fire) {
msg ("You empty it over the fire, which sizzles and spits, then dies.")
this.full = 0
room_with_fire.fireout = true
}
else {
msg ("You empty it.")
this.full = 0
}
</empty>
<feature_usegive />
<givesingle />
<use type="script">
do (this, "drinkfrom")
</use>
<drinkfrom type="script">
if (this.full = 0) {
msg ("It is empty.")
}
else {
msg ("You take a drink from it.")
this.full = this.full - 1
}
</drinkfrom>
</object>
</object>
<exit alias="south" to="room_with_pool">
<inherit name="southdirection" />
</exit>
<exit alias="east" to="room_with_tap">
<inherit name="eastdirection" />
</exit>
</object>
<verb>
<property>fill</property>
<pattern>fill</pattern>
<defaultexpression>"You can't fill " + object.article + "."</defaultexpression>
</verb>
<object name="room_with_pool">
<inherit name="editor_room" />
<description>There is a pool of water.</description>
<watersource />
<exit alias="north" to="room">
<inherit name="northdirection" />
</exit>
<exit alias="east" to="room_with_fire">
<inherit name="eastdirection" />
</exit>
</object>
<verb>
<property>empty</property>
<pattern>empty</pattern>
<defaultexpression>"You can't empty " + object.article + "."</defaultexpression>
</verb>
<object name="room_with_fire">
<inherit name="editor_room" />
<fireout type="boolean">false</fireout>
<description>{if room_with_fire.fireout:The fire is out}{if not room_with_fire.fireout:There is a fire burning}.</description>
<exit alias="west" to="room_with_pool">
<inherit name="westdirection" />
</exit>
<exit alias="north" to="room_with_tap">
<inherit name="northdirection" />
</exit>
</object>
<object name="room_with_tap">
<inherit name="editor_room" />
<exit alias="south" to="room_with_fire">
<inherit name="southdirection" />
</exit>
<exit alias="west" to="room">
<inherit name="westdirection" />
</exit>
<object name="tap">
<inherit name="editor_object" />
<inherit name="switchable" />
<feature_switchable />
<switchonmsg>You turn on the tap, Water flows into the sink and down the plug hole.</switchonmsg>
<switchoffmsg>You turn it off.</switchoffmsg>
<onswitchon type="script">
this.parent.watersource = true
</onswitchon>
<switchedondesc>Water is flowing from it.</switchedondesc>
<onswitchoff type="script">
this.parent.watersource = false
</onswitchoff>
</object>
</object>
<verb>
<property>drinkfrom</property>
<pattern>drink from</pattern>
<defaultexpression>"You can't drink from " + object.article + "."</defaultexpression>
</verb>
</asl>


What next?

If you have several water containers, you might want to create a watercontainer type. This again follows the DRY principle.

What about different liquids? Is there a contaminated water source? A vial of poison that can be added to water? Perhaps a source of oil that does not mix with the water. Just remember, it will get complicated, and you are on your own!

The Pixie
I have turned this into a library, and made a demo game. Once you have added the library, you will see a new tab appears, Liquids. It can now handle different liquids too!

For Rooms:
You can set a room to be a source of a certain liquid, and specific what that is. You can also add a script that will be run if a container is emptied in the room.

For objects:
You can set them to be containers (of liquids) and given them a capacity and starting volume and liquid.

There are two functions you may want to add to your game to determine what happens if the player mixes two liquids together in a container, and what happens when she drinks from a container.

MixLiquid (object container, string liquidtype)


This function needs to set the liquidtype of container, based on what is already in it, and what you are adding to it. It should also print a message. The function is only used if the container was not already full or empty.

DrinkLiquid (string liquidtype)

This function determines what happens when the given liquid is drunk. The volume in the container will be automatically reduced, but you do need to print a message.

Library:
Demo:

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

Support

Forums