Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Object Oriented Help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Amit




PostPosted: Thu Jan 29, 2009 10:57 pm   Post subject: 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:

Turing:

player.gun.bullet.procedure


Is it possible to do this, and if so what is the syntax?
Sponsor
Sponsor
Sponsor
sponsor
DanielG




PostPosted: Thu Jan 29, 2009 11:08 pm   Post subject: 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




PostPosted: Thu Jan 29, 2009 11:28 pm   Post subject: 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.
Turing:

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




PostPosted: Fri Jan 30, 2009 12:14 am   Post subject: Re: Object Oriented Help

@Amit-

I would recommend reading Cervantes' class tutorials 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:

Turing:
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




PostPosted: Fri Jan 30, 2009 2:08 am   Post subject: 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:
Turing:


%in the player class
if player.Shoot then
         bulletManager.addBullet(attributes of what you want)
end if
Amit




PostPosted: Sat Jan 31, 2009 10:53 pm   Post subject: 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: