Organizational discussion

The Partridge Family were neither partridges nor a family. Discuss.
adabo
Posts: 154
Joined: October 27th, 2012, 3:28 am
Location: Houston, Texas

Organizational discussion

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

Please bare with me, guys. I don't mean to spam the boards, so maybe this thread could consolidate my concerns.

In my game, I made a struct for "Enemy". Then I'll declare the struct for it:

Code: Select all

private:
struct Enemy{
    int hp;
    int x;
    int y;
};
private:
    Enemy enemy[ MAX_ENEMIES ];
This works fine. My concern is that certain properties I want track and change things globally for the enemies. For instance their velocity, color, count.

For the time being I decided to make a 2nd global struct called:

Code: Select all

Global_Enemy{
    velocity;
    color[ 3 ];
    count;
};
I really wanted to keep all the enemy properties in one struct, but it doesn't make sense. Because when I make an array of enemies like the earlier above example, then I can't keep a global property in there since each variable is different for each element in the array.

Is any of this making sense? Am I making things more complicated than necessary? I love the concept of structs because it keeps all relevant variables together, but I'm puzzled what to do when I need to change everything all at once without for-looping every time.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Organizational discussion

Post by cameron » February 13th, 2016, 6:52 pm

You could store a ptr/shared ptr to a GlobalEnemy within the enemy struct, if all the enemies in the array have those properties. You could either heap allocate the enemy of store it on the stack and on creation of enemy take a ptr to global enemy. Once you have that implemented you could create some kind of interface for creating and linking enemies to global enemies if you'd like.

I mean you could do what you were suggesting and even clean it up with inheritance, but that could also waste a lot of memory and time to change all the instances of the values.

Just a thought.
Computer too slow? Consider running a VM on your toaster.

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

Re: Organizational discussion

Post by MrGodin » February 13th, 2016, 6:54 pm

if I think i understand what you're trying to say is you want to change a variable for all enemies at once ?. say color ?. If this is the case you make the color variable static, say
Header file

Code: Select all

class Enemy
{
private:
static int color; 
public:
 Enemy();
static void SetColor(int val);
}

CPP File

Code: Select all

int Enemy::color = 255;// default

Enemy::Enemy(){}
void Enemy::SetColor(int val){color = val;} // will set ALL enemies color at once to val.
You then go somewhere in your code, Enemy::SetColor(128);
I may be off base of what you are wanting to do, but this is one way of changing a variable for all entities of type enemy
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 13th, 2016, 7:55 pm

Thank you, guys! Unfortunately I lost my last message I typed. Guess I closed the browser

Basically, I'm just letting you know I'm still in the beginner series and don't know about the c++ things yet. I didn't comprehend everything that was mentioned

MrGodin: I think that might be what I'm looking for, but I've never created a class before. I'm only using structs. If a class would be a better way of controlling everything then I would gladly implement it :)

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Organizational discussion

Post by albinopapa » February 13th, 2016, 8:12 pm

class and structs are pretty much the same with a couple of exceptions.

Structs have, by default, all public access members and methods. That is why you can do this
struct Enemy
{
float x, y;
};

void SomeFunc(Enemy &En)
{
En.x = 10.0f;
}

with classes, by default, members and methods are private.

class Enemy
{
float x, y;
};

void SomeFunc(Enemy &En)
{
En.x = 10.0f // Error, x is inaccessible.
}

You can make members and methods of classes public like so

class Enemy
{
public:
float x, y;
};

now Enemy acts exactly like a struct.

The purpose of a class though is to have only the methods (functions) that other functions need to interact with public, and the members (the data) and other methods set to private.

class Enemy
{
// Since private is the default, some like to declare the members first
float x, y;

// Then declare public functions like the constructor
public:
Enemy();
Enemy(float InitX, float InitY);

void Update(float NewX, float NewY);


// You can also go back to private, this means that this function can only be called from other function
// in this class and not from functions from other classes like Game::Go
private:
void ChangeColor(unsigned int Color);
};
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

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Organizational discussion

Post by albinopapa » February 13th, 2016, 8:13 pm

Oh, and I was going to suggest using static members over a globals also.
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

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Organizational discussion

Post by cameron » February 13th, 2016, 8:32 pm

I was under the impression that he wanted multiple types of enemies, in such a case static members would not work.
Computer too slow? Consider running a VM on your toaster.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Organizational discussion

Post by albinopapa » February 13th, 2016, 10:36 pm

Yeah, I was thinking about that after I made the post. There are going to be instances where you'd want to have a global vs a static member. Thanks for pointing that out.
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:11 am

albinopapa: So if I make an array of class enemies, I could change all of the members data at once?

To elaborate, I have the class Enemy:

Code: Select all

class Enemy
{
    float x,y;
    int color;
    int level;
    int hp;
}
Then I make an array of enemies from the class:

Code: Select all

 Enemy enemy[ 100 ]
Normally, I'm updating coordinates and hp individually. However, for color and level I want to change globally. For now I just use a for loop:

Code: Select all

for( int i = 0; i < 100; i++)
{
    enemy[ i ].color = new_value;
    enemy[ i [.level = new_value;
}
What I would prefer is to just change all those values without the for loop in 2 lines like this psuedo:

Code: Select all

enemy.color = new_value;
enemy.level = new_value;
I hope that clears it up. From what I've read from you albinopapa, I'm not familiar with. I'll need to watch the lessons about classes soon.

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

Re: Organizational discussion

Post by adabo » February 14th, 2016, 3:14 am

cameron: Not necessarily different types. Just different instances all with the same properties, just with different values depending on location and health. There are multiple enemies on the screen at once.

Post Reply