Frustrating problem involving structures

FieldDoc

Newcomer
Joined
May 11, 2003
Messages
10
Hi,
I am a relative newbie and I have been struggling with a problem for several days now and I need some help. It's quite long-winded (sorry) so i'll start from the top.

I am trying to make a turn-based 2-player strategy game based on the warhammer 40K tabletop game (i.e. tabletop wargame). In this game there are many different types of units that the player can control, each unit with many stats, these are:

WeaponSkill (WS)
BallisticSkill (BS)
Strength (S)
Toughness (T)
Wounds (W)
Leadership (Ld)
Save (Sv)

I thought the best way to input all these units would be to make a new public structure, termed Infantry. This is shown below:
Visual Basic:
 Public Structure Infantry   
          Public Race As String
          Public Name As String
          Public Points As Short
          Public WeaponSkill As Short
          Public BallisticSkill As Short
          Public Strength As Short
          Public Toughness As Short
          Public Wounds As Short
          Public Initiative As Short
          Public Attacks As Short
          Public Leadership As Short
          Public Save As Short
          Public Weapon As String
    End Structure
I created a public function to add units stats as below:
Visual Basic:
 Public Function AddInfantry(ByVal Race As String, ByVal Name As String, ByVal Points _
      As Short, ByVal WeaponSkill As Short, ByVal BallisticSkill As Short, _
    ByVal Strength As Short, ByVal Toughness As Short, ByVal Wounds As Short, _
    ByVal Initiative As Short, ByVal Attacks As Short, ByVal Leadership As Short, _
    ByVal Save As Short, ByVal Weapon As Short) As Infantry
        With AddInfantry
            .Race = Race
            .Name = Name
            .Points = Points
            .WeaponSkill = WeaponSkill
            .BallisticSkill = BallisticSkill
            .Strength = Strength
            .Toughness = Toughness
            .Wounds = Wounds
            .Initiative = Initiative
            .Attacks = Attacks
            .Leadership = Leadership
            .Save = Save
            .Weapon = Weapon
        End With
    End Function
Let's take 2 units - a "Space Marine" and a "Fire Warrior". I can add all the relevant stats easily by typing the following:

Visual Basic:
Public FireWarrior = AddInfantry("Tau", "Firewarrior", 10, 2, 3, 3, 3, 1, 2, 1, 7, 4, 10)
This would mean that, for instance:
FireWarrior.Race = "Tau"

Still awake? Cool.

When 1 unit shoots at another unit, I call a function named ShootingEngine. This is quite long and complex but it requires to be passed to it the following variables by value:
Attackers BS
Attackers S
Defenders T
Defenders Sv

I can do this quite easily if I want say, the "FireWarrior" to attack the "Space Marine" (therefore, space marine = defender) as I simply use the following syntax (the variable 'Result' stores the outcome of the shooting):

Result = ShootingEngine(FireWarrior.BallisticSkill, FireWarrior.Strength, SpaceMarine.Toughness, SpaceMarine.Save)

Fine and dandy. HERE IS THE PROBLEM IF YOU ARE STILL AWAKE!

As you can imagine, there will be many more than 2 units and hundreds of combinations of attackers and defenders. I have 2 more variables which I hoped would solve the problem:

PlayerOne (a string variable that stores the name of the unit currently controlled by player one, e.g. "FireWarrior")

PlayerTwo (a string variable that stores the name of the unit currently controlled by player two, e.g. "SpaceMarine")

I want to be able to do the following:

Result = ShootingEngine(PlayerOne.BallisticSkill, PlayerOne.Strength, PlayerTwo.Toughness, PlayerTwo.Save)

IS THIS CLEAR? I want to somehow figure out a way to make the compiler see 'PlayerOne.Strength' as the same as 'FireWarrior.Strength' (assuming that PlayerOne = "FireWarrior")

I hope someone can help me and I appreciate it if you got this far.

FieldDoc
 
Last edited by a moderator:
If you make a class instead of a structure, it will be a reference type. That means you can declare PlayerOne as your type and then assign any of your configured instances to it, and then it'll refer to that instance.

I know that doesn't sound very clear but I can't think of a better way of putting it. I think you should investigate the differences between the way value and reference types are passed.
 
Make your structures a class, then do something like..

C#:
Infantry FireWarrior;
if (PlayerOne.Name == "FireWarrior") {
    FireWarrior = PlayerOne; // Or FireWarrior = new Infantry(); ?
    FireWarrior.Strength = 19;
    // etc..
}

Or if you have several FireWarriors, you can simply make an ArrayList of Infantry named FireWarrior and just do a FireWarrior[0].Strength etc..

If I'm not mistaken this is basically what divil is getting at. Or am I not understanding clearly what either of you are saying? :)
 
It was C# :)
A translation: :)
Visual Basic:
Dim FireWarrior As Infantry
if PlayerOne.Name = "FireWarrior" Then
    FireWarrior = PlayerOne ' Or FireWarrior = new Infantry(); ?
    FireWarrior.Strength = 19
End If
 
Back
Top