Tuesday, February 5, 2013

UDK How To: How to detect Alternative Fire on Custom Weapons

Alt fire is when you press the right mouse button to shoot.

For instance, the Link Gun in UT has an alt fire of a beam.

If you are creating a Custom weapon you can access this functionality quite easily.

Note that there are other ways to do this (such as creating your own exec function for the "DefaultInput.ini" file to refer to). This allows you to access alt-fire without modifying anything other than your own Custom Weapon Class.

You will need to work with:

  • Your Weapon script
You may read the following scripts:

  • PlayerController.uc
  • Pawn.uc
  • Weapon.uc
  • DefaultInput.ini


TL;DR
You will go to your weapon and overwrite the function:
  • simulated function StartFire(byte FireModeNum)
FireModeNum 0 means primary, 1 means alt.

So you can have functionality execute or not execute depending on the 0 or 1 value.

SOME EXPLINATION
If you check out the UDK\UDKGame\Config\DefaultInput.ini, you can scroll around until you find the player input for alt fire:
  • .Bindings=(Name="GBA_AltFire",Command="StartAltFire | OnRelease StopAltFire")
We can see that an exec function get's called to handle the business, "StartAltFire ".

This means in the PlayerController script this function get's called.

When we check out the script ourselves we see that it then calls:
  • Pawn.StartFire( FireModeNum );
In Pawn.uc, we see this in turn calls:
  • Weapon.StartFire(FireModeNum);
...which is what we took advantage of.

Do you see? By tracing the desired info from the input we were able to find the scripts related to our result and utilize the fuck out of the necessary functions.

This can be a good way to debug your UDK designs (that is, to find out what is going on this huge system).


5 comments:

  1. Hi. I'm trying to give an alternate attack to my melee system, currently it works with leftmouseclick, but I want to add a secondary rightmouse attack.
    I added the line
    simulated function StartFire(byte FireModeNum)
    to my weapon file but now I can't swing my sword... I'm kinda lost about this alternate fire thingy.

    ReplyDelete
    Replies
    1. Go to pastebin.com and copy+paste your entire weapon script file.

      Post the link here so I can see it and check out what's happening in your code.

      It'll be a ton faster to see your stuff first before I start throwing out random words to try and fix things lol.

      Delete
    2. I actually have two files, a general weapons file and the actual sword file, here's the two of them

      http://pastebin.com/GWiFUYm3
      http://pastebin.com/it4ynwxY

      There are comments in spanish because that's my native language...

      Delete
    3. It looks like you already have some good stuff working though (animations, hit effects, etc) and understand how to express yourself well in the uscript.

      What you do is add the function call for StartFire(byte FireModeNum), and within the function, you perform your logic for attack A when FireModeNum=0, and attack B when FireModeNum=1. It's like an if/else if kind of thing.

      Your FireAmunition() function seems to be your attack. You can take that code and put it into a function called "MyPrimarySwordAttack()". Then, have this get called within StartFire when FireModeNum is 0. When it's 1, have it call a function "MySecondarySwordAttack()" to execute your other attack.

      Delete
    4. I'll give it a try then, btw thanks for replying so quickly ^^

      Delete