User Tools

Site Tools


door_scripting_tutorial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
door_scripting_tutorial [2017/11/03 14:59] justindoor_scripting_tutorial [2017/11/04 11:22] (current) justin
Line 6: Line 6:
  
 =====Creating the Model===== =====Creating the Model=====
-You'll first need to build a [[voxel]] model for your door. Create a new [[object]] in the [[Voxel Editor]] and build a door model similar to one of those shown in the examples below. For this tutorial, you'll want to have at least two separate frames, one for the door in its closed state and one in its open state. +You'll first need to build a [[voxel]] model for your door. Create a new [[object]] in the [[Voxel Editor]] and build a door model similar to one of those shown in the examples below. For this tutorial, you'll want to have at least two separate frames, one for the door in its closed state and one in its open state. Most of the examples in this tutorial will be be based on the first type of door shown.
  
 The style, orientation, and positioning all depend on what best matches the existing architecture of your game. For example, the top door in the images below is centered depth-wise since it's designed for a game where the walls take up the entire [[tile]] block. The bottom door is positioned flush with the grid edge since it's designed for a game where the walls are thinner and aligned with the [[tile]] edges. The style, orientation, and positioning all depend on what best matches the existing architecture of your game. For example, the top door in the images below is centered depth-wise since it's designed for a game where the walls take up the entire [[tile]] block. The bottom door is positioned flush with the grid edge since it's designed for a game where the walls are thinner and aligned with the [[tile]] edges.
Line 22: Line 22:
  
 {{:wiki:door_setup_in_map_01.gif?nolink|}} {{:wiki:door_setup_in_map_01.gif?nolink|}}
- 
-{{:wiki:door_setup_in_map_02.gif?nolink|}} 
  
 =====Scripting the Door for One-Time Use===== =====Scripting the Door for One-Time Use=====
Line 37: Line 35:
 </code> </code>
  
-The [[quick script]] will play the "door_open" [[sound|sound effect]], trigger the model's "open" [[animation]], replaced any adjacent "Interact Only" [[navigation_and_interaction|navigation paths]] (relative to the door's tile) with "Walk and Interact", and finally remove the door's [[script]]. The [[Replace Navigation]] function is key, as it will allow the player to pass through the door once the [[script]] has executed.+The [[quick script]] will play the "door_open" [[sound|sound effect]], trigger the model's "open" [[animation]], replace any adjacent [[navigation_and_interaction|navigation paths]] of type "Interact Only" (relative to the door's tile) with the type "Walk and Interact", and finally remove the door's [[script]]. The [[Replace Navigation]] function is key, as it will allow the player to pass through the door once the [[script]] has executed.
  
 If you'd prefer, you can also create a visual [[script]] in the [[Script Editor]] and assign it to the door instead of a [[quick script]]. This method of creating the [[script]] as its own file can be useful in cases where it's generic enough to be reusable (i.e. no explicit entity IDs are referenced). If you'd prefer, you can also create a visual [[script]] in the [[Script Editor]] and assign it to the door instead of a [[quick script]]. This method of creating the [[script]] as its own file can be useful in cases where it's generic enough to be reusable (i.e. no explicit entity IDs are referenced).
Line 44: Line 42:
  
 ====Method 2: Modify Navigation==== ====Method 2: Modify Navigation====
-(Explain method using [[Modify Navigation]] function. Must manually specify and reference tile IDsand call function for each connection but more control over navigation changes)+Alternatively, you can use the [[Modify Navigation]] function instead of the [[Replace Navigation]] function. With this method, you must first assign unique entity IDs to any [[tile|tiles]] needing their navigation modified. As shown in the image below, the [[tile]] on which the door is located has an ID of "door_tile" and the two adjacent [[tile|tiles]] have IDs of "tile01" and "tile02".
  
 {{:wiki:door_tile_ids_01.png?nolink|}} {{:wiki:door_tile_ids_01.png?nolink|}}
 +
 +This version of the [[script]] is very similar to the first one, with the exception of calling the [[Modify Navigation]] function to explicity modify each "connection" between the door's tile and the two adjacent ones to set the navigation type to “Walk and Interact”.
  
 <code lua> <code lua>
Line 59: Line 59:
  
 =====Allowing the Door to Be Toggled===== =====Allowing the Door to Be Toggled=====
-  * Script to allow door to be closed and opened, maintain state in entity property for use in script to determine which animation to play and navigation to set) +The previous [[script|scripts]] work fine for some cases, but most of the time you probably wouldn't want doors to be stuck in an open state. Fortunately we can improve these [[script|scripts]] and allow the door to be toggled between an opened and closed state
-  * Using "Save as Script" to save the quick script as reusable resource for other doors.+ 
 +The key to this functionality is maintaining the state of the door in an entity property using [[Set Entity Property]] and checking this property with an "if" statement (or the "Evaluate Condition" node in a visual [[script]]). According to the door's current state, you will trigger the appropriate [[animation]], modify the [[navigation_and_interaction|navigation paths]], and update the entity property with the new state. For example, a property named "openwith a value that gets toggled between true or false could be used to maintain the door's state. 
 + 
 +The new versions of the [[script|scripts]] are very similar to the previous ones except there is now second set of events that can occur to "revert" the door back to a closed state by playing the "close" [[animation]] and changing the [[navigation_and_interaction|navigation paths]] back to their original types.
  
 ====Method 1: Replace Navigation==== ====Method 1: Replace Navigation====
 +The improved version of the [[script]] using the [[Replace Navigation]] method is fairly straightforward. We simply swap the navigation types between "Interact Only" and "Walk and Interact" as the door opens and closes.
 <code lua> <code lua>
 if self.property["open"] == true then if self.property["open"] == true then
Line 76: Line 80:
 end end
 </code> </code>
 +
 +{{:wiki:door_script_example.gif?nolink|}}
  
 ====Method 2: Modify Navigation==== ====Method 2: Modify Navigation====
 +For the improved version using the [[Modify Navigation]] method, let's assume we are using a different type of door that swings open, as shown in the image below. In the [[script]], the navigation between "door_tile" and "tile02" is set to the type of "Interact Only" when the door is open due to the way its model swings inward. This prevents the player from walking through the door when it's open.
 +
 {{:wiki:door_tile_ids_02.png?nolink|}} {{:wiki:door_tile_ids_02.png?nolink|}}
 <code lua> <code lua>
Line 95: Line 103:
 </code> </code>
  
-There are often multiple methods to accomplish the same functionality and there isn't necessarily a right or wrong way. As with many other concepts, the best way to learn is to experiment with different setups and determine what works best for you and the particular game that you're creating.+Scripting [[object|objects]] like doors is a great way to learn more about the RPG in a Box [[script_syntax|scripting language]]. There are often multiple methods to accomplish the same functionality and there isn't necessarily a right or wrong approach. As with many other concepts, the best way to learn is to experiment with different setups and determine what works best for you and the particular game that you're creating. Have fun!
  
door_scripting_tutorial.1509746366.txt.gz · Last modified: 2017/11/03 14:59 by justin