Plastic Gem #24: Energy Scripting
by Paul Dana · 07/11/2008 (9:51 pm) · 1 comments

Plastic Gem # 24 : Energy Scripting
Difficulty: Easy
Before you follow this gem you must follow the instructions in Gem #22 to get the jetting ability and Gem #23 to learn how to put a Energy Bar GUI on the play HUD. Jetting means ability to fly around using right mouse to activate a jet that pushes you up into the air. If you are using TGE Advanced then you don't need to do anything as jetting comes as part of the demo.
Recharge Rate
Once you follow Gem #22 and Gem #23 you will be able to use the right mouse to jet around and an Energy Bar GUI on your HUD will show you using energy when you jet, but also will show the energy recharge automatically. We can control the recharge rate by setting a variable in the player datablock.
Edit the file ~/server/scripts/player.cs and look for a datablock of type PlayerData called PlayerBody. Find the field called rechargeRate and change the value to half of whatever it is. For example I changed this:
rechargeRate = 0.256;
...to read like this instead:
rechargeRate = 0.128;
Save the file and run the game again and notice that your energy only recharges at half the rate it did before.
Max Energy
The maximum amount of energy is controlled by another field in the same datablock. This one is called maxEnergy. Mine is set to 60 but you can set it to 100 to get a more sensible range of values....
Setting Energy Directly
It is also possible to set the current energy level directly on a player object. Of course first you need to have a player object to alter. Here is a clever way to get the current player object in a single player game (this does not work in a multiplayer game unless you are hosting the game). Use the tilde key (~) to get the console and type:
$P = LocalClientConnection.player;
Now the global variable $P will contain the current player object. We can set our energy level to zero and watch the effect on the Energy Bar GUI we added. At the console, type this:
$P.setEnergyLevel(0);
You should see the energy bar dip down to zero before it starts climbing up again.
Next week
That's it! Next week we will start working on another example shape that is even more elaborate than the Fan we have already described. This is a "zapper" object. We will show how to program it to act as a refueling station for filling up on energy. Have a good weekend!
Sailendu Behera