Checking whether an object being picked up is in a certain container
BigSi67
08 May 2020, 13:32Hi,
I am trying to check whether an object being picked up is currently in a container. I tried to add a check in the object's Take script that checked whether object was in the container or not:
e.g.
if (Contains (trunk,coin)) {
msg ("The coin is in the trunk")
}
But it doesn't work because the script is called after the object is taken so it isn't in the container anymore. How can I check where it was before it was taken?
Presumably if there is no other way I could add an attribute and set it when the object is placed in the container but I want to do this for several objects so it would get pretty messy. But there must be a way of doing it automatically right?
Thanks for any suggestions
Simon
mrangel
08 May 2020, 13:44But it doesn't work because the script is called after the object is taken so it isn't in the container anymore. How can I check where it was before it was taken?
There are two scripts you can put on an object. ontake
is run after an object is taken; while take
is run instead of the default take behaviour.
If you give the object a take
script, you can check what container it is in. You just need to remember to add the object to the player's inventory afterwards.
So you could either move your existing script to the take
script, or give it a simple take
script:
this.takenfrom = this.parent
AddToInventory (this)
which adds an attribute takenfrom
to store where the player is taking it from.
BigSi67
08 May 2020, 14:34The GUI just gives you ontake presumably as there was only one Take option under the Inventory tab of the object on the GUI which was the one I was using. Presumably you need code view to use the other method.
Thanks for the quick response.
Simon
mrangel
08 May 2020, 15:01Right at the top of the inventory tab is a drop down list which lets you choose "Default behaviour" or "Run script". This is where you add a take
script.
(It's most commonly used if there's some condition determining whether or not you can take something; which is why if the object is taken you need to make the script add it to the inventory)
Hope that helps :)
BigSi67
09 May 2020, 13:45Yes, it is working now. Thanks for your help.