Page 1 of 3
Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 10:52 am
by F |Madness| U
Okay I've been trying to script some healthpacks that do the following. When somebody dies, a healthpack is dropped. It will stay on the ground until somebody touches it, or until 30 seconds has passed, then the healthpack will disappear. I've tried scripting it, and kind of did it right (I think) however I need help with `linking` the 2 functions.
Code: Select all
dropHealth()
{
    self waittill("death");
   Â
    healthpackorigin = self.origin + (0, 0, 30);
    healthpack = spawn( "script_model", healthpackorigin );
    healthpack thread do_healthtrigger();
    healthpack setModel("p_lights_cagelight01_red_on");//<-- red health
    wait 30;
    if(isdefined(healthpack))
    {
        healthpack delete();      //-- waits 30 seconds, if nobody has touched the health (healthpack still exists) then it is deleted
        healthtrigger delete();
    }
       Â
}
Â
do_healthtrigger()
{
    triggerorigin = healthpackorigin - (0, 0, 25);
    healthtrigger = spawn("trigger_radius", triggerorigin, 0, 30, 50);
Â
    for(i=0; i>0;)
    {
        healthtrigger waittill("trigger", player);
Â
        if(player.team == "allies" && player.health < 100 && isdefined(healthpack))
        {
            player thread givehealth();
                                       Â
            healthpack delete();
            healthtrigger delete();
            i++;
//-- when player touches trigger, checks to see if 30 secs has been (If it has, healthpack won't be defined because it will have been deleted)
        }   Â
    }
}
The problem I'm having is that the variables are seen as undefined when used in each function (for example healthpackorigin in the first function is undefined in the second function). Basically I need to know how these 2 functions can share these variables. At first I tryed using self.variable, however this caused problems because the person who died to drop the healthpack (self) could drop mutiple healthpacks over time, and caused many errors. Any help would be great.
Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 11:40 am
by Nekoneko
You can easily get it's origin in the second script with self.origin, if thats what you are trying to do.
Edit:
Some tips:
make it an infinite loop, so that after each death a pack will spawn, maybe add a wait between 2 packs.
Assign any variables or entities to the healthpack (for example healthpack.trigger )
Have the wait 30; in another thread, so that more than one at a time can be spawned (not sure if you want this though)
Have the threads end in some notify, in case it somehow gets called up twice.
If you want to spoil the fun of scripting, I wrote something quick (might not work, haven't tested)
Code: Select all
dropHealth()
{
    self endon("disconnect");
   Â
    while(true)
    {
        self waittill("death");
   Â
        healthpackorigin = self.origin + (0, 0, 30);
        triggerorigin = healthpackorigin - (0, 0, 25);
   Â
        healthpack = spawn( "script_model", healthpackorigin );
        healthpack setModel("p_lights_cagelight01_red_on");       //<-- red health
   Â
        healthpack.trigger = spawn("trigger_radius", triggerorigin, 0, 30, 50);
   Â
        healthpack thread health_trigger_wait();
        healthpack thread health_time_wait();
healthpack waittill("delete_healthpack"); //thread this if you want more than one healthpack
healthpack.trigger delete();
    healthpack delete();
    }
}
Â
health_trigger_wait()
{
  self endon("delete_healthpack");
   Â
  for(;;)
  {
    self.trigger waittill("trigger", player);
       Â
    if(player.team == "allies" && player.health < 100 )
    {
      player thread givehealth();
      self notify("delete_healthpack");
    }   Â
  }
}
Â
health_ttime_wait()
{
    self endon("delete_healthpack");
    wait 30;
    self notify("delete_healthpack");
}
Edit again:
Maybe the notify wasn't a too good idea, since i could imagine after self delete(); it might not get notified anymore ><;
changed again.. should work now.. fail for 6 times edit D:
Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 11:59 am
by iCYsoldier
Code: Select all
dropHealth()
{
  self waittill("death");
Â
  healthpackorigin = self.origin + (0, 0, 30);
  healthpack = spawn( "script_model", healthpackorigin );
Â
  // Moved these two lines from the other func to this one
  triggerorigin = healthpackorigin - (0, 0, 25);
  healthtrigger = spawn("trigger_radius", triggerorigin, 0, 30, 50);
Â
  // Passed the trigger as an argument, allowing you to use it in the next function aswell
  healthpack thread do_healthtrigger(healthtrigger);
Â
  healthpack setModel("p_lights_cagelight01_red_on"); //<-- red health
  wait 30;
  if(isdefined(healthpack))
  {
     healthpack delete(); //-- waits 30 seconds, if nobody has touched the health (healthpack still exists) then it is deleted
    healthtrigger delete();
  }
}
Â
// Because you called this function on the healthpack, you can refer to it as 'self'
do_healthtrigger(healthtrigger)
{
  for(i=0;i>0;)
  {
    healthtrigger waittill("trigger", player);
Â
    if(player.team == "allies" && player.health < 100 && isdefined(self))
    {
      player thread givehealth();
Â
      self delete();
      healthtrigger delete();
      i++;
    }   Â
  }
}
I've made a few changes, which I've commented. I've simply declared the trigger in the top function and passed it into the next function as an argument. Also, since you're calling the 'do_healthtrigger' function on the healthpack, you can refer to it as 'self' in the function.
Also, I'm curious as to why you've included that for loop. I assumed you want it to run once, so there would be no need to have it there.
Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 12:11 pm
by IzNoGoD
Code: Select all
drophealth()
{
dropspeed=200;
self endon("disconnect");
self endon("spawned_player"); //added this to prevent duplicate threads
self waittill("death");
start=self.origin+(0,0,30);
trace=bullettrace(self.origin+(0,0,30),self.origin-(0,0,10000),false,undefined);
end=trace["position"];
if(trace["fraction"]==1)
return; //no ground found
healthpack=spawn("script_model",start);
healthpack setmodel("p_lights_cagelight01_red_on");
movetime=distance(start,end)/dropspeed;
healthpack moveto(end,movetime,movetime/3,movetime/3);
healthpack thread healthtrigger(100,50,30);
}
healthtrigger(amount,maxdist,time)
{
picked=false;
timer=0;
while(!picked&&timer<time)
{
players=getentarray("player","classname");
for(i=0;i<players.size;i++)
{
if(isdefined(players[i].sessionstate)&&players[i].sessionstate=="playing"&&players[i].health<players[i].maxhealth&&distancesquared(self.origin,players[i].origin)<maxdist*maxdist)
{
trace=bullettrace(players[i].origin+(0,0,30),self.origin+(0,0,20),false,undefined);
if(trace["fraction"]==1)
{
players[i].health+=amount;
if(players[i].health>players[i].maxhealth)
players[i].health=players[i].maxhealth;
picked=true;
break;
}
}
}
timer+=0.05;
wait 0.05;
}
self delete();
}
Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 12:12 pm
by F |Madness| U
I put the for loop there because otherwise if a player triggered it but the player was on axis (not allies) I wouldn't want the health to be destroyed. I was pretty tired writing it maybe it wasn't necessary, anyway I'm going to try and do it myself, if worst comes to worse I'll have to try parts of what you have supplied me

Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 12:24 pm
by Nekoneko
Iznogod's script is so overkill ><
Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 1:08 pm
by F |Madness| U
Haha, I'm looking through it now trying to understand it. I tested some script, however it seems that the trigger is not working at all, I even took out all the arguments and it appears that I can't even `activate` the trigger. Going to look into it more soon.
Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 4:21 pm
by F |Madness| U
Hmm for some reason my triggers can't be activated, I'm not sure why. (Even took out the args for them).
Code: Select all
dropHealth()
{
self waittill("death");
phealthpackorigin = self.origin + (0, 0, 30);
if((self == level.zombie1 || self == level.zombie2) && !level.inlastman)
{
//self thread dropMine();
}
else if(!level.inlastman)
{
phealthpack = spawn( "script_model", phealthpackorigin );
phtriggerorigin = phealthpackorigin - (0, 0, 25);
phtrigger = spawn("trigger_radius", phtriggerorigin, 0, 30, 50);
phealthpack setModel("p_lights_cagelight01_red_on");//<-- red health
phealthpack rotateYaw(21600, 30);
phealthpack thread do_healthtrigger(phtrigger);
wait 30;
if(isdefined(phealthpack))
{
phealthpack delete();
phtrigger delete();
}
}
}
do_healthtrigger(phtrigger)
{
for(i=0;i>0;)
{
phtrigger waittill("trigger", player);
//if(player.team == "allies" && player.health < 100 && isdefined(self))
//{
player thread givehealth();
self delete();
phtrigger delete();
i++;
//}
}
}
Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 4:39 pm
by IzNoGoD
Just use my code

Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 4:42 pm
by Nekoneko
He's here to learn scripting xD
And btw, may I ask what you are planning on making.
Every day I see another thread asking for the weirdest things (not that i dislike them ^^)
Will this be some big mod, or are you just scripting for fun?
Edit:
I think the problem lies withing your weird for loop.
It's strange and hard to read..
you define i as 0 and it checks if i (0) is smaller than 0,
I would also make it a while loop (even easier to read)
Code: Select all
do_healthtrigger(phtrigger)
{
    picked_up = false
    while(!picked_up)
    {
        phtrigger waittill("trigger", player);
       Â
        if(.......)
{
       Â
        player thread givehealth();
           Â
        self delete();
        phtrigger delete();
Â
        picked_up = true;
}
    }
}
Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 6:44 pm
by Drofder2004
IzNoGoD wrote:Just use my code

I think you forget people are trying to learn code and your method of scripting is terrible for learning. We are not coding for smallsized applications, we do not need to worry about saving a few bytes of space by removing any space characters.
Code: Select all
trace=bullettrace(self.origin+(0,0,30),self.origin-(0,0,10000),false,undefined);
/* OR */
trace = bullettrace( (self.origin + (0,0,30)), (self.origin - (0,0,10000)), false, undefined);
You may be good at coding, but your techniques are undesirable in practice.
---
This is your fault:
'i' EQUAL 0, while 'i' is GREATER THAN 0, loop.
The loop never starts.
Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 7:05 pm
by F |Madness| U

FFFFFFFFFFFFFFFFFFFUUUUUUUUUUUUUUUU
I was thinking that the middle argument was where the loop STOPPED working >.< Either way it was still a retarded way of scripting a single loop, but thanks for all replies!
And to whoever asked, I'm trying to recreate zom_db (personal favourite mod from CoD4, made by great modder NovemberDobby) on Black Ops. Partially because I want to actually see a decent mod on Black Ops, and I've began to take an interest into scripting so I figured this would give me an all-round experience. I don't know wether I'll actually release the mod, as it will likely be nowhere near as good as NovemberDobby's release on CoD4, and I wouldn't want to give his mod a bad rep

Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 8:14 pm
by Drofder2004
The middle condition is a "while-true" condition.
So while the condition you have coded is true, it will loop.
Re: Scripting Healthpacks that drop on death
Posted: July 11th, 2011, 8:40 pm
by F |Madness| U
I know, I've known that since I started learning scripting. For some reason whilst doing that scripting I thought otherwise though.

Re: Scripting Healthpacks that drop on death
Posted: July 12th, 2011, 12:34 pm
by F |Madness| U
Okay, for some reason my trigger is being set off TWICE everytime I walk on it once. Eg if I have 40 health, I walk over the healthpack. It will print "you picked up 20 health" twice, and heal me 40 health. I don't see how the trigger is being set-off by the same person twice though.
Code: Select all
dropHealth()
{
self waittill("death");
phealthpackorigin = self.origin + (0, 0, 30);
if((self == level.zombie1 || self == level.zombie2) && !level.inlastman)
{
//self thread dropMine();
}
else if(!level.inlastman)
{
phealthpack = spawn( "script_model", phealthpackorigin );
phtriggerorigin = phealthpackorigin - (0, 0, 25);
phtrigger = spawn("trigger_radius", phtriggerorigin, 0, 30, 50);
phealthpack setModel("p_lights_cagelight01_red_on");//<-- red health
phealthpack rotateYaw(21600, 30);
phealthpack thread do_healthtrigger(phtrigger);
wait 30;
if(isdefined(phealthpack))
{
phealthpack delete();
phtrigger delete();
}
}
}
do_healthtrigger(phtrigger)
{
self endon("endloop1");
phgone = 0;
while(1)
{
phtrigger waittill("trigger", player);
if(player.team == "allies" && player.health < 100 && isdefined(self))
{
player thread givehealth();
self delete();
phtrigger delete();
self notify("endloop1");
wait 1;
}
}
}
givehealth()
{
self.oldhealth = self.health;
self.newhealth = self.oldhealth + 20;
self.health = self.newhealth;
self iprintln("^2You picked up "+(self.health - self.oldhealth)+" health.");
}