CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 1999
    Location
    Holland
    Posts
    34

    Arrays in a class

    Hi, I'm writing a program that parses a text-file. That text-file is actually a file that contains values for creating your own bot (computer-controlled player) in the game Quake 3 Arena. The text-file has a structure like this:

    skill 1
    {
    name "blabla"
    accuracy "12"
    viewfactor "5"
    ...
    }

    skill 4
    {
    name "blabla"
    accuracy "20"
    viewfactor "6"
    ...
    }

    skill 5
    {
    name "blabla"
    accuracy "30"
    viewfactor "10"
    ...
    }



    I want to create a class (called bot) so that I can write code that sais
    dim mybot as new bot


    I want that the class has properties like
    mybot.skill1.name = "hello"
    mybot.skill1.accuracy = 15
    mybot.skill1.accuracy = 20
    mybot.skill5.accuracy = 30


    is it possible to do something like this in VB, is it possible to declare these properties of the bots as arrays in the class file or must I use some other method?

    I have never worked with class files before if anybody could help me out, I would appreciate it greatly.


  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Arrays in a class

    Glad someones trying to improve the bot's in QuakeIII at least...

    If you only have the 5 skill levels (and always have them in the file), then I'd suggest that you have two class modules - cBot and sSkill, you don't mention what else should be in the bot class, but here's what I made up :

    I'm starting with the skill class because that will be referenced by the Bot Class :


    (Class module called cSkill)

    option Explicit
    '
    private msName as string
    private miAccuracy as Integer
    private miViewFactor as Integer
    '
    private Sub Class_Initialize()
    '
    ' Initialise with some default values (lowest setting ?)
    '
    me.Name = "Default"
    me.Accuracy = 12
    me.ViewFactor = 5
    End Sub
    '
    public property let Name(byval sNewName as string)
    msName = sNewName
    End property
    '
    public property get Name() as string
    Name = msName
    End property
    '
    public property let Accuracy(byval iAccuracy as Integer)
    miAccuracy = iAccuracy
    End property
    '
    public property get Accuracy() as Integer
    Accuracy = miAccuracy
    End property
    '
    public property let ViewFactor(byval iViewFactor as Integer)
    miViewFactor = iViewFactor
    End property
    '
    public property get ViewFactor() as Integer
    ViewFactor = miViewFactor
    End property




    This code will initialise the skill object with default values (taken from your file for skill level 1)

    Next the cBot Class :


    option Explicit
    '
    private moSkill1 as cSkill
    private moSkill2 as cSkill
    private moSkill3 as cSkill
    private moSkill4 as cSkill
    private moSkill5 as cSkill
    '
    private Sub Class_Initialize()
    '
    ' Create Skill Objects
    '
    set moSkill1 = new cSkill
    set moSkill2 = new cSkill
    set moSkill3 = new cSkill
    set moSkill4 = new cSkill
    set moSkill5 = new cSkill
    End Sub
    '
    public property get Skill1() as cSkill
    set Skill1 = moSkill1
    End property
    '
    public property get Skill2() as cSkill
    set Skill2 = moSkill2
    End property
    '
    public property get Skill3() as cSkill
    set Skill3 = moSkill3
    End property
    '
    public property get Skill4() as cSkill
    set Skill4 = moSkill4
    End property
    '
    public property get Skill5() as cSkill
    set Skill5 = moSkill5
    End property




    When created, the Bot object will create 5 internal skill objects (if this isn't a set number, then use a collection and pass the objects that way).

    A calling program / form might use the Bot object as such :


    Dim oBot as cBot

    set oBot = new cBot

    oBot.Skill1.Name = "Chris"
    oBot.Skill1.Accuracy = 1
    oBot.Skill1.ViewFactor = 2

    oBot.Skill2.Name = "blah blah"
    oBot.Skill2.Accuracy = 3




    You might have noticed that I changed the values for accuracy and viewfactor to integers (just to be nice and easy) - you can always change them back to strings if you like.






    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

  3. #3
    Join Date
    Oct 1999
    Location
    Holland
    Posts
    34

    Re: Arrays in a class

    Thanks a lot, you helped me out a lot, you can't use integers for accuracy and aim (wich aren't the actual names, just made them op for simplicity) because they have to contain values between 0.0 and 2.0. Again, thanks a lot for the reply.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured