User Tools

Site Tools


hpl2:tutorials:script:disable_gravity_tutorial

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Last revision Both sides next revision
hpl2:tutorials:script:disable_gravity_tutorial [2010/11/04 15:05]
jens
hpl2:tutorials:script:disable_gravity_tutorial [2011/01/11 16:28]
frontcannon [The scripting]
Line 40: Line 40:
  
 ===== The scripting ===== ===== The scripting =====
 +
 +
 Now we'll do the actual scripting! The floating effect is based on timer functions, and one of these we'll add in //​OnStart()//​ so it is immediately called when the level loads! The syntax for timer function looks like this: Now we'll do the actual scripting! The floating effect is based on timer functions, and one of these we'll add in //​OnStart()//​ so it is immediately called when the level loads! The syntax for timer function looks like this:
  
-<code c> + 
-//Function syntax: Func(string &in asTimer)+<code c>//​Function syntax: Func(string &in asTimer)
 void  AddTimer(string&​ asName, float afTime, string& asFunction);​ void  AddTimer(string&​ asName, float afTime, string& asFunction);​
 </​code>​ </​code>​
 +
  
 The first string //asName// is the name of our timer, //afTime// is the time when the timer will call the function in //​asFunction//​ after the timer is executed. Adding our timer in //​OnStart()//​ will make our code look like this: The first string //asName// is the name of our timer, //afTime// is the time when the timer will call the function in //​asFunction//​ after the timer is executed. Adding our timer in //​OnStart()//​ will make our code look like this:
  
-<code c> + 
-////////////////////////////​+<code c>////////////////////////////​
 // Run first time starting map // Run first time starting map
 void OnStart() void OnStart()
 { {
     GiveItemFromFile("​lantern",​ "​lantern.ent"​);​     GiveItemFromFile("​lantern",​ "​lantern.ent"​);​
-    for(int i=0;​i<​10;​i++) GiveItemFromFile("​tinderbox_"​+i,​ "​tinderbox.ent"​); ​  +    for(int i=0;i< 10;i++) GiveItemFromFile("​tinderbox_"​+i,​ "​tinderbox.ent"​); ​  
-    + 
     AddTimer("​FloatTime",​ 0.01, "​FloatObjects"​);​     AddTimer("​FloatTime",​ 0.01, "​FloatObjects"​);​
 } }
 </​code>​ </​code>​
 +
  
 As you can see, **FloatTime** is the name of our timer which calls the function **FloatObjects** after a very short time, exactly 0.01 seconds. The purpose of this will be explained after we created **FloatObjects**! As you can see, **FloatTime** is the name of our timer which calls the function **FloatObjects** after a very short time, exactly 0.01 seconds. The purpose of this will be explained after we created **FloatObjects**!
 +
  
 For actually making the objects float, we'll add this function in **FloatObjects**:​ For actually making the objects float, we'll add this function in **FloatObjects**:​
 +
  
 <code c> <code c>
Line 69: Line 75:
 </​code>​ </​code>​
  
-**AddPropImpulse** gives a specified entity an impulse, which means pushing it in a certain way with certain force. //asName// is the name of the entitity, The floats //afX//, //afY// and //afZ// are the direction coordinates the push has to follow and //​asCoordSystem//​.. well I don't know what that does, but it doesn'​t need any input in our case.\\ 
-**AddPropImpulse** has to be created for **every** object you want to float, which makes three of them in my case! The last thing we add in **FloatObjects** is a timer function that practically calls itself so we can create a loop that will continually execute **FloatObjects** and give our entities a little push every 0.01 seconds! Our function should look like this: 
  
-<code c> +**AddPropImpulse** gives a specified entity an impulse, which means pushing it in a certain way with certain force. //asName// is the name of the entitity, The floats //afX//, //afY// and //afZ// are the direction coordinates the push has to follow and //​asCoordSystem // specifies the coordinate system the game refers to. You can leave it empty, but valid strings would be "​World"​ for using the XYZ planes you can see in the editor or "​local"​ if it should use the XYZ axis of the object you are adressing.\\ ​ **AddPropImpulse** has to be created for **every** object you want to float, which makes three of them in my case! The last thing we add in **FloatObjects** is a timer function that practically calls itself so we can create a loop that will continually execute **FloatObjects** and give our entities a little push every 0.01 seconds! Our function should look like this: 
-void FloatObjects(string &in asTimer)+ 
 + 
 +<code c>void FloatObjects(string &in asTimer)
 { {
     AddPropImpulse("​human_skull_1",​ 0, 0.15, 0, ""​);​     AddPropImpulse("​human_skull_1",​ 0, 0.15, 0, ""​);​
Line 81: Line 87:
 } }
 </​code>​ </​code>​
 +
  
 So now I've added an impulse for every object I want to float (the skull and the two books) and a timer that calls itself, repeating **FloatObjects** over and over. So now I've added an impulse for every object I want to float (the skull and the two books) and a timer that calls itself, repeating **FloatObjects** over and over.
 +
  
 You may have noticed that I've only put in a value for the //afY// float. That means the impulse only goes in the Y direction or UP! 0.15 is a very weak impulse, but it is applied every 0.01 seconds so all the little pushes can't be noticed by the player which creates the effect of the objects floating or falling very slowly. You can now save the .hps file and load your level. Have fun and thanks for reading! You may have noticed that I've only put in a value for the //afY// float. That means the impulse only goes in the Y direction or UP! 0.15 is a very weak impulse, but it is applied every 0.01 seconds so all the little pushes can't be noticed by the player which creates the effect of the objects floating or falling very slowly. You can now save the .hps file and load your level. Have fun and thanks for reading!
hpl2/tutorials/script/disable_gravity_tutorial.txt · Last modified: 2011/02/15 21:17 by l1ght5h0w