Search code examples
coopinstantiationprocedural-programming

How would I 'spawn' or instantiate multiple creatures procedurally?


When I decided to teach myself to program, I started with Java. And although I'm familiar with writing simple procedural software (mostly in PHP), I've recently come to realize that for more complex operations I rely on objects.

My first Java game spawned opponents at random locations and with random speeds by instantiating objects. I am puzzled by the concept of implementing this procedurally in C (which I have very little working knowledge of), but I could only think of creating different arrays for the variables in each opponent and using the array indices as pseudo-object reference 'variables'.

int size[50]; // opponent size
for (i = 0; i < 100; i++) {
    size[i] = random_number();
}

Is there a simple solution that my object-oriented mind has overlooked? Or do certain problems really lend themselves to object-oriented concepts to the extent that you have to make up fake objects (or at least use similar models)? I see structs in C code all the time, and I know they're similar to classes in concept, but that's all I know. So I'm wondering if stuff like this is possible with a totally non object-oriented mindset. There's no real substitute for instantiation, is there? It's all just allocation of memory, so the way I see it, certain C programs have no choice but to be written using some form of what I consider objects.

Please tell me I'm overlooking something, because otherwise I can't respect C purists who berate the object-oriented paradigm. (I'm by no means an OO nut; my mind has become very accustomed to the idea, though, and it's hard to see things through a different lens that doesn't offer what you're used to.)

Thanks!


Solution

  • If we neglect all advanced object oriented programming concepts, as inheritance or polymorphism by type parameters everything boils down to the same thing.

    In C++ you have objects and you call methods that are attached to them, in C you have structs and you call global functions by passing these functions by argument.

    What I mean is that the essential oop paradigm is conceptually to have objects which are able to perform things through their methods. Now in C you can obtain the same behavior but you have functions which are not attached to any object.

    So basically in C++ or Java you would have:

    class Point {
      private:
        int x, y;
      public:
        Point(int x, int y) : x(x), y(y) { }
        void draw() { ... }
    };
    

    In pure C you would have:

    struct Point {
      int x, y;
    }
    
    struct Point makePoint(int x int y) {
      struct Point p = {.x = x, .y = y};
      return p;
    }
    
    void drawPoint(struct Point p) {
      ..
    }
    

    But the semantic power of both representation is essentially the same, the syntax is different though (and even the concept behind it). Of course everything changes if we start considering more powerful oop features.

    In many circumstances you don't need all this power to represent objects, not everything is meant to be oop neither all problems are best solved with an oop approach. If writing a complex engine for a game in C would be overkill at the same time writing low level functions in Java which operates with the system would be overkill at the same time.