Difference between revisions of "Create story script"

From XStoryPlayer Wiki
Jump to: navigation, search
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
In this tutorial we will add some scripting: Give feedback to player, make the doors open when a specific event occurs, let female alien walk by.
+
In this tutorial we will add some scripting: Make the doors open when a specific event occurs, Give feedback to player.
  
 
<ol>
 
<ol>
<li>We don't want the doors to open automatically. We need to make the doors objects that can be driven by the script.<br>
+
<li>We don't want the doors to open automatically. We need to make the doors into objects that can be driven by the script.<br>
 
Open the <code>/init/story/scenes.dat</code> file and add under <code>data {...}</code> :
 
Open the <code>/init/story/scenes.dat</code> file and add under <code>data {...}</code> :
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
Line 34: Line 34:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
This will make a door object from the scene object. Now we can also open the door using a script.
 
This will make a door object from the scene object. Now we can also open the door using a script.
 +
</li>
 +
<li>Now lets test the doors. Open the <code>/init/story/player/brain/run.dat</code>. This file contains the state code of the player.<br>
 +
Change the code to this:
 +
<syntaxhighlight lang="cpp">
 +
#include "init/std/base/main/brain/code/run.dat"
 +
 +
//-------------------------------------------
 +
 +
<state_run>
 +
 +
case (state.dyn.me.do.state)
 +
{
 +
  [RUN0] state_run0();
 +
}
 +
 +
</state_run>
 +
 +
//-------------------------------------------
 +
 +
<state_run0>
 +
 +
case (state.dyn.me.do.state2)
 +
{
 +
  [NONE]
 +
  {
 +
    // Set timer to 3 seconds
 +
    do_set_timer(3);
 +
   
 +
    state.dyn.me.do.state2 = START;
 +
  }
 +
 
 +
  [START]
 +
  {
 +
    // Wait for timeout
 +
    loc.ts = GetTs(); 
 +
    [loc.ts < state.dyn.me.do.ts] return;
 +
 +
    // Open doors
 +
    loc.so.obj = SPACESHIP1:CELL1;
 +
    loc.so.par = DOOR_SET_STATUS;
 +
    loc.so.val = DOOR_SETVAL_STATUS_OPEN;    
 +
    SetObject(loc.so);
 +
 +
    loc.so.obj = SPACESHIP1:CELL2;
 +
    loc.so.par = DOOR_SET_STATUS;
 +
    loc.so.val = DOOR_SETVAL_STATUS_OPEN;    
 +
    SetObject(loc.so);
 +
 +
    // Set timer to 6 seconds
 +
    do_set_timer(6);
 +
 +
    state.dyn.me.do.state2 = OPEN;
 +
  }
 +
 
 +
  [OPEN]
 +
  {
 +
    // Wait for timeout
 +
    loc.ts = GetTs(); 
 +
    [loc.ts < state.dyn.me.do.ts] return;
 +
 +
    // Close doors
 +
    loc.so.obj = SPACESHIP1:CELL1;
 +
    loc.so.par = DOOR_SET_STATUS;
 +
    loc.so.val = DOOR_SETVAL_STATUS_CLOSE;    
 +
    SetObject(loc.so);
 +
 +
    loc.so.obj = SPACESHIP1:CELL2;
 +
    loc.so.par = DOOR_SET_STATUS;
 +
    loc.so.val = DOOR_SETVAL_STATUS_CLOSE;    
 +
    SetObject(loc.so);
 +
 +
    // Set timer to 6 seconds
 +
    do_set_timer(6);
 +
 +
    state.dyn.me.do.state2 = START;   
 +
  }
 +
}
 +
 +
</state_run0>
 +
</syntaxhighlight>
 +
This code opens the doors and then closes them again.
 +
</li>
 +
<li>Start the abducted scene and check if the doors open and close in a continuous loop.</li>
 +
<li>Now add a sound to the doors. Copy the <code>unlock2.wav</code> to the <code>/scenes/sounds/door</code> directory.<br>
 +
Open the <code>/sources/scenes/spaceship/scene.ini</code> file and add this code to both cell door objects:
 +
<syntaxhighlight lang="cpp">
 +
rb "cell1Shape"
 +
{
 +
  ...
 +
 +
  media
 +
  {
 +
    elem[0]
 +
    {
 +
      filename = "scenes/sounds/door/unlock2.wav";
 +
 +
      type      = AUDIO;
 +
      stream    = false;
 +
      pos3d    = true;
 +
      pos3d_min = 1.0;
 +
      start    = false;
 +
      loop      = false;
 +
      volume    = 1.0;
 +
    }
 +
  }
 +
}
 +
</syntaxhighlight>
 +
Run the filemaker and create the objects.
 +
</li>
 +
<li>Run the abducted story, because the cell objects are door objects the sound in the object is automatically used for the door sounds.</li>
 +
<li>Now let us make a start with the story. Open the <code>/init/story/player/brain/run.dat</code> again.<br>
 +
Change the <code>state_run0</code> function to this:
 +
<syntaxhighlight lang="cpp">
 +
<state_run0>
 +
 +
case (state.dyn.me.do.state2)
 +
{
 +
  [NONE]
 +
  {
 +
    do_set_timer(10);
 +
   
 +
    state.dyn.me.do.state2 = START;
 +
  }
 +
 
 +
  [START]
 +
  {
 +
    loc.ts = GetTs(); 
 +
    [loc.ts < state.dyn.me.do.ts] return;
 +
 +
    // Give feedback to player
 +
    feedback.s        = "What happened to me? I remember waking up from a bright light.";
 +
    feedback.dur      = 6.0; // Show feedback for 6 seconds
 +
    feedback.rem.delay = 5.0; // Wait 5 seconds
 +
    feedback.rem.s    = "And now I am stuck in this strange place."; // Then show this feedback
 +
 +
    do_set_timer(30);
 +
 +
    state.dyn.me.do.state2 = START2;
 +
  }
 +
 
 +
  [START2]
 +
  {
 +
    loc.ts = GetTs(); 
 +
    [loc.ts < state.dyn.me.do.ts] return;
 +
 +
    feedback.s = "Wait, I believe I hear someone coming...";
 +
 +
    do_set_timer(15);
 +
 +
    state.dyn.me.do.state2 = START3;
 +
  }
 +
}
 +
 +
</state_run0>
 +
</syntaxhighlight>
 +
This code shows some feedback to the player starting the story off. Now lets add a female alien character in the next tutorial '[[Add character to scene]]'.
 
</li>
 
</li>
 
</ol>
 
</ol>

Latest revision as of 16:18, 12 January 2015

In this tutorial we will add some scripting: Make the doors open when a specific event occurs, Give feedback to player.

  1. We don't want the doors to open automatically. We need to make the doors into objects that can be driven by the script.
    Open the /init/story/scenes.dat file and add under data {...} :
    door_objecti CELL1
    {
      rotate   = false; // Door translates
     
      q        = 4.5;  // Open height
      q_min    = 0.0;  // Min height
      q_max    = 6.0;  // Max height
      q_spring = 30.0; // Spring used for opening door
     
      open = true; // Door status
     
      obj_name = "cell1"; // Object in scene that is the door
    }
     
    door_objecti CELL2
    {
      rotate   = false;
     
      q        = 4.5;
      q_min    = 0.0;
      q_max    = 6.0;
      q_spring = 30.0;
     
      open = false;
     
      obj_name = "cell2";
    }

    This will make a door object from the scene object. Now we can also open the door using a script.

  2. Now lets test the doors. Open the /init/story/player/brain/run.dat. This file contains the state code of the player.
    Change the code to this:
    #include "init/std/base/main/brain/code/run.dat"
     
    //-------------------------------------------
     
    <state_run>
     
    case (state.dyn.me.do.state)
    {
      [RUN0] state_run0();
    }
     
    </state_run>
     
    //-------------------------------------------
     
    <state_run0>
     
    case (state.dyn.me.do.state2)
    {
      [NONE]
      {
        // Set timer to 3 seconds
        do_set_timer(3);
     
        state.dyn.me.do.state2 = START;
      }
     
      [START]
      {
        // Wait for timeout
        loc.ts = GetTs();  
        [loc.ts < state.dyn.me.do.ts] return;
     
        // Open doors	
        loc.so.obj = SPACESHIP1:CELL1;
        loc.so.par = DOOR_SET_STATUS;
        loc.so.val = DOOR_SETVAL_STATUS_OPEN;	    
        SetObject(loc.so);
     
        loc.so.obj = SPACESHIP1:CELL2;
        loc.so.par = DOOR_SET_STATUS;
        loc.so.val = DOOR_SETVAL_STATUS_OPEN;	    
        SetObject(loc.so);
     
        // Set timer to 6 seconds
        do_set_timer(6);
     
        state.dyn.me.do.state2 = OPEN;
      }
     
      [OPEN]
      {
        // Wait for timeout
        loc.ts = GetTs();  
        [loc.ts < state.dyn.me.do.ts] return;
     
        // Close doors	
        loc.so.obj = SPACESHIP1:CELL1;
        loc.so.par = DOOR_SET_STATUS;
        loc.so.val = DOOR_SETVAL_STATUS_CLOSE;	    
        SetObject(loc.so);
     
        loc.so.obj = SPACESHIP1:CELL2;
        loc.so.par = DOOR_SET_STATUS;
        loc.so.val = DOOR_SETVAL_STATUS_CLOSE;	    
        SetObject(loc.so);
     
        // Set timer to 6 seconds
        do_set_timer(6);
     
        state.dyn.me.do.state2 = START;    
      }
    }
     
    </state_run0>

    This code opens the doors and then closes them again.

  3. Start the abducted scene and check if the doors open and close in a continuous loop.
  4. Now add a sound to the doors. Copy the unlock2.wav to the /scenes/sounds/door directory.
    Open the /sources/scenes/spaceship/scene.ini file and add this code to both cell door objects:
    rb "cell1Shape"
    {
      ...
     
      media
      {
        elem[0]
        {
          filename = "scenes/sounds/door/unlock2.wav";
     
          type      = AUDIO;
          stream    = false;
          pos3d     = true;
          pos3d_min = 1.0;
          start     = false;
          loop      = false;
          volume    = 1.0;
        }
      }
    }

    Run the filemaker and create the objects.

  5. Run the abducted story, because the cell objects are door objects the sound in the object is automatically used for the door sounds.
  6. Now let us make a start with the story. Open the /init/story/player/brain/run.dat again.
    Change the state_run0 function to this:
    <state_run0>
     
    case (state.dyn.me.do.state2)
    {
      [NONE]
      {
        do_set_timer(10);
     
        state.dyn.me.do.state2 = START;
      }
     
      [START]
      {
        loc.ts = GetTs();  
        [loc.ts < state.dyn.me.do.ts] return;
     
        // Give feedback to player
        feedback.s         = "What happened to me? I remember waking up from a bright light.";
        feedback.dur       = 6.0; // Show feedback for 6 seconds
        feedback.rem.delay = 5.0; // Wait 5 seconds
        feedback.rem.s     = "And now I am stuck in this strange place."; // Then show this feedback
     
        do_set_timer(30);
     
        state.dyn.me.do.state2 = START2;
      }
     
      [START2]
      {
        loc.ts = GetTs();  
        [loc.ts < state.dyn.me.do.ts] return;
     
        feedback.s = "Wait, I believe I hear someone coming...";
     
        do_set_timer(15);
     
        state.dyn.me.do.state2 = START3;
      }
    }
     
    </state_run0>

    This code shows some feedback to the player starting the story off. Now lets add a female alien character in the next tutorial 'Add character to scene'.