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
Next revision
Previous revision
hpl2:tutorials:script:disable_gravity_tutorial [2010/11/04 15:05]
jens
hpl2:tutorials:script:disable_gravity_tutorial [2011/02/15 21:17] (current)
l1ght5h0w
Line 1: Line 1:
-====== Beginner Scripting Tutorial 1======+====== Beginner Scripting Tutorial 1 ======
  
-//This is an easy tutorial for beginners. It is not intended to teach you new techniques but more to show you all the neat stuff you can do with the functions and the HPL2 engine.// 
  
-In this tutorial you'​ll ​learn how to make entities '​float'​ or rather make them seem to be very very light. +//This is an easy tutorial for beginners. It is not intended to teach you new techniques but more to show you all the neat stuff you can do with the functions and the HPL2 engine.//  
-Possible uses could be for some strange sanity effects or even simulating an underwater environment!+ 
 + 
 +In this tutorial you will learn how to make entities '​float'​ or rather make them seem to be very very light. Possible uses could be for some strange sanity effects or even simulating an underwater environment! 
  
 ===== Getting started ===== ===== Getting started =====
-Start by creating a new level in the level editor and then saving the map. There are no real limits, but keep it simple and include some movable entities you can use the script on. In my little room it'​s ​the skull and two books. You should include another entity you don'​t ​use the effect on, so you can compare their characteristics!+ 
 + 
 +Start by creating a new level in the level editor and then saving the map. There are no real limits, but keep it simple and include some movable entities you can use the script on. In my little room they are the skull and two books. You should include another entity you do not use the effect on, so you can compare their characteristics! 
  
 My room looks like this: My room looks like this:
  
-[[http://​imgur.com/​OiFEr.png|Link]] 
  
-After you have saved your map, create a .hps file with the same name. This is where we'​ll ​do all the scripting, add the default script there. I have removed the if-clause in my example, it'​s ​not needed.+[[http://​imgur.com/​OiFEr.png|Link]]  
 + 
 + 
 +After you have saved your map, create a .hps file with the same name. This is where we will do all the scripting, add the default script there. I have removed the if-clause in my example, it is not needed. 
  
 <code c>////////////////////////////​ <code c>////////////////////////////​
Line 21: Line 29:
    ​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"​); ​   ​
 } }
    
Line 38: Line 46:
 } }
 </​code>​ </​code>​
 +
  
 ===== 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: 
  
-<code c> + 
-//Function syntax: Func(string &in asTimer)+Now we will do the actual scripting! The floating effect is based on timer functions, and one of these we will 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)
 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 will add this function in **FloatObjects**:​ 
  
 <code c> <code c>
Line 69: Line 84:
 </​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 82: Line 97:
 </​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. 
  
-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!+So now I have 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 have 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 ​ca not 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! 
  
 ===== Additional information ===== ===== Additional information =====
-Here you can put the little things you've noticed while playing around and testing my script. Stuff I've already found out: 
  
-  ​* Different entities need different //afY// values! For small objects like books 0.15 is enough, but everything bigger than 2 will make them float upwards like a helium balloon. + 
-  * Play around with //afTime//! Bigger values like 0.5 combined with larger impulses like 5 can create some crazy bouncing effect!+Here you can put the little things you have noticed while playing around and testing my script. Stuff I have already found out: 
 + 
 + 
 +    ​* Different entities need different //​afY// ​ values! For small objects like books 0.15 is enough, but everything bigger than 2 will make them float upwards like a helium balloon. 
 + 
 + 
 +    ​* Play around with //afTime//! Bigger values like 0.5 combined with larger impulses like 5 can create some crazy bouncing effect!
  
hpl2/tutorials/script/disable_gravity_tutorial.1288883125.txt.gz · Last modified: 2010/11/04 15:05 by jens