stepper
stepper

Reputation: 1157

Is there a better way of initializing classes?

I'm up to making some sort of list of weapons, hardcoding them of course so they can't be hacked or modded without special stuff. And I was just wondering if there's a faster way of doing this:

public Firearm(string WeaponName, int WeaponId, float Damage, FireType WeaponFireType, float FireRate, float ReloadTime, int MaxClip, bool InfiniteAmmo)
{
    weaponName = WeaponName;
    weaponID = WeaponId;
    damage = Damage;
    weaponFireType = WeaponFireType;
    fireRate = FireRate;
    reloadTime = ReloadTime;
    maxClip = MaxClip;
    infiniteAmmo = InfiniteAmmo;
}

and then later this: (This is just an example, I won't be doing it like this in the actual script.

void InitializeWeapons()
{
    var allFirearms = new List<Firearm>();
    allFirearms.Add(new Firearm(
        "Pistol", 
        0, 12.5f, Firearm.FireType.SemiAutomatic, 
        1.2f, 1.5f, 12, true
        )); 
}

Is there a different way of doing that? Something that is a little more cleaner and easier to see what is going on?

Edit: Kind of sucks I can't put you all as approved answer, since they all helped me learn something and so forth. I'll just go for the one I decide to use. I'll finish the code and approve the one that I used.

Upvotes: 2

Views: 118

Answers (3)

michalk
michalk

Reputation: 566

Maybe try Fluent Builder pattern? The pattern is not without flaws, but it sure makes complex object creation more readable. First you need to add WeaponBuilder class:

public class WeaponBuilder
{
    private string name;
    private int damage;
    private int fireRate;

    public WeaponBuilder CreateWeapon(string name)
    {
        this.name = name;
        return this;
    }

    public WeaponBuilder WithDamage(int damage)
    {
        this.damage = damage;
        return this;
    }

    public WeaponBuilder WithFireRate(int fireRate)
    {
        this.fireRate = fireRate;
        return this;
    }

    public Weapon Build()
    {
        return new Weapon(name, damage, fireRate);
    }
}

And then you create your Weapons like this:

    var weapon = new WeaponBuilder()
        .CreateWeapon("AK-47")
        .WithDamage(100)
        .WithFireRate(50)
        .Build();

Upvotes: 3

GvS
GvS

Reputation: 52518

You could use a T4 Text Template:

  • Store your weapons in a database/config file, etc. in your development environment.
  • Generates code at compile time, so your data is hardcoded.

Your code might be ugly, but the data file will be much more maintainable, and you can even use a database or shared file to store the data.

To use it, create a file with .tt extension in Visual Studio. Looks something like this:

The following code is not tested

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>

namespace Foo {
    class Bar {

        static void InitBar() {

<# 
            // Create an object for every line:
            // Create an object for every line:
            var lines = File.ReadAllLines("testfile");
            foreach(var line in lines) {
              <#= var newObject = new Weapon("#> line <#= "); #>
            }
#>
        }    

    }
}

Upvotes: 1

Tigran
Tigran

Reputation: 62246

  1. Write down the information into some configuration file.
  2. Add code that reads configuration file in iteration and creates objects you need.

    • Cons: You need to spend a time to create a configuration file.

    • Pros: Once you have it, the code becomes clearer and scallable, open for future possible expansions and solutions.

Upvotes: 1

Related Questions