Organizational discussion

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Organizational discussion

Post by albinopapa » February 14th, 2016, 3:53 am

Code: Select all

class Enemy
{
    float x,y;
    int hp;
    static int color;
    static int level;
}
if you add static in front of the variables you want to carry over to all enemies, then when you change the value, all instances of enemy will use the same values and you would be able to do

Code: Select all

enemy->color = new_value;
enemy->level = new_value;

// which is the same as
enemy[0].color = new_value;
enemy[0].level = new_value;
//but since all instances will have the same value, it doesn't matter, the latter method though makes it look like it's only affecting the first enemy, so I prefer the first method.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

adabo
Posts: 154
Joined: October 27th, 2012, 3:28 am
Location: Houston, Texas

Re: Organizational discussion

Post by adabo » February 14th, 2016, 3:19 pm

Thank you very much! I look forward to experimenting with classes now.
albinopapa wrote:[...]the latter method though makes it look like it's only affecting the first enemy[...]
Interesting. I thought [0] actually is the first instance of the element. Correct me if I'm wrong, but are you saying that I can change all instances of a class array by changing the [0] element?

adabo
Posts: 154
Joined: October 27th, 2012, 3:28 am
Location: Houston, Texas

Re: Organizational discussion

Post by adabo » February 14th, 2016, 4:13 pm

Ok, I'm failing at getting the class to work. Can you see anything wrong with this?:

Code: Select all

class Game
{
private:
    class Enemy
    {
    public:
        float x, y;
        int hp;
        static int color;
        static int level;
    };
public:
    void ComposeFrame();
private:
    Enemy enemy[10];
};

void Game::ComposeFrame() {
    enemy->color = 255;
    enemy->level = 100;
}
I think I'm supposed to "initialize" the static members?

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Organizational discussion

Post by MrGodin » February 14th, 2016, 7:47 pm

in game .cpp file go
int Game::Enemy::color = whatever you want to initialize it with
int Game::Enemy::level = 1 or 0 whatever you like
Curiosity killed the cat, satisfaction brought him back

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Organizational discussion

Post by MrGodin » February 14th, 2016, 8:11 pm

I then believe in Compose frame you'd go (or whatever function/method handles data changing of the enemies) Game::Enemy::level = new level.
I use setters to do this as i noted in my original post on this subject. I understand wrapping your head around classes can be difficult at first, but when you get it, it's quite powerful.
I might also suggest making a separate cpp and header file for your enemy then add the Enemy.h file to game.h (#include "Enemy.h"). You can then have more freedom to pass enemy around to different classes. (something you'll do when you understand classes more). Like passing gfx ( a class it's self) to game for instance.
Just a thought for future reference.
Peace Out
Curiosity killed the cat, satisfaction brought him back

adabo
Posts: 154
Joined: October 27th, 2012, 3:28 am
Location: Houston, Texas

Re: Organizational discussion

Post by adabo » February 14th, 2016, 8:21 pm

Thank you, MrGodin. This forum is a REFUGE, wow. So glad it exists. What you said helps me a lot. Yes, I figure once I do wrap my head around the classes it will be very powerful. Again, thank you.

Post Reply