Thursday, November 5, 2009

Problems I ran into.

Well one of the problems from eariler that I can remember was that I spent at least an hour trying to find out why one of my boolean varibles was marked true when there wasn't one line of code that marked it true! that is, until I found this

if (destroy = true)
{
     endgame();
}

which should of been
if (destroy == true)
{
    endgame();
}

yea, I know I'm never going to forget that again.

Also, I was working on my game today. Which I probably should elebrate more on what I'm making. It's going to be a 2d/open-ended/strategy/turn-based/rpg/simulation. Yup, I'm pretty sure that explains it (lol). Anyways, I have 3 objects which control the majority of the objects in the game. They are the
Input object
Logic object
and Draw object

These three objects are parents of (lets assume) every object. Each object with one (or all) of these as parents will put its pointer into an vector(array). That way I can do this to the main loop:
init();
while ()
{
     for (int i = 0; i < inputArray.size(); i++)
     {
           inputArray[i]->handleInput();
     }
     for (int i = 0; i < logicArray.size(); i++)
     {
           logicArray[i]->logic();

     }
     for (int i = 0; i < drawArray.size(); i++)
     {
           drawArray[i]->draw();

     }

}
return 0;

Now the draw function isn't exactly what you think it is because there is some code im not showing you, but don't worry this is just to get the idea. Any problems I ran into with is was just because I never used vectors before and didn't understand how they work. However, I did run into another problem. My game is openended and simulation, so if I want trees to grow and spread out the game would have to do all the logic for the trees every frame! I solved this today by adding something I'm going to call passive logic. I added code so each object can be "deactivated" (when not on screen). When a object is deactivated it removes itself from the all the arrays and is placed in a "buffer" array. The draw and input functions for the "buffer" arrays are not executed. However, The logic's buffer array is executed, but only parts of it are executed. Hence, instead of doing 2000 trees a frame we are doing 66 trees a frame which is the user is complete oblivious of because 1) its not drawn to the screen and 2) the tree's logic events are random anyways, the delay just adds more to the randomness.

I should post an example of what just explained...

hmm this will be just something quick and ruff

obj parent class

  • this class is mostily for other stuff

logic class
  • virtual void logic()
  • virtual void deLogic() // logic the object uses when deactivated
character class (inherts from logic, input, draw and objparent class)
  • input
  • logic
  • delogic
  • draw
CLogic//constructor
{
     adds this to a global array of object pointers for the logic event
}
~CLogic//deconstuctor
{
    removes self from the global array
}

logic()
delogic()//empty functions to be filled in by child objects

CCharacter
{
   int x, y, power, kills, w/e;
}
input()
{
   press up set direction and speed, ect
}
logic()
{
   go in direction with set speed
}
delogic() //this is the one and only thing that is done when the character is offscreen
{
   rest and recover;
}
draw()
{
   draw(self, x, y, tilesetoffset)
}

This is the main loop:
init();

while ()
{
     for (int i = 0; i < inputArray.size(); i++)
     {
           inputArray[i]->handleInput();

     }
     for (int i = 0; i < logicArray.size(); i++)
     {
           logicArray[i]->logic();

     }
    
    counter = 66;
    while (counter !=0)
    {
           logicArray[position]->logic();
           position++;//this is saved globally so it doesn't erase
           if (position > logicArray.size())
               position = 0;

           counter--;

     }


     for (int i = 0; i < drawArray.size(); i++)
     {
           drawArray[i]->draw();

     }

}
return 0;



If anyone understands what I'm saying have alternative methods then feel free to leave a comment ;)

0 comments:

Isaac's Web Log 2009, template by Ourblogtemplates.com 2008

Back to TOP