Game Development Community

Projectile Code Questions

by RedCore · in Torque Game Engine · 06/12/2004 (3:49 pm) · 4 replies

SourceIdTimeoutTicks = 7,

if(mSourceObject && mCurrTick > SourceIdTimeoutTicks)
{
mSourceObject = 0;
mSourceObjectId = 0;
}

1) This code makes no sense to me. Why would u want to clear the sourceobject and id? and after only 7ms.. Anyone know why you would want to do this?

2) Why would u not want to delete the projectile as soon as the explode/collision occured? instead of just setting the hidden variables true.. and having it not run through the process tick code?

3) If onCollision doesnt always get called, why does it still handle the scripted side of the collision? the decal code was moved to explode() becuase of this.. why isnt everything just handled in 1 function?

4) If i removed the code that set the velocity of the projectile to "0 0 0" after a collision and also the code that sets the hidden variable to true.. then shouldnt the projectile contiunue to collide with other objects even after its collided once.. (it doesnt, not sure why)


Thx,
RedCore

#1
06/13/2004 (10:26 am)
1) This is a really a three part question.

Firstly,
Quote:Torque works off of a concept of ticks. Ticks are slices of time 32 milliseconds in length.
In other words processTick is going to be called 7 times, for a total of about 224ms of time before the SourceIdTimeoutTicks takes effect.

Secondly,
As to the reason we want to timeout the sourceobject and id is somewhat cryptic. You will notice that mSourceObject is a type of SimObjectPtr. This is a smart pointer utility template class. Basically it registers a reference with the actual object we are interested in. The object itself then keeps an internal list of those references and 'notifies' them of certain events, such as the object being deleted. This is important because if the object is deleted our pointer would now be pointing to invalid memory/data. The SimObjectPtr takes care of this for us, and NULL's the pointer automatically.

The projectile only needed the source object, the player who shot the weapon which spawned this particular type of projectile, for two things:
a) to get the gun muzzle position and direction
b) to disable/enable collisions against the source object

Collisions are disabled/enabled against the source object because when the projectile first starts off we don't want it to collide with the player who shot the projectile, which is what will happen if collisions are not disabled.

Since these two purposes are fullfilled almost immedately we no longer have a use for the source object. So we set it to 0 after 7 ticks, which then tells the smart pointer utility that we no longer have a use for it, and automatically unregisters the reference for us.


2) The projectile is not deleted immediately because we must wait for the collision/explosion event to propogate to all the clients within scope of the projectile. If we were to delete the projectile immediately some or all of the clients may never recieve the event and to their client simulations it would appear as if the projectile just vanished in thin air. Once the explosion function has been called on the server a SimEvent is created to trigger at approximately 500ms from the current simulation time. This SimEvent is responsible for deleting the projectile object for us. 500ms is a safe enough time to allow the server to propogate the explosion to all the clients within scope and to insure that these clients recieve the event. Since the server has determined an explosion has occured we no longer want to process the projectile object at all, hence the reason mHidden is set to true and is checked in various functions to ensure that we no longer process a 'dead' projectile.


3) onCollision is used as a hook into script to notify the scripts that a collision with the projectile has occured. This function is only guaranteed to be called on the server since it is server side scripts which handle this type of notification.
#2
06/14/2004 (1:56 pm)
What about the sourceobjectid that gets sent to oncollision().. How does all that work? i'm asking because i'm having a problem grabbing the sourceobject.. and sending the a messageto the cleints saying such and such kill whoever.. works ok.. cept the source object isnt working..

some parts just dont make sense to me..
#3
10/01/2004 (8:10 pm)
Reason for this is so that the projectile doesn't hurt the player as it is launched. After a certain ammount of time, its not an issue so the object is designed to basically forget about who fired it. Look also at arming delay as it is related.