Scripting Healthpacks that drop on death

Have a question about modding, modelling or skinning? Have a tutorial to post? Post here!

Moderator: Core Staff

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Scripting Healthpacks that drop on death

Post by F |Madness| U » July 11th, 2011, 10:52 am

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.
-

Nekoneko
CJ Fan
CJ Fan
Posts: 170
Joined: April 18th, 2011, 3:48 pm

Re: Scripting Healthpacks that drop on death

Post by Nekoneko » July 11th, 2011, 11:40 am

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:
Last edited by Nekoneko on July 11th, 2011, 12:17 pm, edited 6 times in total.

User avatar
iCYsoldier
CJ Worshipper
CJ Worshipper
Posts: 289
Joined: December 5th, 2009, 7:12 am
Location: Australia

Re: Scripting Healthpacks that drop on death

Post by iCYsoldier » July 11th, 2011, 11:59 am

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.

IzNoGoD
CJ Worshipper
CJ Worshipper
Posts: 343
Joined: January 6th, 2009, 8:39 pm
Location: Netherlands/Holland

Re: Scripting Healthpacks that drop on death

Post by IzNoGoD » July 11th, 2011, 12:11 pm

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();
}
LMGTFY!

Its not a glitch... Its the future!

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: Scripting Healthpacks that drop on death

Post by F |Madness| U » July 11th, 2011, 12:12 pm

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 :)
-

Nekoneko
CJ Fan
CJ Fan
Posts: 170
Joined: April 18th, 2011, 3:48 pm

Re: Scripting Healthpacks that drop on death

Post by Nekoneko » July 11th, 2011, 12:24 pm

Iznogod's script is so overkill ><

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: Scripting Healthpacks that drop on death

Post by F |Madness| U » July 11th, 2011, 1:08 pm

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.
-

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: Scripting Healthpacks that drop on death

Post by F |Madness| U » July 11th, 2011, 4:21 pm

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++;
                //}
        }
}
-

IzNoGoD
CJ Worshipper
CJ Worshipper
Posts: 343
Joined: January 6th, 2009, 8:39 pm
Location: Netherlands/Holland

Re: Scripting Healthpacks that drop on death

Post by IzNoGoD » July 11th, 2011, 4:39 pm

Just use my code :)
LMGTFY!

Its not a glitch... Its the future!

Nekoneko
CJ Fan
CJ Fan
Posts: 170
Joined: April 18th, 2011, 3:48 pm

Re: Scripting Healthpacks that drop on death

Post by Nekoneko » July 11th, 2011, 4:42 pm

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;
                }
        }
}

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: Scripting Healthpacks that drop on death

Post by Drofder2004 » July 11th, 2011, 6:44 pm

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:

Code: Select all

for(i=0;i>0;)
'i' EQUAL 0, while 'i' is GREATER THAN 0, loop.
The loop never starts.
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: Scripting Healthpacks that drop on death

Post by F |Madness| U » July 11th, 2011, 7:05 pm

:oops: 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 :oops:
-

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: Scripting Healthpacks that drop on death

Post by Drofder2004 » July 11th, 2011, 8:14 pm

The middle condition is a "while-true" condition.
So while the condition you have coded is true, it will loop.
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: Scripting Healthpacks that drop on death

Post by F |Madness| U » July 11th, 2011, 8:40 pm

I know, I've known that since I started learning scripting. For some reason whilst doing that scripting I thought otherwise though. :?
-

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: Scripting Healthpacks that drop on death

Post by F |Madness| U » July 12th, 2011, 12:34 pm

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.");
}
-

Post Reply

Who is online

Users browsing this forum: No registered users and 43 guests