Making objects work together.

Matty2theMax
20 Aug 2021, 20:50

I know this can be done because I've seen it done on someone else's game but I can't find any instructions on how to do it. Having spent a couple of hours looking I decided it best to ask you lovely, knowledgeable people.

I have a dark basement and I want users to need to find a torch to go down there. However, I don't want to make it too easy. I want it so that the torch needs batteries which they will have to buy from the shop. So what I need to do is create a script where you can use the batteries once you have them in the torch and only then will it work on lighting the basement.

Does that make sense?

I was playing a game and in theirs, if you mixed one chemical with another it created a third object and the two chemicals then disappeared from the inventory... how was that done?

Appreciate any suggestions.


mrangel
21 Aug 2021, 16:03

For the torch, I suspect the natural way to do it would be making the torch a container. Then the player can open and close it, and put the batteries in.

However, you don't want the player to put some random object in there, so you use the addscript attribute ("Script to run when trying to add an object:" in the editor). You would set it to something like:

if (object = batteries) {
  msg ("You put the batteries in the torch.")
  MoveObject (batteries, this)
  if (GetBoolean (this.switchedon)) {
    msg ("The torch lights up!")
    this.lightsource = true
  }
}
else {
  msg ("That won't fit in the battery compartment.")
}

Then you would modify the torch's switchable script so that it only works if the batteries are in place:
Script for "After switching on the object:"

if (Contains (this, batteries)) {
  msg ("You switch it on and it lights up.")
  this.lightsource = true
}
else {
  msg ("You press the button, but nothing happens. Maybe it needs vatteries.")
}

And "After switching off the object:"

if (this.lightsource) {
  msg ("You switch it off.")
  this.lightsource = false
}
else {
  msg ("You press the button, but still nothing happens.")
}

You'd also probably want the torch to go out if you remove the batteries. So you could modify the 'addscript' above like this:

if (object = batteries) {
  msg ("You put the batteries in the torch.")
  MoveObject (batteries, this)
  if (GetBoolean (this.switchedon)) {
    msg ("The torch lights up!")
    this.lightsource = true
  }
  object.take => {
    if (this.parent = torch) {
      if (torch.lightsource) {
        msg ("As soon as you remove the batteries, the torch goes out.")
        torch.lightsource = false
        this.take = true
      }
    }
    AddToInventory (this)
  }
}
else {
  msg ("That won't fit in the battery compartment.")
}

This adds a new 'take' script to the batteries, that checks if they're in the torch and if so makes it go dark.


Another method would be making the object usable; but I think the player is more likely to guess "put batteries in torch" than "use batteries on torch".

There are a lot of different ways you can do this; it just depends how realistic you want to be. I think there's a lot of people who can help with the script part; but first you should probably decide in more detail how you want the player to use it.


Matty2theMax
22 Aug 2021, 08:27

Thanks mrangel. You're always there to come to the rescue on these sort of things. I have to say on first glance I'm pretty confused but, then again, that's my go-to default expression.
I'll have a play with what you've sent me and try an implement it into my game. Hopefully once I start I'll get my head around it.
You're a star, thank you again.