Sign in to follow this  

best way to load different prefabs per level?

Recommended Posts

Each level (8 in total) in my game has a different enemy prefab. I made it work with the following code, but having a huge if else chain doesn't seem very efficient. Any advice for a better way to do this? 

if (this.currentLevel === 1) {
    this.blade1 = new TwoBlades.Blade1(, * Math.random(), 0);
    this.blade2 = new TwoBlades.Blade1(, * Math.random(),;
    this.enemySpeed = 150;
    this.enemySpeed2 = 200;

} else if (this.currentLevel === 2) {
    this.blade1 = new TwoBlades.Blade2(, * Math.random(), 0);
    this.blade2 = new TwoBlades.Blade2(, * Math.random(),;
    this.enemySpeed = 200;
    this.enemySpeed2 = 150;

I also did the same thing in another function, changing text and images in the next level description modal after each level

else if(this.currentLevel === 4) {
    this.nextLevelName.text = 'Growth Spurt';
    this.nextLevelAbilityName.text = "Grow";

} else if(this.currentLevel === 5) {
    this.nextLevelName.text = 'Ludicrous Speed';
    this.nextLevelAbilityName.text = "Speed Burst";


As a side note, if I have two prefabs that do the exact same thing, but one is slightly different with some extra functions, do they have to be split into two prefabs or is there a way to use a prefab without using all it's functionality?

Share this post

Link to post
Share on other sites

Yes, I have done that. My question was on how to properly call them based on the current level, rather than having if/else statements based on the level variable. I'd imagine I couldn't do this if a game had many levels.

Share this post

Link to post
Share on other sites

My game Blaster has a very simple file format for describing each wave of the game. The game is built using webpack, so I just "require" the YAML file right in:

Each "g" is a "grunt", each "e" is an "enforcer", etc. Here's the plugin that uses that data: You can see in the method "loadCurrentWave" that there are a series of for-loops that build all the things.

This system could have been made MORE generic by making a map of "property in level data" to "Classname of thing to spawn" and then iterated over the keys of the map and run the for loop. e.g.:

var waveEnemies = {
  'g': Guard,
  'e': Enforcer

Object.keys(waveEnemies).forEach(k => {
  const clazz = waveEnemies[k];
  const thing = new clazz(,,;

Something like that.

You want this to be more data driven, essentially. Then you're not reaching in to some huge if statement to change things, you're just changing a data file. You could put the text in there, too.


Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.