Self? Other? Owner?

Discuss programming in the QuakeC language.
Post Reply
toneddu2000
Posts: 1395
Joined: Tue Feb 24, 2009 4:39 pm
Location: Italy

Self? Other? Owner?

Post by toneddu2000 »

straight to the point: I'm totally incapable of learning quakec.
It has been about 2/3 months I'm seriously digging on other peoples code and something from scratch I made but I still have a lot of doubts on basic concepts of quakec

What is self? When use other? When use owner?
I made an example. If I want to make a monster I wrote

Code: Select all

void() mymonster = {
  self.classname = "mymonsterclassname"; //so self is the monster, no problem
  setmodel (self,"monsters/mymonster.iqm"); //no problem either. self is the entity "subject" of the model initialization
//and so on....
}
Very simple. But how can I use other?
on the quakec specification I found this:
6.2 Definition of entity fields
These are the fields that are available in the entity objects (like self, other). Beware that this is not true object oriented programming: there is no protection when accessing those fields, and no guaranty on the validity of values. So if you put garbage there you will probably crash the game.
So, if I understood well, self,other and owner are the "pillars" of quakec entity programming.
There's anyone who can tell me if there's a thorough guide on how to use the entity fields? Because I can't tell quakec to "link" 2 entities to each others, for example to simulate a dog follows its owner, or two distint objects that fall together, etc.

Thanks in advance guys
Meadow Fun!! - my first commercial game, made with FTEQW game engine
taniwha
Posts: 401
Joined: Thu Jan 14, 2010 7:11 am
Contact:

Re: Self? Other? Owner?

Post by taniwha »

To be asking questions like this, you actually do get the concepts of quakec :).

Anyway:
self: the currently active entity. for think functions, it's the entity doing the thinking. in touch functions, it's the entity being touched (eg, doors). Set by the engine.
other: the other entity in touch functions. ie, the entity doing the touching (eg, the player). Set by the engine.
owner: the entity that "owns" the entity in question. Used mostly for missiles so grenades/rockets won't explode on the firing player, but also for paired doors (doorent.owner is the "master" door). Set by QC.

For self and other, there are times where QC sets the field, but these are rare, and the field is usually restored to its previous contents after doing something fancy.
Leave others their otherness.
http://quakeforge.net/
Seven
Posts: 301
Joined: Sat Oct 06, 2007 8:49 pm
Location: Germany

Re: Self? Other? Owner?

Post by Seven »

Hello toneddu2000,

For me, it helped a lot to read and understand the items.qc.
It helped me to proper understand what is "self" and "other".

Items.qc handles the powerups and keys and stuff.
So, all the models, that do nothing, but are touchable (!).
The players or monsters, who walk around and touch these models are always: "other" in the viewpoint of the powerups.

Example:
The health boxes functions have a lot of "other." in them, because they give the others health points, right ?
They do not give the health points them'selves'.
The "other" is the player who touch them.

One more important thing:
Look into Throwgib function (in player.qc) for example.
You can spawn new local entities and give them ANY 'name.' you want.
Example in throwgib: 'new.'
So all declarations start with 'new.' then.
But as soon as you jump into the next function that handles these entities,
you must use 'self.' for them (if it is not a global).
If you make these entities touchable, the player/monster that touches these passive ents are the 'other.' again (like described above).

I put my mind "into" the entities view to better understand who is self and who is the other.
That helps me to understand it better.
All powerups/keys and stuff in items.qc will help you to get a "feeling" for it.

Or another example are the projectiles (like rocket, grenade, ...) :
They give the 'other.' the damage (they do not give it them'self' (!)).
The other is the monster/player who gets touched...
In their function, they use:
if (other.health)
{
.....
}
So only 'others' with health (like players, monsters) get the damage.


... I guess what I just wrote is kind of a mess (grammar-wise and context-wise), but maybe this helps you bit.

Best wishes,
Seven
toneddu2000
Posts: 1395
Joined: Tue Feb 24, 2009 4:39 pm
Location: Italy

Re: Self? Other? Owner?

Post by toneddu2000 »

Thanks a lot taniwha and Seven!
I'm now starting to undestand the "passive" concept of "other" entities!

But what about oldself? I saw a lot of times a construct like this:

Code: Select all

self = oldself;
oldself = self;
why?
Meadow Fun!! - my first commercial game, made with FTEQW game engine
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: Self? Other? Owner?

Post by Spike »

'self' is to qc as 'this' is to c++
it is merely the 'active' entity that your code is currently talking about.

saving self into oldself (and restoring later) is a way to refer to a different entity in various function calls, without breaking things in the function that called you (which still expects self to be what it was when the current function was called).
thus you'll find functions that notify other entities of events on that entity switching self to the entity that's being notified then calling whatever notification function required, whether thats one of the th_foo functions or a direct function call.
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Self? Other? Owner?

Post by Baker »

This was always on my list of things I didn't know. Good question. I kept forgetting to ask.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
mh
Posts: 2292
Joined: Sat Jan 12, 2008 1:38 am

Re: Self? Other? Owner?

Post by mh »

If you know some C it's helpful to look at the engine source here. Search for calls to PR_ExecuteProgram and you'll find that it's always preceded by pr_global_struct->self = whatever, and the same for other. Have a look at which entity is being set to self, have a think about what's happening there, and it starts to come together.
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
toneddu2000
Posts: 1395
Joined: Tue Feb 24, 2009 4:39 pm
Location: Italy

Re: Self? Other? Owner?

Post by toneddu2000 »

@Spike, thanks a lot, now it's more clear.

@mh, yeah I'd like to learn more "serious" programming but I'm bit stuck with pointers! :) I'll try to search what you post it, thanks!

It would be great if it was available a "complete quakec guide for beginners"! :lol:
Meadow Fun!! - my first commercial game, made with FTEQW game engine
taniwha
Posts: 401
Joined: Thu Jan 14, 2010 7:11 am
Contact:

Re: Self? Other? Owner?

Post by taniwha »

toneddu2000: think of pointers as "entity" and the fields as (eg) ".float health", and ignore the difference between -> and . (even the designers of C regret that one). Pointers are really quite easy once you realize they're just a way to have one variable refer (point) to another. No different than asking one friend for another friend's phone number :).
Leave others their otherness.
http://quakeforge.net/
toneddu2000
Posts: 1395
Joined: Tue Feb 24, 2009 4:39 pm
Location: Italy

Re: Self? Other? Owner?

Post by toneddu2000 »

wow taniwha! I never seen pointer in this way! It's more clear, like this, thanks!
Meadow Fun!! - my first commercial game, made with FTEQW game engine
taniwha
Posts: 401
Joined: Thu Jan 14, 2010 7:11 am
Contact:

Re: Self? Other? Owner?

Post by taniwha »

I'm glad I could help :)
Leave others their otherness.
http://quakeforge.net/
Post Reply