Question about talking with multiple Objects

mattkw80

Well-known member
Joined
Jan 3, 2008
Messages
49
Location
Ontario, Canada
Programming Experience
Beginner
Hi Everybody!

Hoping someone can clear up a great mystery for me….

I'm trying to learn Object Oriented programming, and to do so, I am writing a small Role Playing Game.

I understand why we break Players, Items, and Monsters into Classes… what I don’t “get” is how does the program know which Class object is “in focus”.

For example… if I randomly generate 3 monsters… from within the game… A Zombie, A Wolf, and A Lion… for example, how do my other methods know which of
these is “in play”. If during the game, a Zombie is called new…. How does my attack method reference this ‘new’ zombie? Sorry if that’s confusing.

How I have been handling it…

I have a ‘master’ object called ‘Monster’.

Public templatemonster As New monsterClass
Public mastermonster As New monsterClass
Public zombiemonster As New monsterClass
Public wolfmonster As New monsterClass
Public lionmonster As New monsterClass


I then have to “load” and “unload” them each time I want to use them.

For example…. I will copy “zombiemonster” over top of “mastermonster”.

I will then do all my referencing to “mastermonster”.

Ie:

mastermonster.doDamage
mastermoster.doHealing

Then… to “save” it… I have to copy it back. (Copy mastermonster object to zombiemonster)

Then.. I have to ‘reset’ the mastermonster by copying the templatemonster over top of it.

Then… if a Wolf jumps in.. I have to copy wolfmonster overtop of mastermonster… etc. etc. etc.


This is all because, within the methods of whatever I want to do to these objects (damage, heal, gain xp, etc.) I only know
how to ‘reference’ it by 1 name. “ie: mastermonster”. I have to do this if I am talking to objects as well. This is a key piece I know I am missing.

How do other people handle this?

I’m so sorry if this is too broad of question, or if I’ve made this confusing.
 
No problem.

Will I essentially have 100 or so objects, all called "monster" but with unique property "names" for each?

yeah basically. Just note that you are creating a reference to an object, they are not exactly all called "monster".

I believe the for each loop is what your referring to,
For index As Integer = 0 To monsters.Length - 1
    If monsters(index).name = "zombie" Then
        monsters(index).doDamage()
 
    Else 'lets say we also want to print out the name of the monster if we do not know it
        Console.WriteLine("The unknown monster is named: {0}", monsters(index).name)
 
    End If
Next
 
For Each thisMonster As Monster In monsters 'renamed for clarification
    If thisMonster.name = "zombie" Then
        thisMonster.doDamage()
    Else
        Console.WriteLine("The unknown monster is named: {0}", thisMonster.name)
    End If
Next

The for each loop in this example gets a reference to each object in the 'monsters' array as a 'Monster' type and names it 'thisMonster'. For example, lets say there are three Monster objects in this array. the first time through the loop 'thisMonster' will be a reference to the first 'Monster' object, the second time it loops, 'thisMonster' will be a reference to the second 'Monster' object that is stored in the array and then the third time around it'll be the third, and so forth it there is more.

Also note that an array can store duplicates, unless you do something to prevent that, so not every element in an array will have unique properties unless you make it so. ex. You could have made 10 copies of the same object and put it into an array if you wanted to.
 
Last edited:
Is there anyway I can 'alias' an object's name as a string?

For example....

If a monster was called 'wolf'.

Dim targetmonster as string
targetmonster = "wolf"

'Then try to talk to the object with a string variable as it's name

targetmonster.doSomething
 
Is there anyway I can 'alias' an object's name as a string?

For example....

If a monster was called 'wolf'.

Dim targetmonster as string
targetmonster = "wolf"

'Then try to talk to the object with a string variable as it's name

targetmonster.doSomething

Can you elaborate on what your trying to do?

I'm not quite sure what your trying to accomplish by doing so, and if i understand correctly I don't think there is a way to do that..

?maybe someone else on the forum has a better suggestion?
 
Basically, if we look at this code:

For Each thisMonster As Monster In monsters 'renamed for clarification
If thisMonster.name = "zombie" Then
thisMonster.doDamage()
Else
Console.WriteLine("The unknown monster is named: {0}", thisMonster.name)
End If


This works fine, so long as I mention "zombie" specifically. The problem is, what if my game has hundreds or monster objects, or hundreds of inventory_item objects?

I'm trying to get my class methods to work with whatever my object's name is, without having to make specific reference to hundreds of potential objects, some of which don't even exist yet.


Matt
 
Remove if Statement? Here's another example working with objects using for each loop

This works fine, so long as I mention "zombie" specifically. The problem is, what if my game has hundreds or monster objects, or hundreds of inventory_item objects?

I'm trying to get my class methods to work with whatever my object's name is, without having to make specific reference to hundreds of potential objects, some of which don't even exist yet.

For Each thisMonster As Monster In monsters 'renamed for clarification
    If thisMonster.name = "zombie" Then
        thisMonster.doDamage()
    Else
        Console.WriteLine("The unknown monster is named: {0}", thisMonster.name)
    End If
Next

Can't you just remove the if statement? thisMonster represents an object in the array, it can be any object and you do not have to make any specific references, you just need to know what type of object it is. you can then change or call any properties of that object
ex.
For Each thisMonster As Monster In monsters
    'get and output this monsters properties
    Console.WriteLine("The unknown monster is named: {0}. It current has {1} HP.", thisMonster.name, thisMonster.hp)

    'call this monsters methods
    thisMonster.doDamage()
    thisMonster.doHealing()
Next



Here's a demo of a for each loop working with objects. This uses the classes from the previous posts. Also added one line to the previous HumanoidMonster's DropKick() method

        Dim str As String = "weeee"
        Dim objects() As Object = {basicMonster, zombieMonster, wolfMonster, loinMonster, New Object, str}

        For Each obj As Object In objects
            If TypeOf obj Is Monster Then
                Console.WriteLine("A object of base type Monster is found. Object is of {0} type.", obj.GetType.ToString)

                Dim thisMonster As Monster = CType(obj, Monster)

                Console.WriteLine("Current Monster Stats:: Name={0}, HP={1}", thisMonster.name, thisMonster.hp.ToString)

                If TypeOf obj Is AnimalMonster Then
                    Dim animal As AnimalMonster = CType(thisMonster, AnimalMonster)
                    animal.Growl()

                ElseIf TypeOf obj Is HumanoidMonster Then
                    Dim humanoid As HumanoidMonster = CType(thisMonster, HumanoidMonster)
                    humanoid.DropKick()

                Else
                    thisMonster.doDamage()

                End If

            ElseIf TypeOf obj Is String Then
                Dim thisString As String = CStr(obj)
                Console.WriteLine("A string object was encountered. It contains ""{0}""", thisString)

            Else
                Console.WriteLine("An unknown object type of {0} was encountered.", obj.GetType.ToString)

            End If

            Console.WriteLine()
        Next

this code outputs:
VB.NET:
A object of base type Monster is found. Object is of ConsoleApplication1.Module1
+Monster type.
Current Monster Stats:: Name=monster, HP=100
monster was attacked!, Now has 99 HP

A object of base type Monster is found. Object is of ConsoleApplication1.Module1
+HumanoidMonster type.
Current Monster Stats:: Name=zombie, HP=100
zombie DropKicked you!

A object of base type Monster is found. Object is of ConsoleApplication1.Module1
+AnimalMonster type.
Current Monster Stats:: Name=wolf, HP=100
wolf used Growl on you!

A object of base type Monster is found. Object is of ConsoleApplication1.Module1
+AnimalMonster type.
Current Monster Stats:: Name=loin, HP=100
loin used Growl on you!

An unknown object type of System.Object was encountered.

A string object was encountered. It contains "weeee"
 
Thanks again 'Flipped'. You've really gone above and beyond.

I think - instead of copying the object "in focus" into a temporary object, looks like I need to learn to add all objects I may be working with into a collection, and then learn to use loops to work with the objects based on whatever conditions I need to.

Object Collections seem to be the key that I was missing.

Thanks again.
 
Back
Top