Game Development Community

Looking for a start for alt fire.

by Philip Gregory · in Torque 3D Professional · 02/21/2012 (1:43 am) · 7 replies

First things first:I don't want a solution for this problem. I'm using it as a learning experience!

With that being said, I'm trying to prototype an FPS. For the prototype I'll stick with the default weapons and key commands and such. However there is one obstacle:
I want to use the right mouse button to cast spells.

Here's what I know about programming: I've taken a programming logic & design course in college, and I can read and plug in things via trial and error. This is what I'll be learning TorqueScript by.

Here are questions I want answers to (more to come as I learn):
1.) To assign the right mouse key to cast a spell (we will say "altFire"), I've started to browse the scripts. In the scripts I found ./Scripts/Client/default.bind.cs -- Is this where I should be looking?

2.) Assuming that I'm looking in the right place, is
moveMap.bind( mouse, button0, mouseFire );
the assignment of the LEFT mouse buttom and
moveMap.bind( mouse, button1.......
the assignment of the right mouse button, ie: Mouse 0=left and Mouse 1=Right?

3.) Ignoring my findings, I could edit the Lurker Weapon to have an alternate firing that "casts" the spell, right?

4.) If #3 is true (which I'm sure it is), I would need to create a different Lurker for each spell, right? If so, this will suit a prototype of my game.

5.) If all above statements are true: it would be cleaner (but harder) to reassign the right mouse button to spells and assigning keys to alternate between spells, right?

Edit: I started out with the first two questions. Then the third one. Then the fourth and fifth one. I think documenting my questions is helping me piece this together better.

About the author

Yes... Those are ribs. Current projects: Breaking out of the box in T2D, devoting efforts and talents to Middle School student tech projects (including basic Python programming!)


#1
02/21/2012 (10:14 am)
Quote:1.) To assign the right mouse key to cast a spell (we will say "altFire"), I've started to browse the scripts. In the scripts I found ./Scripts/Client/default.bind.cs -- Is this where I should be looking?
Yes, game/scripts/client/default.bind.cs is the correct script to change key bindings. Although, keep in mind that game/scripts/client/config.cs overrides the default.bind.cs file as that is where the user's keybinding preferences are saved. Because of this you'll want to either delete the config.cs file after changing default.bind.cs to see the changes take effect or also update the config.cs file at the same time. I personally just delete the config file to save time.


Quote:2.) the assignment of the right mouse button, ie: Mouse 0=left and Mouse 1=Right?
Correct, button0 is left, button1 is right, and button2 is middle. In Torque 3D 1.2 release the right mouse button defaults to camera zoom. You can find it binded as follows:
moveMap.bind( mouse, button1, mouseButtonZoom );

And several lines above that one you'll find the function definition of mouseButtonZoom.


Quote:3.) Ignoring my findings, I could edit the Lurker Weapon to have an alternate firing that "casts" the spell, right?
First Part: No, you cannot ignore your findings as you'll need to change the right mouse button keybind to become the actual altFire again as it was done in Torque 3D 1.1 :)

Of course you don't have to use right mouse button for alt firing as you can use any key or mouse button to do so, but you'll still need to bind AltFire to something. Going back to game/scripts/client/default.bind.cs and search for altTrigger and you will find both the commented out function and the associated keybinding for it of which is too commented out. Uncomment out the function and the associated keybinding under the mouseFire keybinding. Next, be sure to then just comment out or change the keybinding for mouseButtonZoom as leaving it will override/replace your right mouse button handler since it will execute after your altTrigger binding within the script. Now you'll have working alt fire for weapons that have that ability which I discuss in the next part.

Second Part: You can change onFire and onAltFire in game/scripts/server/weapons.cs to do anything you want it to do, you'll just have to change the code in those functions to do a test to see if the weapon being fired is either the stock wepaons, in that case just run the code as normal, or if its your custom weapon(s) to run your custom code to do spell casting and what not.

See game/art/datablocks/weapons/rocketLauncher.cs as an example of a weapon that has both primary and secondary/alterative firing. You'll also see that onFire and onAltFire function calls are based off of weapon states in the weapon's ShapeBaseImageData datablock.


Quote:4.) If #3 is true (which I'm sure it is), I would need to create a different Lurker for each spell, right? If so, this will suit a prototype of my game.
You are not required to create multiple weapons per spell as described above you have full control over what happens when the player uses primary and secondary/alternative fire triggers. You can pretty much do whatever you want. Such as use another keybinding, or even a custom popup GUI dialog with a menu, to change or toggle through various spells that will be casted.


Quote:5.) If all above statements are true: it would be cleaner (but harder) to reassign the right mouse button to spells and assigning keys to alternate between spells, right?
It all depends on what you want. Do you want to just cast spells and toggle through them without being tied to a weapon/item that the player has, or do you want the spells specific to each weapon/item that the player is using?

#2
02/21/2012 (10:47 am)
If you use two Images, each mouse button (once the altfire keybind is re-configured) will trigger the differing Images using the onFire() callback for the specific weapon or the default one found in weapons.cs. If using only one weapon Image, then the code in weapon.cs will be used where the right button = onFire() and left button = onAltFire().

This is the default system, which is easily extended such that each image could have as many modes as you wish with a simple toggle to cycle through the modes per Image.

You'll find that when mixing weapons + spells that there is an almost infinite amount of variation and ways of doing this.

Something to be aware of is that the mvTrigger used for altFire will conflict with jetting if you want the player to have jumpjets... but this can be easily remedied in the source code.
#3
02/21/2012 (11:50 am)
Thanks guys! I guess I'm on to it for the most part! I'll probably have more questions soon (probably next week since the next part of my work rotation starts tonight).
#4
03/26/2012 (1:02 am)
This has a new approach, let me know what you think:
The game I'm working on has two classes--a soldier/mage class and a soldier class able to use a weapons alt fire. The soldier/mage class can shoot and use their alt fire to cast spells while the other uses their alt fire for grenade launchers, dual wielding, etc. My solution (in the form of an algorithm, I haven't had time to implement it):
//Get the player's class and store it in a variable.
//Use the class variable to determine alt fire with something like:
%class.altfire
//inside the altfire function

This would mean that I would also need to script the classes (or copy and paste, simply changing their names, attributes, and alt fire functions).

Sound like a plan?
#5
03/26/2012 (3:08 am)
To keep things simple (without knowing the particulars of your class or weapon system) I would have class specific spell-weapons for the soldier/mage's offhand. That way if the spell-weapon is mounted the alt-fire trigger will automatically use the secondary Image instead of the alt-fire for the primary Image.
#6
03/26/2012 (1:26 pm)
In TorqueScript, datablocks are usually used as a pseudo-'class' system. Because so many callbacks are called on datablocks (like all the Armour:: functions - onCollision, onDeath, etc.), objects that use a particular datablock will use the functions associated with that datablock.

What Michael is getting at, I think, is that if you mount everything as images, you can have very simple code to handle a right-mouse click: you always trigger the second image. The soldier class will handle it one way, since they have a particular second image mounted, and the mage class will handle it another.

The downside here is that you need a separate image, instead of just being able to code one weapon with a grenade launcher and have only soldiers use it.
#7
03/26/2012 (1:40 pm)
I don't believe it was ever accurately documented, but there are two types of alt-fire coded into the engine. One image with two modes through callbacks (onFire() and onAltFire()), or two mounted images at once -- which supersedes the former by by triggering the images independently, but only using the onFire() callback for each.

You would still be able to have one weapon with dual fire modes that when equipped by the soldier will work with left & right mousefire as normal. But if the soldier-mage equipped that same weapon and had his spell-weapon also equipped, right mousefire would trigger the 2nd image (the spell-weapon). If the soldier-mage didn't have his spell-weapon equipped you could include a check in the alt-fire callback of the regular dual mode weapon that would disable the secondary mode for the soldier-mage class.