
-----------------------------------
Amit
Thu Jan 29, 2009 10:57 pm

Object Oriented Help
-----------------------------------
I'm in the process of making a 2-D shooter, and i'm running into a problem. I decided to try to use objects, so I have a player class, enemy class, etc. The problem I'm having is this: my player class has a gun object, which in turn has an array of bullet objects. Is there any possible way for me to access a procedure in the bullet class, through my player class?

Pretty much I want to do something like this:

 
player.gun.bullet.procedure
 

Is it possible to do this, and if so what is the syntax?

-----------------------------------
DanielG
Thu Jan 29, 2009 11:08 pm

RE:Object Oriented Help
-----------------------------------
I think it should be possible, I know it's possible to put a type inside another type.

The best way to check this would be to try it out with a few simple classes.

-----------------------------------
TheGuardian001
Thu Jan 29, 2009 11:28 pm

Re: Object Oriented Help
-----------------------------------
yes, it is possible, although doing so would probably be incredibly resource intensive. I believe the only way to do this is through extensive use of modules. It doesn't appear to work with classes, monitors, or types.

module top
    export middle
    module middle
        export bottom
        module bottom
            export hello
            proc hello ()
                put "hello"
            end hello
        end bottom
    end middle
end top

top.middle.bottom.hello()


That program will pop up a warning the first time you run it, however ignoring it allows the program to run properly, and will not bring up a new warning until you change it. I'm not sure how an actual program would respond to this kind of tower implementation. There are probably much better, easier ways to do this.

-----------------------------------
DarkRider
Fri Jan 30, 2009 12:14 am

Re: Object Oriented Help
-----------------------------------
@Amit-

I would recommend reading  if you haven't already done so, they will show how to create a network of classes that will let you easily do what you want.

Based on your psuedo code it looks like your Player class is inheriting your Gun class, which is then inheriting your Bullet class. The "is a" relationship (which Cervantes explains) states that your Player "is a" Gun and your Gun "is a" Bullet. Does it make sense that a Player is a Gun and Gun is a Bullet? No, but a Player can use a Gun and a Gun can use a Bullet, a Player can also use a Bullet. What you need to do is create a method within your Player class that accepts a Bullet class, from there you can access any method Bullet has.

If you did not understand what I explained I would recommend reading the tutoring I linked, as it will explain classes (Object Oriented Programming) more in-depth.

@TheGuardian001-

By adding "var" in front of module exports you can avoid this warning:

module top 
    export var middle 
    module middle 
        export var bottom 
        module bottom 
            export hello 
            proc hello () 
                put "hello" 
            end hello 
        end bottom 
    end middle 
end top 

top.middle.bottom.hello()

-----------------------------------
CodeMonkey2000
Fri Jan 30, 2009 2:08 am

RE:Object Oriented Help
-----------------------------------
@DarkRider the player and bullet class are two separate classes. They have nothing in common. So there is nothing to inherit.

First you should have a bullet manager which can process all the bullets (run each one and pop them when it's done) and add bullets dynamically. When a player shoot they are essentially adding a new bullet to the world. So make the bullet class global, pass it to players when they need it. Also, you should have a base class for players. This class has all the functions common to all types of players (eg. drawing, health etc.). The enemy player and the human play should inherit this base class. 

So your code becomes more like:


%in the player class
if player.Shoot then
         bulletManager.addBullet(attributes of what you want)
end if


-----------------------------------
Amit
Sat Jan 31, 2009 10:53 pm

Re: Object Oriented Help
-----------------------------------
CodeMonkey2000, thanks for the help. While your way is probably better than the one I decided on, it would have meant restructuring my entire program, so I just took the lazy way out.

My original problem was that my player class couldn't access functions from its bullet class. But my gun class can access functions from its bullet class, and my player class can access functions from its gun class. So I just gave the gun class a function that passes along values from its bullet classes into my player class. Problem solved, albeit in a pretty inefficient way. 

If anyone cares, I've submitted the finished program. Go take a look.

http://compsci.ca/v3/viewtopic.php?p=178287
