Script Template

OurJud
I was in two minds where to put this. I thought about Libraries and Code Samples, but then changed my mind as it's more question based.

Anyway, I have a recurring scenario in most of my games, which always gives me headache when trying to account for all possibilities. I usually butcher my way through it and end up with a long and complex series of scripts, which I'm sure can be done more efficiently. I did have a template for this which I used to just copy & paste, but then my PC died and all my code samples with it.

The scenario is this:

The player comes across a hut/shed which can be accessed by kicking down the door (I include a hint for this). Inside the shed is an object (doesn't matter what - let's say an axe).

So, here's a list of the things I need to cater for, and I wonder if any of you who are super proficient with scripts could provide a 'template' which will cater.

• The shed location needs two descriptions - a 'first time' and 'otherwise'
• These descriptions need to recognise and output two factors:
1. Has the door already been smashed in?
2. Has the axe been taken?

I'll have a go at this in my own unique way, and post my efforts later to see how they compare, but please feel free to offer your own before then.

PS. I realise now this will require two scripts/locations - outside the hut and inside.

XanMag
I'll throw this code out there and then wonder if it is of any use. I have a script in there that changes the room after the sixth turn in there.

Here is a code for a changing script room from my tutorial game:

  <object name="room that changes script once">
<inherit name="editor_room" />
<description type="script"><![CDATA[
if (GetBoolean(room that changes script once, "changed")) {
msg ("<b>The room looks brand new. The floor is covered in carpet and the walls and ceiling got a new colorful coat of paint!</b>")
}
else {
firsttime {
msg ("This room will change appearance twice. The first is the following:<br/><br/><b>You are in a bare room. Nothing is here. Four white walls, a ceiling, and a floor.</b><br/><br/>If you look at the room again, you will see a simplified version of the description. You can even look at the walls, ceiling, and floor.<br/><br/>These descriptions were accomplished with a first time/otherwise script. When you entered the room, a turnscript started to change the room after six turns. You'll know when to look around again.")
}
otherwise {
msg ("<b>Bare room. White walls and ceiling. Wooden floor.</b><br/><br/>In a few turns, the room will change description drastically. This was set up in the 'After entering room' script. After six turns, I raised a flag on the 'room that changes script once' room. I called it changed. In the floor, ceiling, walls, and room description, I used an 'If' script to check and see if the flag 'changed' was true.")
}
}
]]></description>
<enter type="script">
SetTurnTimeout (6) {
msg ("Without warning, a slew of construction workers and artists come rushing in. In a hysteria of carpentry and painting the room you are in gets a make-over. As soon as they arrived, they depart, leaving you in a new room! Take a look!")
SetObjectFlagOn (room that changes script once, "changed")
}
</enter>
<object name="walls1">
<inherit name="editor_object" />
<alias>walls</alias>
<alt type="stringlist">
<value>wall</value>
</alt>
<usedefaultprefix type="boolean">false</usedefaultprefix>
<look type="script">
if (GetBoolean(room that changes script once, "changed")) {
msg ("The walls are painted in an array of colors. They look like a swirl of graffiti. There are pinks and yellows and blues and whites and greens, all swirled together in a hypnotic blend.")
}
else {
msg ("Four white walls. Nothing remarkable.")
}
</look>
</object>
<object name="floor">
<inherit name="editor_object" />
<look type="script">
if (GetBoolean(room that changes script once, "changed")) {
msg ("The floor that once was boring wood is now carpeted and plush. Feels good on your feet!")
}
else {
msg ("One wooden floor. Quite boring.")
}
</look>
</object>
<object name="ceiling">
<inherit name="editor_object" />
<look type="script">
if (GetBoolean(room that changes script once, "changed")) {
msg ("The ceiling was once flat, boring, and white. It is now accessed by crown molding and has a comfortable beige tint to it. Much nicer!")
}
else {
msg ("A ceiling. Plain. Boring.")
}
</look>
</object>
<command name="explain room cmd">
<pattern>explain room</pattern>
<script><![CDATA[
msg ("There are really two ways to get the room description to change once. The easiest is the use of a 'First time/Otherwise' script.<br/><br/>1. Simply choose to run a script instead of having the default 'text' description.<br/>2. To this script, add new script and select the 'First time...' script under the scripts category in the GUI.<br/>3. In the First time section below the Run script, click add new script. Type whatever message you want for your room description.<br/>4. Repeat this for the 'Otherwise' section. Add a message that you want to print every other time the play enters the room or types 'look'.<br/>---<br/>5. In addition to this, I added a 'Run script after set number of turns' to give you another look at the room. After six turns, I printed a message to indicate that the room is now different.<br/>6. I also added a flag... add new script, set object flag (under variables), and set object flag on the room called changed.<br/>7. You can place an 'If' script in the room description box and have it look like this --> If object has flag object [current room name] flag name changed, then print the message explaining the changed script.")
]]></script>
</command>
</object>


I have an 'If' script that checks for the 'changed' flag on the room, walls, floor, and ceiling. You would just need one on the room and the door.

OurJud
I get a bit confused when people post game script. I was looking for UI script.

Anyway, I'm not sure it's the headache I feared. Here's what I came up with. This uses flags, the 'lock exit / unlock exit' feature, and a custom command for 'kick door in; break down door etc'

shed_outside

firsttime {
if (GetBoolean(player, "shedopen")) {
msg ("There's a small shed here. The door hangs off its hinges.<br/><br/>You can go south or in.")
}
else {
msg ("There's a small shed here. The door is closed and locked with a padlock, but it all looks rather flimsy.<br/><br/>You can go south or in.")
}
}
otherwise {
if (GetBoolean(player, "shedopen")) {
msg ("The small shed sqauts miserbaly in the long grass, it's door hanging from the hinges.<br/><br/>You can go south or in.")
}
else {
msg ("The small shed sqauts miserbaly in the long grass. The door is closed and locked with a padlock.<br/><br/>You can go south or in.")
}
}

shed_inside (with "shedopen" flag set on "After entering the room" in the Scripts tab)

if (Got(Axe)) {
msg ("The inside is thick with dust.")
}
else {
msg ("The inside is thick with dust. There's an axe lying in the corner.")
}

XanMag
Here is what I just tossed together. See if we solved it the same way!



Or code
  <object name="outside hut">
<inherit name="editor_room" />
<description type="script">
if (GetBoolean(hut, "")) {
}
</description>
<object name="hut">
<inherit name="editor_object" />
<look type="script">
if (GetBoolean(hut door, "bashed")) {
msg ("It's a small hut with a gaping hole where the door used to be. The door lies around the threshold in ruins.")
}
else {
msg ("It's a small hut with a moderately sturdy door on its front that is supposed to keep scum like OurJud out!")
}
</look>
</object>
<object name="hut door">
<inherit name="editor_object" />
<scenery />
<look type="script">
if (GetBoolean(hut door, "bashed")) {
msg ("The door here lays in splinters. You sure showed it who is boss!")
}
else {
msg ("It's a door that with a little ramming or slamming or kicking might just collapse inward.")
}
</look>
<ram type="script">
firsttime {
msg ("You slam into the door with all your might. You hear a *CRACK* but you think that might have come from your shoulder! Maybe you should try again?")
}
otherwise {
if (GetBoolean(hut door, "bashed")) {
msg ("Why would you want to bash an already destroyed door. Just go in!")
}
else {
msg ("You throw your mighty belly against the door with all your might. *CRASH* The door is splintered into a gazillion pieces. Really... a gazillion.")
SetObjectFlagOn (hut door, "bashed")
}
}
</ram>
</object>
<object name="player">
<inherit name="editor_object" />
<inherit name="editor_player" />
</object>
<exit alias="in" to="inside hut">
<inherit name="indirection" />
<locked type="boolean">false</locked>
<runscript />
<script type="script">
if (GetBoolean(hut door, "bashed")) {
msg ("You step across the recently destroyed door and into the hut.")
MoveObject (player, inside hut)
}
else {
msg ("There is a door that blocks your way.")
}
</script>
</exit>
</object>
<object name="inside hut">
<inherit name="editor_room" />
<description type="script">
if (GetBoolean(hut door, "bashed")) {
if (GetBoolean(axe, "taken")) {
msg ("You are standing in a small hut. There is little to be seen here. Even the priceless axe that once rested on the wall is missing. You can go out if you step over the remains of the busted door.")
}
else {
msg ("You are in a small hut. There is little to be seen here except for the small axe that is resting against the wall. You can go out if you step over the remains of the busted door.")
}
}
else {
if (GetBoolean(axe, "taken")) {
msg ("You are standing in a small hut. There is little to be seen here. Even the priceless axe that once rested on the wall is missing. The hut door stands firmly anchored on its hinges.")
}
else {
msg ("You are in a small hut. There is little to be seen here except for the small axe that is resting against the wall. The hut door stands firmly anchored on its hinges.")
}
}
</description>
<object name="axe">
<inherit name="editor_object" />
<take type="script">
msg ("Don't go cutting your thumbs off, Paul Bunyon.")
SetObjectFlagOn (axe, "taken")
</take>
</object>
<exit alias="out" to="outside hut">
<inherit name="outdirection" />
</exit>
<object name="hut door1">
<inherit name="editor_object" />
<scenery />
<look type="script">
if (GetBoolean(hut door, "bashed")) {
msg ("The door here lays in splinters. You sure showed it who is boss!")
}
else {
msg ("It's a door that with a little ramming or slamming or kicking might just collapse inward.")
}
</look>
<ram type="script">
firsttime {
msg ("You slam into the door with all your might. You hear a *CRACK* but you think that might have come from your shoulder! Maybe you should try again?")
}
otherwise {
if (GetBoolean(hut door, "bashed")) {
msg ("Why would you want to bash an already destroyed door. Just go in!")
}
else {
msg ("You throw your mighty belly against the door with all your might. *CRASH* The door is splintered into a gazillion pieces. Really... a gazillion.")
SetObjectFlagOn (, "")
}
}
</ram>
<alias>hut door</alias>
</object>
</object>
<verb>
<property>ram</property>
<pattern>ram; slam; kick; punch; push</pattern>
<defaultexpression>"You can't ram; slam; kick; punch; push " + object.article + "."</defaultexpression>
</verb>

OurJud
Certainly better written than mine, from a literary aspect :lol:

If you could just post the scripts from the UI rather than the whole game code, I would be able to compare easier.

XanMag
Noted and will do. I posted the whole game code so, if you wanted, you could copy-paste it into a new game and see the whole thing in the UI.

The Pixie
OurJud wrote:• The shed location needs two descriptions - a 'first time' and 'otherwise'
• These descriptions need to recognise and output two factors:
1. Has the door already been smashed in?
2. Has the axe been taken?

The first factor depends on the exit from outside to inside being unlocked, so the best way is to check that flag, rather than setting a new one. If you go to the exit, and look at the list of attributed you should see "locked" is set to true. I guess it already has a name so it can be unlocked, say it is "entrance_to_shed", you could code it like this (the first time the player sees it the door must still be there, right?):
firsttime {
msg ("There's a small shed here. The door is closed and locked with a padlock, but it all looks rather flimsy.<br/><br/>You can go south or in.")
}
otherwise {
if (not entrance_to_shed.locked) {
msg ("The small shed squats miserably in the long grass, it's door hanging from the hinges.<br/><br/>You can go south or in.")
}
else {
msg ("The small shed squats miserably in the long grass. The door is closed and locked with a padlock.<br/><br/>You can go south or in.")
}
}

For the inside, you need to check if the axe is in the shed, not if the player has it. What if the player drops somewhere else?
if (not Axe.parent = this) {
msg ("The inside is thick with dust.")
}
else {
msg ("The inside is thick with dust. There's an axe lying in the corner.")
}

Or you could set the axe to be scenery, and check against that. I think if it gets picked up the scenery flag gets turned off, and if it is dropped again, it would appear in the list of stuff here (depending on how your game is set up). This could be better, but you should check what happens when you pick up and drop the axe back in the room, etc. and see if it works.
if (not Axe.scenery) {
msg ("The inside is thick with dust.")
}
else {
msg ("The inside is thick with dust. There's an axe lying in the corner.")
}

OurJud
Thanks, TP.

My game setups are always the same, in that I never auto-list any items / exits etc. Everything I want in the scene is just incorporated into the description.

The checking that the axe is in the shed rather than if the player is carrying it, is a good one. When I test it my way, if I drop the axe outside the shed, it gets magically returned to its original position, as you alluded to, so I really need to change that.

I'll test and see how it works your way when I drop it in the shed and outside the shed.

[edit] okay, so the checking the shed for the axe works. If I drop it outside the shed, it stays where I drop it. If I drop it inside the shed, I get the 'There's an axe lying in the corner.' line.

All good there. The only thing I need to be aware of is that because I don't auto-list objects, they don't get listed in my descriptions when I drop them elsewhere.

But maybe that's a puzzle for another thread.

Thank you all very much.

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

Support

Forums