Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Health Script

Alex Parks
Registered User
Join date: 4 Aug 2004
Posts: 21
05-14-2005 19:22
Ok, so here I am doing one of my first scripts for the Forums, I dont come on here alot but I am bored and my game wont work so I will just do this. If it doesnt work, IM me in game or just post here please and I will be sure to fix it!
////////////////////////////////////////////////////////////////////////////////////////////////////////

//Health Script By: Alex Parks
//First we need to make the health as an integer for the script and what ever the bullet will be parcially named that will hurt it and also a number that will help out with calculating damage.
string DBullet;
integer Health;
integer Damage;
default
{
//When the script begins we need to have to set the health to 100%.
state_entry()
{
//This will set the health all the way to 100% again and start to make the text say the health left.
Health=100;
llSetTimerEvent(.001)
}
timer()
{
llSetText("Health: "+(string)Health,<1,1,1>,1);
}
//this is what will happen after it is hit by any other object.
collision_start(integer num_detected)
{
DBullet=llDetectedName(0);
if(llGetSubString(DBullet, 0, 8)=="DBullet";)
{
if(llGetSubString(DBullet, 8, llStringLength(DBullet))==(integer))
{
Damage=llGetSubString(DBullet, 8, llStringLength(DBullet))
Health=Health - Damage;
if(Health > 0)
{
llSetText("Health: "+(string)Health,<1,1,1>,1);
}
else if(Health < 0)
{
Health=0;
llSetText("Health: "+(string)Health,<1,1,1>,1);
llSay(0, "You have died. Please remove the health pack and put it back on again to continue playing!";);
}
}
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
Just remember I had no chance to test this so it is up to you to fix it, or to contact me because I would be happy to fix it :) !
Alex Parks
Registered User
Join date: 4 Aug 2004
Posts: 21
Ok, I fixed it now and now I am 99% sure it works better now!
05-20-2005 13:29
Ok, here is the fixed code, BUT if you want it to work then you need to use bullets that are called a DBullet then a number. (ex. "DBullet 2" this bullet will do 2 damage on contact)

CODE
//Health Script By: Alex Parks
//First we need to make the health as an integer for the script and what ever the bullet will be parcially named that will hurt it and also a number that will help out with calculating damage.
string DBullet;
integer Health=100;
string Damage;
default
{
//When the script begins we need to have to set the health to 100%.
state_entry()
{
//This will set the health all the way to 100% again and start to make the text say the health left.
Health=100;
llSetTimerEvent(.001);
}
attach(key id)
{
llRequestPermissions(llGetOwner(),PERMISSION_ATTACH);
}
timer()
{
llSetText("Health: "+(string)Health,<1,1,1>,1);
}
//this is what will happen after it is hit by another object.
collision_start(integer num_detected)
{
DBullet=llDetectedName(0);
if(llGetSubString(DBullet, 0, 6)=="DBullet")
{
if(llGetSubString(DBullet, 8, llStringLength(DBullet)))
{
Damage=llGetSubString(DBullet, 8, llStringLength(DBullet));
Health=Health - (integer)Damage;
if(Health > 0)
{
llSetText("Health: "+(string)Health,<1,1,1>,1);
}
else if(Health < 0)
{
Health=0;
llSetText("Health: "+(string)Health,<1,1,1>,1);
llSay(0, "You have died. Please put the health pack back on to continue playing!");
Health=100;
llDetachFromAvatar();
}
}
}
}
}


Well there it is, and hope you enjoy it :D

If you have any questions, suggestions, custom scripts you want made, please post here or Instant Message me in-world as Alex Parks. If I charge there is a good chance it will be pretty cheap, so remember me!
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
05-20-2005 13:54
For one thing, you can highlight your code correctly by using the php tag (in brackets).

CODE

//Health Script By: Alex Parks

//First we need to make the health as an integer for the script and what ever the bullet will be parcially named that will hurt it and also a number that will help out with calculating damage.
string DBullet;
integer Health=100;
string Damage;
default
{
//When the script begins we need to have to set the health to 100%.
state_entry()
{
//This will set the health all the way to 100% again and start to make the text say the health left.
Health=100;
llSetTimerEvent(.001);
}
attach(key id)
{
llRequestPermissions(llGetOwner(),PERMISSION_ATTACH);
}
timer()
{
llSetText("Health: "+(string)Health,<1,1,1>,1);
}
//this is what will happen after it is hit by another object.
collision_start(integer num_detected)
{
DBullet=llDetectedName(0);
if(llGetSubString(DBullet, 0, 6)=="DBullet")
{
if(llGetSubString(DBullet, 8, llStringLength(DBullet)))
{
Damage=llGetSubString(DBullet, 8, llStringLength(DBullet));
Health=Health - (integer)Damage;
if(Health > 0)
{
llSetText("Health: "+(string)Health,<1,1,1>,1);
} else if(Health < 0)
{
Health=0;
llSetText("Health: "+(string)Health,<1,1,1>,1);
llSay(0, "You have died. Please put the health pack back on to continue playing!");
Health=100;
llDetachFromAvatar();
}
}
}
}
}


...There's a nicely formatted version of it.

And another thing, setting a timer event of .001 second is ridiculous. It is completely uneccesary to update your hovertext more than once a second. Hovertext does not disappear once every few milliseconds. Indeed, from what the code displays, it updates the hovertext whenever it would change in the first place.

So, you should really take out the entire timer, if you don't want the lag police after you. Also, consider a function updatehovertext() that would update the hovertext.

Try those changes...

CODE

//Health Script By: Alex Parks
//Fixed by: Keknehv Psaltery

//First we need to make the health as an integer for the script and what ever the bullet will be parcially named that will hurt it and also a number that will help out with calculating damage.

string DBullet;
integer health=100;
string damage;

updatehovertext()
{
llSetText("Health: "+(string)Health,<1,1,1>,1);
}

default
{
//When the script begins we need to have to set the health to 100%.
state_entry()
{
//This will set the health all the way to 100% again and start to make the text say the health left.
health=100;
updatehovertext();
}
attach(key id)
{
llRequestPermissions(llGetOwner(),PERMISSION_ATTACH);
}
//this is what will happen after it is hit by another object.
collision_start(integer num_detected)
{
DBullet=llDetectedName(0);
if(llGetSubString(DBullet, 0, 6)=="DBullet")
{
if(llGetSubString(DBullet, 8, llStringLength(DBullet)))
{
damage=llGetSubString(DBullet, 8, llStringLength(DBullet));
health -= (integer)Damage;
if( health > 0)
{
updatehovertext();
} else if( health < 1 ) //Cover when health is exactly zero as well
{
health = 0;
updatehovertext();
llSay(0, "You have died. Please put the health pack back on to continue playing!");
health = 100;
llDetachFromAvatar();
}
}
}
}
}


There's a nice and readable version...
Synergy Belvedere
Prim Reaper
Join date: 7 Jul 2004
Posts: 253
05-20-2005 14:38
.001 !!! Omg

Just crash the sim while you're at it :P
_____________________
----------------------------------------------------------
--The mind's eye is limited only by its focus--
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
05-20-2005 16:30
Actually, I think what would happen would be that you would get a huge list of event calls to process, so, in effect, he has an infinite loop, that ALSO uses a timer :D
Ace Cassidy
Resident Bohemian
Join date: 5 Apr 2004
Posts: 1,228
05-20-2005 16:44
From: Alex Parks

llSetTimerEvent(.001)


NOT KEWL!!!

- Ace
_____________________
"Free your mind, and your ass will follow" - George Clinton
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
05-20-2005 17:18
It's moot, remember that the shortest time between events being called is 0.05 seconds. You can *never* process more then 20 events a second. Not that you will ever achive this, as executing bytecode takes time.

When writing combat systems, if you want them to run fast you need to reduce the amount of code.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey