• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Enemy System Help

A

AtomicAstro

Guest
Alright, so I'm making a bullet hell schmup at the moment, and this isn't so much a "how do I do this" so much as asking for a bit of advice on something. Originally I was going to have different enemy types be different objects, but then I got an idea where I can just have a single enemy object and a switch statement that will assign different scripts depending on the assigned enemy type which will determine the sprite, functionality and everything. I also intended to do this for stage bosses, where there is one single boss object which picks a script based on which boss it is. However, in trying to keep it simple that way, it's actually made things a bit more complicated. The biggest problem I'm having is that if I set up a variable within the create event, I have to assign it a value right away. So, for instance, if I establish a variable for "enemy type" I have to give it a value, which I just set to a basic enemy.

What I normally do when spawning the enemies (since I plan for there to be a set pattern for enemy spawning, not randomly generated) is that I have an enemy generator object which constantly counts down on a timer equivalent to the length of the stage, and on specific frames it will spawn the enemy object, and set the variable "type" of that enemy to whatever enemy type I so choose. Since I have the switch statement in my step event, it will see the enemy type and run the script. But the problem is, it takes a split second for it to recognize as that particular enemy type, since by default I have to set the enemy type within the create event to something. It's been immensely frustrating trying to get this system I thought of to work, and I've been constantly racking my brain trying to create some Frankenstein's monster out of this system trying to get it to work the way I want it to.

So, that got me thinking. Since this is not an extremely large-scale game, I'm planning it to be a shooter with 6 stages or so, should I just go the simpler route and have different objects for different enemy types, or should I continue with this system and try to make it work? I didn't intend to ask around about this, since I normally prefer to solve things on my own if at all possible and detest the idea of pestering forums for solutions, but I really have nothing here. Any help is appreciated.
 

Rob

Member
A solution would be to do a check (with a Boolean/existing variable) to see if the code for changing the Sprite has already run and only then draw the instance.

If you are assigning the sprite when the instance is created then I would expect the original image to be overwriten though.

It sounds like the problem is that a draw event happens for that instance before its Sprite is updated and that's why you can see the original Sprite for a step.
 
R

robproctor83

Guest
There are a few ways you could resolve this, but probably the simplest and most straight forward way is to just make that base enemy have a type = noone. So, when you first generate an enemy is has essentially no type, then in your code only do something if type != noone. Same thing with your draw event, make sure you don't draw it until after type is set (noone is not set). Use noone for things like that, it's very useful.

Alternatively, you could make a custom instance_create wrapper script that passes variables to the create event, and then you could define things directly from the create rather than having to wait for it to initiate from a timer or from the first step. To pass variables to the create you have to define a global before hand, and then in the create you reference the global. Take a look here for more details, it's very easy and very very useful

https://yal.cc/gamemaker-create-event-arguments/
 

TheouAegis

Member
Don't have any enemies appear at the start of a room. That avoids
the whole draw event issue.

Either use a moderately complex State machine in the step event where state zero is where you initialize variables and sprites and all that stuff, or that's why I suggested, just check if a variable is set yet to then run initialization code. If you create an instance in the begin step or regular step event and have your switch inside the step event, as well as making sure the object which is going to be spawning the enemies in is located higher up in the resource tree then the enemy object, inanimate object will always run its code after it has been created.
 
Top