Author Topic: Detect when an Object (.DTS model) lands near player  (Read 4040 times)

Yeah you kinda did lol
I mean now I know what you meant, but all you said then was "datablock"

Well its been almost two days since thread creation and no word from op so it's not like we're really derailing anything

To clear up any confusion, the only thing he really needs to worry about is what you posted, which is a good start (the ::onImpact function might not apply, depending on what he's trying to do), and

Sorry, there was a project for my Modern European Class. So anyways i updated the topic with better information, so preatty much how it is in shooting games, guy throws a grenade, when the grenade lands next to an enemy they yell "Holy forget grenade!"

Quote
Assuming you wanted to do it when it lands (correct me if I'm wrong here) the code should look something like this:
Code: [Select]
function YourDatablock::onImpact(%datablock, %vehicle, %collidingObject, %position, %speed) {
    %retVal = parent::onImpact(%datablock, %vehicle, %collidingObject, %position, %speed);
    %datablock.playSoundRadius(%vehicle);
    return %retVal;
}

So the best time to call this is when the object hits the ground, and IF it hits the ground next to a player?

That function detects if there are any players, so call it every time it hits the ground. Otherwise you'll be doing two checks for players rather than one.

However, since you'll be using a projectile, you're gonna wanna use YourDatablock::onCollision. The downside about this is that I don't think it'll trigger that if the grenade hits the ground, only if it hits a brick, player, vehicle, or other object. I got around this with onImpact earlier, but that only works for players and vehicles. You could theoretically make the player throw a vehicle, then after a certain amount of time have that vehicle explode as if it were a grenade, but right now I'm a little too lazy to explain the whole process. Basically, you'd parent YourWeapon::onFire and create a new vehicle at the player's getMuzzlePoint(0) rotated to their getWords(%player.getTransform(), 3, 5) with a velocity of whatever the player's getMuzzleVector(0) is multiplied by whatever the initialVelocity of the projectile was supposed to be (and optionally add the player's getVelocity() to that), then set the vehicle explosion parameters on the vehicle and right after you create it call %vehicle.schedule(delay, "explode"); on it.
« Last Edit: November 19, 2014, 08:03:58 PM by $trinick »

So anyways i updated the topic with better information, so preatty much how it is in shooting games, guy throws a grenade, when the grenade lands next to an enemy they yell "Holy forget grenade!"
ll this is when the object hits the ground, and IF it hits the ground next to a player?
Alright, so first change you're going to want to do:
trinicks ::playSoundRadius method will play the sound for every player in range, which would be obnoxious for use.
Just throw a break; after the play3d call

Second, I don't believe projectiles have an onImpact method, so you may need to use onCollision instead

So the best time to call this is when the object hits the ground
That function (or onCollision, if that needs to be used instead) is the callback when it does hit the ground, it will automatically be called

and IF it hits the ground next to a player?
the playsoundradius will take care of checking for that

So instead of
Code: [Select]
function YourDatablock::onImpact(%datablock, %vehicle, %collidingObject, %position, %speed) {
it be
Code: [Select]
function YourDatablock::onCollision(%datablock, %vehicle, %collidingObject, %position, %speed) {?

 i want other players to hear the sound so should it be
Code: [Select]
%player.server.play3d(YourSoundDatablock, %position); or something?
Like this, is the "break;" in the right spot?
Code: [Select]
function YourDatablock::playSoundRadius(%datablock, %object) {
    %position = %object.getPosition();
    %radius = 5;
    initContainerRadiusSearch(%position, %radius, $TypeMasks::PlayerObjectType);
    while(%player = containerSearchNext()) {
        if(isObject(%player.client))
            %player.client.play3d(YourSoundDatablock, %position);
                break;
    }
}

That break will be used every time the while loop is used. Just indenting your code wont do anything, you need brackets after your if statement surround all of the functions thereafter. However I do not know if that's the right spot for the break.