What would be the best way to load enemies in a level. For example take Mario, are enemies rendered at their x,y positions as the game is loaded or when the player walks past a specific x value. Wouldn't this mean that you would have multiple if conditions checking whether the player has reached a specific x value to spawn an enemy; this doesn't seem like a very good solution to this problem at least to me.

What would be the recommended way?


The originals absolutely loaded the enemies as you got close to them, as loading all enemies at once was likely beyond the memory constraints of the older gaming hardware. A lot of crazy and now unnecessary tricks were used back then to minimize memory usage.

Today, I would still opt to loading them on demand as you neared their spawn locations, as it gives more flexibility. Also, you'll notice that in games like Mario, the enemies always appear in the same place moving the same way; so even if you loade them in advance, you'd have to disable their movement until Mario got close enough.

There should be no question about the number of "if" statements, as you should absolutely not be writing code to spawn specific enemies on a specific map. Write a data file to describe your level and enemy spawn points, and then write a single generic block of code to check to see if Mario has gotten close to any unspawned enemies from the data list.

As much as possible, make everything data driven. Hard coded logic makes development slower and harder and, as you pointed out, just isn't a good solution to this problem.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.