Checking Whether An Inventory Item Is Owned By A Room Or Actor

From SCI Wiki
Jump to navigationJump to search

Here is how you can check to compare whether an inventory item is owned by a room or an actor (ie- ego). This is useful for checking if an item in a room has been picked up yet and if the object representing it in a room should be visible or not when you walk into the room. Say there was a book on a table. When you walk into the room it should not be on the table if you have already picked it up. Here's how to code that.

First we will create a public procedure which can be called from any script. Place this block of code with the other public procedures in the 'Main' script:

Code:
(procedure public (IsOwnedBy invItem roomOrActor)
	(var checkObject)
	= checkObject (send gInv:at(invItem))
	(if(IsObject(checkObject))
	return (send checkObject:ownedBy(roomOrActor))
	)(else
		return(0)
	)
)


The first parameter 'invItem' is the inventory item name listed in 'GAME.SH'. The second parameter 'roomOrActor' is the name or integer of either the room or actor you wish to check to see how owns the item. This can be any room number you want or it can be the global variable 'gRoomNumber' which is always the number of the current room ego is in.

To check if the inventory item is supposed to show up in the current room or not, go to the script of the room in question and add this line of code in the doIt method of the room's instance:

Code:
(if (== IsOwnedBy(INV_ITEM gRoomNumber) TRUE)
    ...
)


Where '...' is where you would put the line of code to init the object (prop) on screen. Such as "(Book:init())".

You can also check whether ego or another actor owns the item with this line of code:

Code:
(if (== IsOwnedBy(INV_ITEM ego) TRUE)
    ...
)


Two points of note: You only need to do the null check if you're worried you may pass something other than a valid inventory number to this code. This code supports checking both whether an object is in a room or in ego's possession. In fact, there is a third use: You can see whether an inventory object is irretrievably gone from the game (if you call gEgo:put with just one parameter, this is what will happen) - and you can check by passing the number -1 for roomOrActor.

Also, for any of this to work you will first need to set the 'owned' property of each inventory item when it is declared in the 'Main' script. So, if you wanted to have the book owned by room 23, for example, you would declare it like so:

Code:
(instance {Old Book} of Iitem
	(properties
		said '/book,(book<old)'
		description
			"This is an old book"
			owner 23
			view 800
			loop 0
			cel 0
			script 0
	)
)