How do I reduce redundant/repeated enemy code?

Get help using Construct 2

Post » Wed Jun 28, 2017 4:57 pm

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:

Image

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.
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Wed Jun 28, 2017 5:29 pm

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.
B
46
S
15
G
73
Posts: 2,029
Reputation: 43,465

Post » Wed Jun 28, 2017 5:55 pm

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.
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Wed Jun 28, 2017 6:17 pm

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.
B
46
S
15
G
73
Posts: 2,029
Reputation: 43,465

Post » Wed Jun 28, 2017 6:30 pm

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?
Last edited by CJacobsSA on Wed Jun 28, 2017 6:38 pm, edited 2 times in total.
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Wed Jun 28, 2017 6:37 pm

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.
B
46
S
15
G
73
Posts: 2,029
Reputation: 43,465

Post » Wed Jun 28, 2017 6:39 pm

I'll give that a try, then. Thank you for the help! :D
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Wed Jun 28, 2017 8:13 pm

plinkie wrote: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.
B
198
S
111
G
7
Posts: 134
Reputation: 19,689

Post » Wed Jun 28, 2017 9:50 pm

@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.
B
46
S
15
G
73
Posts: 2,029
Reputation: 43,465


Return to How do I....?

Who is online

Users browsing this forum: Allardje, Rudi55, sublimate and 2 guests