How do I reduce redundant/repeated enemy code?

0 favourites
  • 9 posts
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • Hello, I'm looking for a bit of help with keeping my code from having to be repeated multiple times (possibly using Families?). My game is going to have multiple kinds of enemies, and different types of projectiles the player can fire, with a few shared variables between all of them. Every bullet has a 'DamageDealt' instance variable, and every enemy has a 'CurrentHealth' variable, and so on. The way I have it set up now is as such:

    The problem is that I don't know how to avoid repeating this code for each type of bullet and then repeating that for each type of enemy. How would I go about doing that? I tried using families, but I can't figure out how to address the instance variables of something in the family, only the family itself.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This is pretty much what families are for, you can combine all bullets into a Bullets family and all Enemies into an Enemies family. Then you have family instance variables so a bullet family damagedealt variable will be available on all individual bullets.

  • I see! How would I go about applying different values to different bullet/enemy types? For example, the same kind of bullet comes out of two different guns, but I want them to do different damage amounts. Similarly, enemies in the same family having different health values.

  • Although the family instance variables are used and apply to all family members, they can be set separately. So if you have a family instance variable BulletDam, it will appear on all bullets as a variable but Bullet A can be set to 10 and Bullet B can be set to 20.

    So you set the individual bullets A and B up on a sheet, where they have their default values for BulletDam. Then when you do the general event if Family Bullet is overlapping Family Enemy, subtract Family Bullet.BulletDam from Family Enemy, in just one event you can set up something where any bullet overlapping any enemy will subtract its own damage from the enemy. I think that's what you wanted, in just one event.

  • Interesting, that makes sense. Is there a way to do it so that I only need one bullet object for A and B? Or will I need to create a new object for each one?

  • Well in theory you could not use families, create one sprite object called bullet and constantly edit its variables and animations during runtime but that's going to be incredibly confusing and not the way I would do it. You need the separate bullet types as objects, you can leave them all on a layout with their default values. Then you group the general events like being hit and taking damage using families. This then allows you to also call upon the separate bullet types in some events if it calls for it.

  • I'll give that a try, then. Thank you for the help!

  • Although the family instance variables are used and apply to all family members, they can be set separately. So if you have a family instance variable BulletDam, it will appear on all bullets as a variable but Bullet A can be set to 10 and Bullet B can be set to 20.

    So you set the individual bullets A and B up on a sheet, where they have their default values for BulletDam. Then when you do the general event if Family Bullet is overlapping Family Enemy, subtract Family Bullet.BulletDam from Family Enemy, in just one event you can set up something where any bullet overlapping any enemy will subtract its own damage from the enemy. I think that's what you wanted, in just one event.

    Can you provide an illustration of this using pictures, like the tutorials? This was something that I tried to set up but I wasn't sure how to get the individual instance variable values to work properly. I'm not sure how this is to be arrange in an event sheet.

  • YoHoho I've made a capx here, it's one event. https://www.dropbox.com/s/71fhi1q6g1y63 ... .capx?dl=0

    The other stuff is just to spawn random bullets so you can see the damage being taken off the enemy.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)