Actor Event Triggers

From SCI Wiki
Jump to navigationJump to search

Chapter 4 - Moving Actors and Triggering Events

 

Almost as likely as you'll need to move actors in your game, you'll want to trigger events when they reach their destination. This is simply implemented with the room script instance's changeState() method.

Getting Started

*Not needed if you have already done the chapter, Chapter 3 - Making Actors Move Along Paths.

View If you haven't already done so, open your game in the resource explorer and select "Resource"->"Add Sample"->"View"->"Brian" from the menu.

Save it to your game as view.001.

Rm001 Script You can obviously use this in any script, but for demonstration purposes, open up the game's "rm001.sc" script.

Since the ego is an actor, everything in this lesson can be applied to it as well. However, to keep things simple, we will create another actor. Scroll down to the end of the script and add the following lines of code:

Code:
(instance aMan of Act
  (properties
    y 170
    x 190
    view 1
  )
)

This simply creates an actor which uses view.001 and positions it at (190,170).

Game Instance

Next, go to the game's instance, then scroll down to the bottom of the init method.

Set up the actor with the following lines of code:

Code:
(aMan:
  init()
  setCycle(Walk)
)

This simply initializes the actor, then sets it's cycle to Walk. If the cycle wasn't set, it would just drag across the screen. It's now ready for it's movement to be set up.

Setting Up The Actor's Motion

Simply call the setMotion method and set it's motion to MoveTo. Make sure the fourth parameter is set the the script's RoomScript instance.

Code:
(aMan:
  init()
  setCycle(Walk)
  setMotion(MoveTo 180 150 RoomScript)
)

The actor is now set to move to the coordinates (180,150).

Handling States

Script states are used as event triggers. States are fully explained in the Volume One of the tutorial.

Each state has a number identifying it.

Add the following method to the RoomScript instance:

Code:
(method (changeState newState)
  = state newState
  (switch (state)
    (case 0
      Print("This is state 0! The room has started!")
      = cycles 1
    )
    (case 1
      Print("This is state 1! The room has now FULLY started!")
    )
    (case 2
      Print("The view has moved to it's first position!")
      (aMan:setMotion(MoveTo 250 100 RoomScript))
    )
    (case 3
      Print("The view has moved to it's second position!")
    )
  )
)
  1. When the room script is first executed, state 0 is called.
  2. If a case is provided for state 0, it is executed.
  3. If state 0's case sets the cycles property to anything greater than zero, it will wait that many interpreter cycles and then execute the next state.
  4. In this case, it sets the cycles to 1, so one interpreter cycle after executing state 0, it executes state 1.
  5. By this time, the room has been fully set up.
  6. Since state 1's case does not set the cycles property, it ends the state execution.
  7. Next, when the actor reaches it's first destination (180,150), it calls state 2's case (the next state).
  8. In this example state 2's case sets the actor to move to (250,100).
  9. When the actor reaches it's destination (250,100), it calls the next state's case (state 3).
  10. And of course, since the states don't set the cycles property, it ends the state execution.

Compile and run and you'll see it all in action!

That sums up actor movement and triggering events.

 

< Previous: Chapter 3 - Making Actors Move Along Paths Next: Chapter 5 - Allowing Actors to Walk Through/Over Views/Props >