Entities

Discuss programming in the QuakeC language.
Post Reply
Cobalt
Posts: 445
Joined: Wed Jun 10, 2009 2:58 am
Location: New England, USA
Contact:

Entities

Post by Cobalt »

Was wondering is there a difference between :

entity THIS;


and


THIS = spawn ();


?


From what I have seen, you can assign THIS paramaters with just it being a global float, such as THIS = THAT; etc
....so I was believing merely floating it globally was like spawning it, but I guess spawn () means its now a dynamic entity and therefore can be removed?
Lets say we walked the ent list via prvm_edicts ....would we see the the global float listed or no? Is the flobal float considered a static ent? Id think no, because we can assign it paramaters via qc....
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: Entities

Post by Spike »

you're confusing yourself now.
if 'entity this' was equivelent to 'this = spawn()' then all those statements that change self=foo all the time would require spawning new entities! expensive as heck, especially when you consider self.enemy.enemy.enemy.enemy.enemy...

all entity _variables_ are merely references to an entity object. uninitialised ones refer to the world (aka: worldspawn) entity.
thus 'entity this;' is generally equivelent to 'entity this = world;'


internally the vanilla engine has a pre-allocated array of entities.
your qc entity variables are actually an integer indexing into that array.
world is entity index 0, and thus uninitialised ents are equivelent to world.
Cobalt
Posts: 445
Joined: Wed Jun 10, 2009 2:58 am
Location: New England, USA
Contact:

Re: Entities

Post by Cobalt »

Spike wrote:you're confusing yourself now.
Yep.....and the engine is NEVER confused unless we are first :D

By ' uninitialized ' I guess you are meaning no specifics have been assigned to it yet. Once you say, THIS = THAT; , now its no longer considered referenced to world, and is now ' initialized ' correct? Unless THAT happens to be world. Therefore THIS = spawn (), is considered an initialized entity, and NOT world, right?
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: Entities

Post by Spike »

spawn allocates a new entity and returns a reference to that entity. one that isn't world.
mankrip
Posts: 924
Joined: Fri Jul 04, 2008 3:02 am

Re: Entities

Post by mankrip »

The entity data type stores addresses.

When you do
Cobalt wrote:entity THIS;
... you're creating a blank entry to store an address.

And when you do
Cobalt wrote:THIS = spawn ();
... you're storing the address of a new entity into that entry.

All blank addresses references the world entity, which is iinitialized by the worldspawn function. So, if you do

Code: Select all

entity THIS, THAT;
THAT = THIS;
... the address stored in THAT variable is still the address of the world entity, because you're just copying a blank value. Nothing is being created.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
mankrip
Posts: 924
Joined: Fri Jul 04, 2008 3:02 am

Re: Entities

Post by mankrip »

Cobalt wrote:Therefore THIS = spawn (), is considered an initialized entity, and NOT world, right?
There are different kinds of initializations.

THIS = spawn(); just means that THIS refers to a new entity. For the entity to react to physics, to .think functions, and to be visible to the clients, specific requirements must be initialized, such as .model (through setmodel() ), .solid and .movetype .

If you create a new entity variable, and allocate the address of a new entity into it but don't initialize it, you can use it just to store values. Spawnpoints are an example of that - they just hold the value of the angles and of the position where the player should be spawned into the game.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
Cobalt
Posts: 445
Joined: Wed Jun 10, 2009 2:58 am
Location: New England, USA
Contact:

Re: Entities

Post by Cobalt »

Hmm interesting. Did some real tests with the Darkplaces engine.

entity THIS;

if (THIS == world)
eprint (THIS);


[ prints nothing , eprint ignored ]

Remove the ' if ' statement and you get:

server: FREE


Replace the ' if ' statement with :

THIS = spawn ();

and eprint returns.....

server EDICT 229:


So spawn () is giving it an edict number and no other paramaters. If its got an edict num != 0 its not world I believe.

So neither of these are technicly ' world ' , though I guess calling any .entity definitions for them would return world by default.

I was curious about this to solve an issue where the local spawn () entity would be removed, the next frame, but it was being carried to another function before that frame, yet the other function was still seeing it as a freed entity....and returning a world origin. Im guessing the only way to fix it is also put it in the global after the local is spawned, and use the global in the other function , rather than returning it as an entity (). Thanks people.
Post Reply