We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › ecosystem with extended classes
Page Index Toggle Pages: 1
ecosystem with extended classes (Read 1115 times)
ecosystem with extended classes
Dec 27th, 2005, 5:19pm
 
Merry Christmas!
I hope some of the following makes sense - I've not been programming long, and I'm totally self-taught, so I might be making some idiotic mistakes. Here goes...
I am trying to make an ecosystem with 5 or 6 different classes of organism. I've made this one, which works (more or less) with 3 different classes in it.

http://www.scanraid.com/speakingincolour/threeOrgIndex.html

So far so good.
I'm not happy with the way my code is structured at the moment as I think I may run into problems as the system grows. The basic problem is writing reuseable methods for all the classes.
All  classes use more or less the same methods, so to start with I structured the code something like this:

class Org{
 Org(){
   //bla bla
 }
 void birth(){
   //bla bla
 }
 void die(){
// bla bla
 }
 void hunt(){
 //bla bla
 }

 //etc....then:

 class Plant extends Org{
 int itsClass;
   Plant(){
     itsClass = 1;
     //bla bla
     }
 }


 class Prey extends Org{
 int itsClass;
   Prey(){
     itsClass = 1;
     //bla bla
   }
 }

//etc....

}
//------------------------------------------------------------------------------
-

I gave each extended class a variable (int) so it knew whether it was a plant, prey etc. and called the same methods for all organisms. The problem with this was that I couldn't work out a way of putting together the name of an object as a variable value -  eg for the birth method I wrote something like

void birth(){
 string objString;
 int itsMax;
 class thisClass;

 switch(itsClass){
   case 1:
     //maxPlant is the length of the array holding all the plant names
     itsMax = maxPlant;
     objString = "plant";
     //not sure I can do next line
     thisClass = Plant();
     break;
   
    case 2:
     itsMax = maxPrey;
     objString = "prey";
     thisClass = Prey();
     break;

 //etc...
 }

 for(int i =0; i<(itsMax-1); i++){

//then here is where I ran into real difficulties. I need to concatenate objString ("plant", "prey" etc.) with the value of  i to create the name of a new object. Something like:

   if(classString[i]== null);{
     classString[i] == new thisClass;
     break;
   }
 }
}

//------------------------------------------------------------------------------
--

There doesn't seem to be a method for turning strings into useable variables. Also, I can't work out how to refer to the name of a class rather than a specific instance ( I'm sure class thisClass = Plant(); can't be right!). I ended up with pretty much the entire method written out repeatedly for each case and there didn't seem to be much point to having global methods at all, so I got rid of class Org and had all the classes functioning independently with local methods, which is how the program is now.

If I use almost identical local methods throughout will it slow up the program?
Is there a way to solve the problems so that I can use extended classes?

I'd be very grateful for any help on this, and also for any general comments or suggestions.
(by the way, I'm using P3D because I can't get OPENGL to work on my stupid graphics card)

thanks
Re: ecosystem with extended classes
Reply #1 - Jan 8th, 2006, 11:23pm
 
Four classes in my system, and I'm still rollin' along! (mind you, it is a bit crashy):

http://www.scanraid.com/speakingincolour/four_org_systemP3D/applet/index.html

plants (jiggly orange blobs), herbivores(purply-blue things), and predators (green meanies) are basically finished. Scavengers - the red blobs at the bottom which mutate into red squares, eat everything and die of indigestion - will be totally re-written. The brightness of the background is determined by the amount of spare "energy" in the system, and its colour changes depending on the success of the various classes, according to a bit of (possibly dodgy) maths lurking in the code somewhere.
Said code is very messy and as yet not commented out, so I don't expect anybody to waste time trying to make sense of it, but I'd still greatly appreciate some general advice on writing reuseable methods, extended classes and passing the names of objects as values.
I'd also appreciate any comments on the project as a whole, as I have to hand in a version at college in a couple of weeks.
I have a much smoother, faster and slightly more stable OPENGL version running on my PC, but still can't get it going on my laptop, as the OPENGL won't work properly on it.
Page Index Toggle Pages: 1