Creating game with classes' instances

Hello!
I am trying to do my first C++ console game based on objects. I have been working in GM(Game Maker) until recently so I have quite good idea about what classes are for and what are instances.
On the other hand: now, with no Interface like GM has, I have problem declaring them. See, in GM you create objects clicking one button, write what they are supposed to do every tick (called Step). And then you just place them in a room and you can play the game.
Now I have tried to do something that works similliar to that. I have created class Object, that has children - class Ball, class Monster, class Brick..; I'd have another class called Comp, that would take care of everything those classes can't + class Drawer, that the classes can use to write out their variables in the console.
So, every class under Object class has it's X and Y. And let's say the balls should fall. So that every step they execute the piece of code: Y++;
I have clear idea how to do that with one ball.

1
2
3
4
5
6
Comp::Comp() {
Ball objBall;
}
Comp::Step() {
objBall.Y++;
}

Now what if I want to have 2 balls. What if I want to have thousands of balls.
I've searched for the solution online and I have tried my own solution and I ended up on two things.
1.
I have tried to create an array of classes or class pointers and neither of them works. You know, I'd use them to run the Step event of every array member - every instance; Like this:
1
2
3
4
5
Ball Instance[1];
Ball Instance[2];
for (i=1; i<3; i+=1) {
Instance[i].Step();
}

Errors:
lines: Ball Instance[1]; and Ball Instance[2];
"missing ';' before identifier"
"missing type specifier - int assumed"

2.
Passing a class name throught function. So that I don't have to create Creating function for each class.
InstanceCreate(int X, int Y, class Obj);
that and
1
2
3
4
void InstanceCreate(int X, int Y, class Obj) {
Obj Instance[1];
Instance[1].X=X;
}


Errors:
line: InstanceCreate(int X, int Y, class Obj);
"incomplete type is not allowed"

Obviously, my approach is entirely off. Could you please push me slightly the right way or send me link to a tutorial regarding this problem?
Thank you for your help. :)
Your first solution would be fine (in theory) if you had declared the type for `i', you knew that arrays in C++ start at 0 instead of 1 (declare an array of 3 items: item 0, item 1 and item 2), and you didn't create three instances of Ball objects. Try this:
1
2
3
4
5
Ball Instance[2]; //two balls
for (size_t i = 0; i < 2; ++i) //arrays start at 0; C++ has a ++ prefix operator that comes in handy instead of doing i += 1
  {
    Instance[i].Step ();
  }
Thank you for your reply, chronokitsune :)
Unfortunately the line
Ball Instance[2];
itself gives me those errors:
Error 4 error C2146: syntax error : missing ';' before identifier 'Instance'
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Edit:
But only if I add some piece of code, that tries to access the instances Instance[] in some way. Like the piece of code you sent me does. I it weren't for that for, I could build an application with no such errors. But I need to get that for working somehow :)

Oh and I have "Ball.h" with the declaration of class Ball included alright.

I work in MS Visual C++.
I'll keep in mind to use index 0 in arrays.
Last edited on
I think you might be making this a little too complicated. Below is a simplified version of what you're doing. I did not create a Comp class or any other item except a Ball class with specific usage -- you might try adapting it a little bit to see if you can make it work for you instead of redoing everything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

class Ball
{
  int X;
  int Y;
public:
  Ball () : X(0), Y(0) {}
  Ball (int x, int y) : X(x), Y(y) {}
  void Step ();
  void ShowPosition ();
};

void
Ball::Step ()
{
  ++Y;
}

void
Ball::ShowPosition ()
{
  std::cout << "(" << X << ", " << Y << ")" << std::endl;
}

int
main ()
{
  Ball Instance[2];
  
  for (size_t i = 0; i < 2; ++i)
    {
      Instance[i].Step ();
      /* Every other Ball instance will call Step() a second time
	 instead of once.  */
      if (i % 2 != 0)
        Instance[i].Step ();
    }
  for (size_t i = 0; i < 2; ++i)
    {
      Instance[i].ShowPosition ();
    }
}
Your example is working perfectly for me, thank you. :) I don't get it though, probably declaring classes like that is not allowed in the declaration of other class?

Thanks again for all your help chronokitsune, I appreciate it.

Now I could solve the second problem using only one class Object, that chooses which actions in Step to take depending on the variable "TypeOfObject" that I pass to it(Ball, Brick, Monster..). They'll ale have to share the variable names, so for variables that only one type of object will use I'll have to create some arrays like int GenericInteger[30] and string GenericString[30] . .then write down, what each index for each type of object means.
So, if anyone knows about a better solution, please let me know :) Until then I'll try to get that idea of mine working.
Last edited on
Topic archived. No new replies allowed.