touch function
Moderator: InsideQC Admins
5 posts
• Page 1 of 1
touch function
hey sniperz227 again. i was wondering how the touch function worked? i was writing a weapon pick up code and well this is what i have:
i called the entity in my map wep_l96a1.mdl and set it model and to my progs/l96a1.mdl( what i do when i load models into maps and it works) and sets the weapon anyways the second part im not sure about thats why i asked. what im saying is if the player touches the entity then your weapon becomes the l96a1.
- Code: Select all
/*
========================================================================================
========Weapon Pick Up Code=============================================================
========================================================================================
*/
/*
=====
L96A1
=====
*/
void() wep_l96a1 =
{
precache_model (self.model);
setmodel (self, self.model);
self.skin = 0;
};
void() pick_l96a1 =
{
if(self.touch == wep_l96a1.mdl)
{
self.weapon == IT_L96A1;
}
pick_l96a1();
}
i called the entity in my map wep_l96a1.mdl and set it model and to my progs/l96a1.mdl( what i do when i load models into maps and it works) and sets the weapon anyways the second part im not sure about thats why i asked. what im saying is if the player touches the entity then your weapon becomes the l96a1.
- sniperz227
- Posts: 112
- Joined: Sat Apr 09, 2011 3:19 am
Re: touch function
In a nutshell: when entities collide (and both are using something different from SOLID_NOT), the engine sets self as the touched entity, other as the touching entity, and calls the .touch() function associated to the touched (self) entity (if not .touch() function is defined then nothing happens).
So, I suggest you to compare your code against the original weapon/item touch code and you'll likely be able to fix it by yourself.
So, I suggest you to compare your code against the original weapon/item touch code and you'll likely be able to fix it by yourself.
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC
(LordHavoc)
-

frag.machine - Posts: 2090
- Joined: Sat Nov 25, 2006 1:49 pm
Re: touch function
hmmm i still dont get it i tried a bunch of stuff but it still didnt work can you show me an example of what your trying to say?
- sniperz227
- Posts: 112
- Joined: Sat Apr 09, 2011 3:19 am
Re: touch function
// The QuakeC Touch Function concept:
// somewhere in the quakec....
local entity foo; // we tell the quakec that this function will use a variable called foo which is local and is of type 'entity '
foo = spawn(); // foo is initiated/created/allocated memory/it has 'joined the game'
foo.solid = SOLID_BBOX; // foo is solid (there are other versions of solid)
foo.touch = TheFooTouchFunction; // this assigns a function to be run when foo is touched
.
.
};
// elsewhere...
// this is the function we want to run when when something touches foo
void () TheFooTouchFunction =
{
// 'self' is foo
// 'other' is the entity that touched foo
};
// somewhere in the quakec....
local entity foo; // we tell the quakec that this function will use a variable called foo which is local and is of type 'entity '
foo = spawn(); // foo is initiated/created/allocated memory/it has 'joined the game'
foo.solid = SOLID_BBOX; // foo is solid (there are other versions of solid)
foo.touch = TheFooTouchFunction; // this assigns a function to be run when foo is touched
.
.
};
// elsewhere...
// this is the function we want to run when when something touches foo
void () TheFooTouchFunction =
{
// 'self' is foo
// 'other' is the entity that touched foo
};
Last edited by OneManClan on Mon Dec 12, 2011 7:31 am, edited 1 time in total.
- OneManClan
- Posts: 243
- Joined: Sat Feb 28, 2009 2:38 pm
Re: touch function
self.touch = whatever;
when the entity is touched by anything it will call upon the function whatever();
inside whatever(); you may specify what is allowed to trigger it, such as:
if (other.classname != "player")
return;
So only the player can activate the entity, this is useful for weapons and ammo so monsters or anything else dont pick them up. Back in an old mod i used to work on (iHalo) i forgot those 2 simple little lines for the grenade pickups, so anything could pick up the grenades, i could throw my grenades and the grenades would pick up the grenades lol.
now notice the "other.classname" other is what touched the entity to call the self.touch function, aka "whatever();" in here.
But anything inside the "whatever();" is being called by entity touched, such as self.think = SUB_Remove, that will remove the entity once the touch function has been called.
If you want to do something or give something to the entity or player that did the touching, you use other. other.currentammo = other.currentammo + 25 for example, this will give the player or activator 25 ammo.
put it all together you get something like this:
when the entity is touched by anything it will call upon the function whatever();
inside whatever(); you may specify what is allowed to trigger it, such as:
if (other.classname != "player")
return;
So only the player can activate the entity, this is useful for weapons and ammo so monsters or anything else dont pick them up. Back in an old mod i used to work on (iHalo) i forgot those 2 simple little lines for the grenade pickups, so anything could pick up the grenades, i could throw my grenades and the grenades would pick up the grenades lol.
now notice the "other.classname" other is what touched the entity to call the self.touch function, aka "whatever();" in here.
But anything inside the "whatever();" is being called by entity touched, such as self.think = SUB_Remove, that will remove the entity once the touch function has been called.
If you want to do something or give something to the entity or player that did the touching, you use other. other.currentammo = other.currentammo + 25 for example, this will give the player or activator 25 ammo.
put it all together you get something like this:
- Code: Select all
void() item_touch =
{
if(other.classname != "player")
return; //Player's Only Club, No monsters allowed
if (self.classname == "Derp Ammo") //This is where the classname comes in, so all the items
{ //Can share the same touch function, just add another if statement
if (other.ammo_derp >= 10) //10 Derps is enough, don't pick it up if you have 10
return;
other.ammo_derp = other.ammo_derp+ 1; // Plus 1 derp
sprint(other, "You picked up "); //Prints what "other" got, in this cause "other" would be the player
sprint(other, "some Derp ammo\n"); // \n is a new line in case of multiple pickups,
//in this case "You picked up" and "some derp ammo" would be on the same line because
// there is no \n after the "you picked up"
} //stuff past this closing bracket occurs univerally within the touch function, not just for "Derp Ammo"
stuffcmd (other, "bf\n"); //screen flash (optional) (bright flash?)
self.model = string_null; //Lets not get rid of it so we can respawn it if we want
self.solid = SOLID_NOT;
if (deathmatch == 1) // If its a deathmatch
self.nextthink = time + 30; // In 30 seconds...
self.think = SUB_regen; //Respawn
SUB_UseTargets(); //If the entity (in this case item) had any targets that the mapper wanted
//it to activate, do it now
}
void() PlaceItem = //**Copied from Quake QC** -- Ghost Fang
{
local float oldz;
self.mdl = self.model; // so it can be restored on respawn
self.flags = FL_ITEM; // make extra wide
self.solid = SOLID_TRIGGER;
self.movetype = MOVETYPE_TOSS;
self.velocity = '0 0 0';
self.origin_z = self.origin_z + 6;
oldz = self.origin_z;
if (!droptofloor(0,0))
{
dprint ("Bonus item fell out of level at ");
dprint (vtos(self.origin));
dprint ("\n");
remove(self);
return;
}
};
void() StartItem =
{
self.nextthink = time + 0.2; // items start after other solids
self.think = PlaceItem; //Actually Spawn now
}; // **End Copy** --Ghost Fang
void() item_ammo =
{
self.classname = "Derp Ammo"; //Give it a classname
self.touch = item_touch; //What happens when touched
self.solid = SOLID_TRIGGER; //We want to walk through it but activate it
precache_model("progs/ammo.mdl"); //Pre-cache the model we're using
setmodel(self, "progs/ammo.mdl.mdl"); //Set the model
StartItem(); //Spawn function
};
- Ghost_Fang
- Posts: 336
- Joined: Thu Nov 12, 2009 4:37 am
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest