//dynamically creates an instance (NOTE: This is the only way to declare an instance)
//create_instance(object type,x,y)
//returns a reference to the instance
template<typename T>
T& create_instance(object& type,int x,int y){
return *(new T(type,x,y));}
...
instance::instance(object& t,int X,int Y){//base instance class constructor
type=t;
startx=X;
starty=Y;
x=X;
y=Y;
gravity=0;
gravity_direction=270;
gravaccel=0;
speed.value=0;
speed.refer=this;
direction.value=0;
direction.refer=this;
hspeed.value=0;
hspeed.refer=this;
vspeed.value=0;
vspeed.refer=this;
if(game.freeids.size()>0){
id=game.freeids[0];
game.ids[id]=*this;
game.freeids.erase(game.freeids.begin());}
else{
id=game.ids.size();
game.ids.push_back(this);}
sprite_index=0;
image_index=0;
image_speed=1;
visible=true;
width=t->image->width;
height=t->image->height;
if(t->create!=NULL){
t->create(this);}}
Basically it just creates a new instance. In the constructor, it initializes some variables, adds itself to a list of instances, and calls the create event (if it exists). I needed it to be dynamically allocated because then you could destroy instances whenever... There are probably better ways to do this, though. Originally, create_instance and all related functions dealt with pointers, but I felt more comfortable using "." than "->," so I started remaking the functions so that they handled references.
You *could* technically delete this elsewhere by deleting the address of the reference, but that is quite a weird thing to do. I would just return a normal object or a pointer.
This was totally, definitely, absolutely not even being close to baring resemblance to the layout of Game Maker instances.
Try making only what you need, really. If you make a platform game, gravity is good to have, but speed is useless for most instances. If you make a topdown game of any sort, gravity is useless, yet speed IS useful. Some instances might not be animated, while still having image_index variables. (Oh, and you forgot image_alpha, _angle, friction and some others (if I recall correctly) :P)
Furthermore, using delete with references can only be used when the reference points to a pointer of some sort. (Which is logical, since a reference is really only a way to point to another object, it's therefore used in the exact same way as the "pointed-to"-object.)
Yes, I based it off of Game Maker, but not everything is implemented yet. Besides, you've only seen 40 lines of code... My engine already has at least a thousand lines of code, up to 200 lines per file, with 10 files. I'm making this as a general-purpose engine, adding functionality as I need it for use later (or sometimes just when I'm bored and thought of a new function).
I'm not sure how to get image_angle working, so if you have any suggestions, they're welcome. Image_alpha I just haven't gotten around to, yet.