SCI Studio Tutorial Chapter 21 - Creating and Using Doors
We will now create the door to link rm001 to rm002. Doors are very similar to props. In fact, like actors, they too are derived from the Prop class.
Importing The Door's View
Before setting up the door, we will need to add a view for it. To save time, instead of drawing it yourself, you can import one.
|Click on the main menu's "Resource"->"Add Sample"->"View"->"Door". When the "Add Resource" dialog pops up, save it as view number 2.|
|Next, click on the script folder and the list of scripts in your game will appear on the right, along with a preview of the first script.|
|Scroll down the list until you see the "rm001" script. Double click on it, and the script editor will open up with the script.|
Setting Up The Door
Now that the view has been added, we can set up the door. In rm001.sc, scroll down to the end of the file. There is where we'll add the door instance.
Add the following lines of code:(instance aDoor of Door (properties y 100 x 195 view 2 entranceTo 2 locked TRUE ) )
This creates the door with view.002 (the door view) as it's view, and positions it at 195,100. The entranceTo 2 means that when the ego goes through it, it will go to rm002. The locked property is set to TRUE because we want the ego to only be able to open it if it has the key. Next, we will add the code to initialize it.
Go to the end of the rm001 instance's init() method.
Add the following line of code:(aDoor:init())
This calls the door's init() method setting it up and making it visible.You can see now just how easy making SCI games is! The props, actors and doors all are essentially set up the same way.
Click the "Compile and Run" button and you will see the door is now part of your game!
Interacting With The Door
To allow the player to open and close the door, we will add a Said handler.
Scroll down the RoomScript instance's handleEvent method and add the following line of code:(if(Said('open/door')) (if(send gEgo:has(INV_KEY)) (aDoor:locked(FALSE)) )(else (aDoor:locked(TRUE)) ) (aDoor:open()) ) (if(Said('close/door')) (aDoor:close()) )
First, this checks if the player typed "open door". If they did, it checks if they have the key. If they have the key, it set's the door's locked property to FALSE, otherwise, it sets it to TRUE. Finally, it calls the door's open() method. If the ego is close enough and the door is unlocked, it will open the door. Otherwise, it will display the appropriate message.
Second, it checks it the player typed "close door". If they did, it calls the door's close() method. If the ego is close enough and the door is open, it will close the door. Otherwise, it will display the appropriate message.
Finally, we'll need to set it up so when ego enters the room coming back from room 2, it is positioned at the doorway. Scroll up to the rm001 init() method.
Adjust the gPreviousRoomNumber switch to the following:(switch(gPreviousRoomNumber) (case 2 (send gEgo: posn(195 110) loop(2) ) ) )
The ego is positioned just below the door when entering, and the loop is set to 2, so it is facing forward.
Click the "Compile and Run" button and you will see the door is now fully functional!
To test out the door, pick up the key, then go close to it and type "open door". you can now go to the second room!
Additional Information On Doors
To have multiple doors, you just need to change their doorCtrl, roomCtrl, and doorBlock controls.The default doorCtrl is color #1 (navy blue), roomCtrl is color #2 (green), and doorBlock color is #14 (yellow).
- doorCtrl specifies the color which is used to determine if ego is near the door. If ego is on the doorCtrl and the player types "open door", it will open the door. Otherwise, it will tell the user that they aren't close enough.
- If ego is on the roomCtrl color, it considers that the ego has walked through the door, so it will go to the room specified by it's entranceTo property.
- doorBlock specifies the door boundary. If the door is closed, this is treated like the white boundary control. If the door is open, it is ignored and the ego can walk past it.
That sums up the basics of creating, using and interacting with doors. In the next chapter, you will learn about working with the player's score!