To go along with the normal format of these tutorials, hi, I'm Jack. I'm not sure if this is just a concept not explained enough, or if it's just something everyone knows and doesn't need any help whatsoever on. But I want to explore enemy sequencing. This is the sequence things(usually enemies, but obstacles work just as well in this case.) enter the screen, and usually eventually exit. One way or another. I'll be showing this code in GML, the Game Maker Language. GML is very similar to C++, except there are a few differences:
-You do not have to declare variable types
-There are only two variable types in GML, floating point numbers, and strings.
-You cannot declare your own variable types(with, say enumeration.)
-You can freely switch a variable between types, there are no restrictions.
That's it though. With the only other differences being functions are prepared differently, and constants not being freely definable.(There's a little form where you pre-enter constants in.)
Now, first. This code can easily be transformed into C++, but I'll let you do that, since I'll probably mess it up if I try.
The first thing we need to do to set up sequencing is set up a time standard. Let's do that by making a function to convert seconds to steps. This will give us a better time standard, and will be much easier to understand when things are going to happen:
return room_speed*argument[0];
That's it. If you're using C++ it'd be more like
int sc(int x) {
return get_framerate_function()*x;
}
So that's that. From now on I won't be converting my code to C++ standards, just so you know. But that's just to give you the idea. Next we should define some constants for our enemies. I'll let you do that, since I don't know what enemies you plan on having. Then you should make either:
A. One script to create any enemies for you
//Enemy_Create(Enemy, arg, arg, arg, etc.)
var E, ic;
E = argument[0];
if (E == DRONECONSTANT) {
ic = instance_create(objDrone,argument[1],argument[2]);
ic.Arg3 = argument[3];
} else
if (E == OTHERENEMYCONSTANT) {
ic = instance_create(objEnemy,argument[1],-25);
ic.ToY = argument[2];
}
You could do it like that. I wouldn't try this in C++ though, since in GML 16 arguments are always defined, they're just defined as 0 if not specified differently. I recommend doing this if you're doing this in Game Maker. If you're doing this in C++, I recommend creating separate functions for each of your enemy objects. For instance:
//DroneCreate(x,y,Arg3)
var ic, xx, yy, a3;
xx = argument[0];
yy = argument[1];
a3 = argument[2];
ic = instance_create(xx,yy,objDrone);
ic.Arg3 = a3;
And you could just do that when creating an enemy in your sequence instead of the more easily modified script stated above. Either way will work, it's just a matter of preference.
Finally, there are two ways to do your actual sequence. One way is with a series of
if statements, the other is with a switch statement. It's good practice to also keep your separate enemy sequences in separate functions. Have a code for StageSequence1(), StageSequence2(), StageSequence3(), and so on.
Here is an example for the if method:
Step += 1;//Assuming you declared the step variable already in some way, depending on your language.
if (Step == s(5)) {Enemy_Create(DRONECONSTANT,50,150,8);}//s() is the converter script mentioned way back.
if (Step == s(7)) {DroneCreate(50,150,8);}
That's one way to do it. Not so bad, eh? Then there's the switch statement method. It's a bit cleaner, but we need to replace the s() function code to make it work properly.
return argument[0]/room_speed;
So, now, the same code said above converted to the switch method:
Step += 1;
switch s(Step) {
case 5: Enemy_Create(DRONECONSTANT,50,150,8); break;//'5' is for five seconds.
case 7: DroneCreate(50,150,8); break;
}
So that concludes my little tutorial on efficient enemy sequencing. I hope it's helped you in some way, or will help you in the future. If not, well. Sorry to hear that. I really hope I didn't just write a tutorial on something everyone knows, but oh well. Hopefully it will help someone down the line.
G'day and G'night,
-Jack