Turn-Based RPG: Guns: Magazines and Ammo

I’ve been trying to adapt a Mecha TableTop RPG into a Turn-based battling game. One feature is that, instead of a basic attack with the equipped weapon, the Mecha can select from one of it’s many weapons equipped and attack with it (if it’s melee, it adds it’s Power Stat to the weapon’s attack while if it’s ranged, it adds the Unit’s Force stat to the weapon’s attack).

The guns have Clips (or Magazines) which hold a limited number of shots. Most guns use one shot each attack but rapid fire guns fire more than one shot, which empties the same amount from the magazine (rapid fire weapons also have a -50% strength penalty to their Force Stat modifier). When the magazine is empty, the weapon cannot be fired until it is reloaded. To reload guns, the player needs to use a certain number of points from their “Ammo” or “Energy” statistic. Also, some energy weapons use energy directly from the unit’s energy stat.

I haven’t seen any scripts that handle a system like this, so are there any suggestions? My weapon script should be posted below if I did the format right.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Weapon
{
    public string name;
    public int attack;
    public int clip;
    public int bulletUse;
    public int reloadAmmo;
    public int reloadEnergy;
    public int plugs;
    public int durability;
    public int recharge;

    public enum Attribute { Conventional, Energy, Heat, Shock}

    public enum Range { Melee, Ranged, Hybrid}

    public enum Effect { Scatter, Pierce1, Pierce2, Missile, Drone}

    public Weapon (string name, int attack, int clip, int bulletUse, int reloadAmmo, int reloadEnergy, int plugs, int durability, int recharge)
    {
        this.name = name;
        this.attack = attack;
        this.clip = clip;
        this.bulletUse = bulletUse;
        this.reloadAmmo = reloadAmmo;
        this.reloadEnergy = reloadEnergy;
        this.plugs = plugs;
        this.durability = durability;
        this.recharge = recharge;
    }
}

Um… write one?!

It’s only a short list of requirements… start with the first one and work through it.

When you have the first requirement done and tested, commit your code and move to the next.

There’s ten billion tutorials on all of it: magazines, reloading, RPG stats modifiers and buffs and whatnot.

When something surprises you or does not work the way you expected, start debugging. Here’s how:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

Okay, buddy, I appreciate your enthusiasm, but I am relatively inexperienced and was looking for more direction. Pretty much all of the Gun scripts I’ve seen are for FPS games and I’m not sure how to implement those into a turn-based RPG. In fact most of the tutorials I find focus more on older RPGs that use basic MP for resource points and such. That was why I was trying to seek professional help here.

I am afraid I don’t know any other way to make games than to enthusiastically write the code required.

If you are aware of a different way, I would love to hear about it. It might save us all a lot of time and effort.

If by “seek professional help here” you mean “Find someone to take my 36-line Weapon class as is, which has nothing but a few data fields assigned in a class constructor and turn it into a faithful rendition of a massive tabletop RPG,” then I fear you may have landed in the wrong forum.

The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

This assumes you have at least written and studied some code and have run into some kind of issue.

If you haven’t even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

If you just want someone to do it for you, you need go to one of these places:

https://forum.unity.com/forums/commercial-job-offering.49/

https://forum.unity.com/forums/non-commercial-collaboration.17/

https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons

Basically you need to figure out what kind of system works for your game. that’s kinda the role.
There’s no set standard unless you’re workin’ for somebody and need to follow some guidelines to aid with working as a group. However, you do have requirements that only you know about and that makes it difficult for folks to tell you the right move.

We can look at examples from the past for inspiration, old video games and try to deconstruct them but then you’re spending more time thinking about it then the original developers did! Here, you are looking to customize a preexisting game without understanding the fundamentals. Because setting up a weapon or inventory system is usually among the first things folks are able to accomplish.

but it’s really up to you how you want to set it up

Im not sure if there is any mech themed tutorial out there, but there are many other turn based tutorials (for “fantasy” style rpg). I would suggest to just watch any of these, because in the end it doesn’t really matter if the “attack” costs mana or ammo. it’s the same idea just a different name.

The weapon attack logic can also be handled like any other turn based ability system.

It sounds like you want magazin per weapon instead of a “shared” resource (which mana usually is) so simply move the magazin variables and logic to the weapon class instead of the player stat class and keep the ammo / energy stats on the player, so they can be “shared”.

1 Like