Self aware classes?
Self aware classes?
Self aware classes?
Self aware classes?
Self aware classes?
Self aware classes? Self aware classes? Self aware classes? Self aware classes? Self aware classes? Self aware classes? Self aware classes? Self aware classes?
Self aware classes? Self aware classes?
Self aware classes?
Go Back  Xtreme Visual Basic Talk > > > Self aware classes?


Reply
 
Thread Tools Display Modes
  #1  
Old 10-30-2011, 10:49 PM
scorpydude scorpydude is offline
Newcomer
 
Join Date: Feb 2009
Posts: 1
Default Self aware classes?


Hi guys... long time reader first time poster. Please be gentle.

I avidly create games in VB.net. Being a hobby programmer I give my games away for free. Since I'm a hobbiest without formal training I do try and educate myself on proper programming principles and follow them when programming such as OOP. There are however some things I have obviously missed being self-taught that perhaps I would know if I were formally educated.

Once such issue that has really been my bane is how different classes (from now on referred to as objects) 'know' about each other WITHOUT programming specific interaction (stay with me here... please...)

Okay imagine this, you have a space game and the following objects

clsShip
clsMissile
clsAsteroid
clsSpaceJunk

Now each one of these objects has a Health, X,Y and Z private members and properties that are all inherited from clsSpaceEntity for argument sake. Now one way of programming collision between them would be to code out the following logic in the main game loop

for each ship in a list of ships
check each clsMissile in a list to see if it collides with it and if so, reduce health
check each clsAsteroidin a list to see if it collides with it and if so, reduce health
check each clsSpaceJunka list to see if it collides with it and if so, reduce health
... and so on and so on for every object
next

ect ect ect...

Now this might seam okay if your talking about the simple example above but some of my games have tens or even HUNDREDS of objects that interact in this basic way.

Now my question to the experienced programmers out there.

Is there a way in OOP to do the following...

for each thing that inherits from clsSpaceEntity
check against every other object that inherits this type (except itself) and if they collide then reduce health
next

?


This sort of ability for a type of objects/class or whatever to be 'aware' of another and how they are the same/different and interact would save me tonnes and tonnes and TONNES of coding.



Any assistance/help or feedback on this would be very appreciated. Thank you for your time, sorry for the long read.
Reply With Quote
  #2  
Old 10-31-2011, 05:38 AM
DrPunk's Avatar
DrPunkSelf aware classes? DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

If a class inherits from another class then you can refer to the inherited class as the class it inherits from.

So, in your example, you could have a collection of clsSpaceEntity that contained all your clsShip, clsMissile, clsAsteroid, clsSpaceJunk and you treat them all as clsSpaceEntity and are able to look at the properties of clsSpaceEntity for each of those classes.

If you need to know exactly what class one of the clsSpaceEntities is then you use TypeOf to check the type of the class, and CType or DirectCast to access the particular properties of that class.

Code:
dim c as new collection

c.add(New clsMissile)
c.add(New clsShip)
c.add(New clsAsteroid)
c.add(New clsSpaceJunk)

' You can iterate over each object with the class they inherit from

dim o as clsSpaceEntity
For each o in c
     ' Use typeof to find out what exactly the class is
     If TypeOf o is clsShip Then
         ' You can access the properties that belong only to clsship
         ctype(o, clsShip).OnlyBelongsToShip
     End if
Next
Dunno if that helps.
__________________
There are no computers in heaven!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Self aware classes?
Self aware classes?
Self aware classes? Self aware classes?
Self aware classes?
Self aware classes?
Self aware classes? Self aware classes? Self aware classes? Self aware classes? Self aware classes? Self aware classes? Self aware classes?
Self aware classes?
Self aware classes?
 
Self aware classes?
Self aware classes?
 
-->