r/Inform7 • u/Dex21772 • Mar 23 '26
Creating a response when a _kind_ of thing is present.
Hi folks,
I've run into a hurdle in trying to streamline some code. Is there a way to create a response dependent on whether a thing of a specific kind is present? In my case I've defined a clock as a kind of thing and defined a Wrist-Watch, a Desk-Lamp, and a Wall-Clock as a clock. Because all of these does the same thing with the same results I'd like to write some code something like the following(which doesn't work), instead of writing the code repeatedly for each of the things I've defined as a 'clock'.
A clock is a kind of thing.
Wrist-Watch is a clock.
Desk-Clock is a clock.
Wall-Clock is a clock.
Checking Time is an action applying to nothing. Understand "Check Time" as checking time.
Check Checking Time:
`if a clock is in the location of the player:`
`say "It's currently [time of day].";`
`now the time of day is 1 minute before the time of day;`
`else:`
`say "You'll need to find a device which displays the time to accomplish that.";`
`stop the action;`
Thanks!
2
u/Dex21772 Mar 24 '26
Thanks folks, I read through your responses and then re-wrote the code and it worked. I must have got something wrong in the syntax.
3
u/tobiasvl Mar 23 '26
Your code works for me? I just had to add the following to your code:
And then it worked fine. Note that your condition (
if a clock is in the location of the player) might not be exactly what you want:After taking the clock, it's no longer in the location of the player, it's now enclosed by the player. Enclosure is transitive, though, so it is still enclosed by the location of the player! https://ganelson.github.io/inform-website/book/WI_3_25.html
But your condition doesn't take into account anything else that might affect the clock (darkness, etc).
2
u/Olaxan is a person in The Lab Mar 23 '26 edited Mar 23 '26
Your code should work, roughly. You probably have some syntax wrong. I wrote this simple case which should also work.
Which gives:
EDIT: Okay, while this does work, more commonly this doesn't really require subclassing unless you have additional requirements. Better to do this (just working within Inform's regular system of instancing):
(I added a wristwatch that you can take with you to demonstrate that also works).
My original code is flawed -- it only works, I believe, because I redefine wall clock with the exact same name; but this could cause Inform to become confused easily. If you want to go by that route, you'd need to define wall clock/wristwatch etc. as kinds of clock (
A wall clock is a kind of clock) -- and then you'd be able to place such constructs in the world, but you'd need to give them unique names as with everything else.2
u/Shardworkx Apr 29 '26
Rather than moving the time back, it might be better to make checking time an action out of world.