Download

How to check if weapon is being held by player or bot?

Hello guys. My guns use a custom reload function, it is working perfectly.

The reload function is an exec function trigerred by key “R”. However I need that the bot also reload the weapon, because the weapons are used by both player and bots.

For the bot I made a simple function that will make the gun automatically reload whenever it reaches ammocount of 1, it works, however I don’t know how to check if the weapon is being held by player or bot, because I want the auto reload function only for bots, as for the player he needs to press “R”.

This is a part of my code


//function to fire gun and play animation/////////////////////////
simulated function FireAmmunition()
{

    //this variables will be used to access player controller
    local Playercontroller localplayer;

        //this will check if ammo reached zero to automatically reload weapon
        if ( AmmoCount <= 1 && UTPawn(Owner) != UTPlayerController(localplayer) )
        {
        //this will call the simulated reload function
        Super.AutoReload();
        }

However it does not work, I receive the compiling error message: "Error, Operator ‘!=’: Comparison will always suceed"

Any help???

you never assigned the localplayer [COLOR=#252C2F][SIZE=13px]variable with any value [/SIZE][/COLOR]

Hello @Chosker

Thanks dude for the repply, however, do you know how can I detect if a weapon is being held by the player or the bot?

Using this statement


UTPlayerController(localplayer)

Did work for me to access the player controller in other script of mine, like this:


UTPlayerController(localplayer).IgnoreLookInput(true);

However here on my weapon script, it is not detecting who is holding the weapon.

Any ideas? :rolleyes:

read about variable scope. you can’t simply define a variable and expect it to have a value assigned. in the other script it was probably pre-assigned somewhere.

in terms of the weapon class the way to get the player would be something like


UTPlayerController(Pawn(Instigator).Controller)

however in a multiplayer game the Pawn Controller isn’t replicated, so simply assuming that the entity controlling a weapon is a bot just because it doesn’t have a playercontroller might not be correct

Thanks bro, I will try it!!!

Hello man. After a lot of thinking, I just asked myself, why don’t let the weapon auto-reload for player aswell?

So I just changed my code to this:


//function to fire gun and play animation/////////////////////////
simulated function FireAmmunition()
{

        //this will check if ammo reached zero to automatically reload weapon
        if ( AmmoCount <= 1 )
        {
        //this will call the simulated reload function
        Super.AutoReload();
        }

        super.FireAmmunition();
}
//function to fire gun and play animation/////////////////////////

The function AutoReload() holds all the logic for reloading, including playing the animations.

I am making a 3rd person action shooter, and I want it to have intense shooting and action, so there is nothing wrong to have the weapon auto-reload regardless the fact that the player can reload it manually by pressing ‘R’. So this way whenever the player is on the heat of a battle, he doesn’t need to worry about reloading the weapon, just collect ammo and the weapon will autoreload.

Cheers!

we use this to find if its a bot or not
if(Instigator.PlayerReplicationInfo.bBot)

Hello dude!!!

Thanks for the info.

So long as I already opened this thread, and I have another doubt related to UTWeapon, I will ask here to see if you can help me.

as I said I’m using UTGame as a basis for my game, and a problem of UTGame is related to the character locomotion, there is no walk, just running. Even the running animation run_fwd_rif has the pawn running and aiming the gun.

However I changed the running animation for a sprint animation, similar to Resident Evil 6, where the character runs with the gun down, but the problem is that in this case when the player shoots while running it gets weird because the bullet comes out of the gun while the character is not aiming the gun in any direction (the gun in pointing down).

to solve this problem I used this code on the weapon class:


//function to fire gun and play animation/////////////////////////
simulated function FireAmmunition()
{

       //this will check if ammo reached zero to automatically reload weapon
       if ( AmmoCount <= 1 )
       {
       //this will call the simulated reload function
       Super.AutoReload();
       }

       //this will play the shooting animation only whenever the player is running, which is when it is needed to make the transition
       //between running and shooting

**       if(UTPawn(Owner).Velocity.x > 0 || UTPawn(Owner).Velocity.y > 0 || UTPawn(Owner).Velocity.z > 0)
       {

       //this will play the custom shoot animation only on the half top body
       UTPawn(Owner).TopHalfAnimSlot.SetActorAnimEndNotification(true);  
       UTPawn(Owner).TopHalfAnimSlot.PlayCustomAnim('idle_ready_rif', 1, 0.15, 0.15, true, false);

       }**

else
{
       UTPawn(Owner).TopHalfAnimSlot.StopCustomAnim(0.15);
}

super.FireAmmunition();
}
//function to fire gun and play animation/////////////////////////


So the statement** if(UTPawn(Owner).Velocity.x > 0 || UTPawn(Owner).Velocity.y > 0 || UTPawn(Owner).Velocity.z > 0)** will check if the pawn is running in any direction, if so, then it will play the idle_ready_rif animation on top body slot, which will make the character aim the gun while shooting.

It is working, however, sometimes it does not work, sometimes the character runs and shoots while the gun in pointing down, sometimes the **idle_ready_rif **does not play.

Is there a better way to do this?

Thanks!