[Q] How can I apply events to a system-made object instance?

Just started using Construct 2? Post your questions here

Post » Tue Sep 02, 2014 1:23 pm

Hi everyone,

I've been trying to create enemy objects using System -> Create Object, and wondered if there's any way for events to apply to every instance of that object? I've created events that check if you're in range of an enemy and if so, they turn towards you and attack, and I'm trying to figure out how to display the name of each instance and it's speed.

All the below code works for one, manually-placed enemy. I'd like it to somehow apply to any instanced enemy created when the examples shown in the code are triggered, rather than having to copy and paste the code six times for enemy_sml_f1, enemy_sml_f2, etc.

This is my code for this question and any assistance would be greatly appreciated:

Image
Founder of Jadelight Studio, Developer of Libertas Aedifex, regular C2 Help contributor and fan of ambient music and Star Wars.
B
12
S
4
Posts: 255
Reputation: 2,008

Post » Wed Sep 03, 2014 5:04 am

Well technically that code you have there should run against all instances of enemy_sml_f regardless if they are created manually in the layout or using the System -> Create Object.

Are you not noticing any of the code running?

I do notice that you increment enemy_sml_f_count when the enemy spawns but do NOT decrement it when they die.....this might be intentional though.

When debugging, you can use the Debug Layout tools built in construct to see some deeper information or when that isn't enough, I tend to add the Browser plugin to the layout and then use the action Browser -> Log to send my own debug text to the browsers console.
B
33
S
10
G
4
Posts: 51
Reputation: 3,507

Post » Wed Sep 03, 2014 5:32 am

inquiesco wrote:I'd like it to somehow apply to any instanced enemy created when the examples shown in the code are triggered, rather than having to copy and paste the code six times for enemy_sml_f1, enemy_sml_f2, etc.[/img]

You might want to look at "families" object feature.

Your code should be fine, have you tried to spawn them much closer to player to see whether it works. See line3 and make it like random(100,500) or something which is close to your player.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Wed Sep 03, 2014 6:29 am

Thanks for the responses. I think it might have been some bizarre browser caching as this particular issue seems to be fixed now despite nothing having been changed. My Google Chrome sometimes doesn't fully clear despite clearing the cache and restarting the browser. The issue was that the code ran but wouldn't report the object instance names or speeds, but that seems to have miraculously cleared up on it's own... Thanks for the Families information @DuckfaceNinja, that'll be great for grouping entities.

Edit: Removed my side-question as I've figured out how to accomplish it, :).
Founder of Jadelight Studio, Developer of Libertas Aedifex, regular C2 Help contributor and fan of ambient music and Star Wars.
B
12
S
4
Posts: 255
Reputation: 2,008

Post » Wed Sep 03, 2014 7:03 am

inquiesco wrote:For example, if I have 5 light fighters on the screen, they would all be enemy_sml_f but each could be identified as Beta 5, Gamma 16, Iota 2 and such, and be added into the text field? As you can see in my code what I have works but each instance is an identical copy without random names.

You can have a text object with pin behavior, in Line3, create text object and assign enemy_sml_f's IID or UID int text object instance variable (so that you can use it in other event, such as updating log on which enemy is destroyed), pin it to enemy_sml_f, and set text accordingly. I don't think picking is necessary in line3 as created object would be automatically picked, if I'm not mistaken.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Wed Sep 03, 2014 7:13 am

DuckfaceNinja wrote:
inquiesco wrote:For example, if I have 5 light fighters on the screen, they would all be enemy_sml_f but each could be identified as Beta 5, Gamma 16, Iota 2 and such, and be added into the text field? As you can see in my code what I have works but each instance is an identical copy without random names.

You can have a text object with pin behavior, in Line3, create text object and assign enemy_sml_f's IID or UID int text object instance variable (so that you can use it in other event, such as updating log on which enemy is destroyed), pin it to enemy_sml_f, and set text accordingly. I don't think picking is necessary in line3 as created object would be automatically picked, if I'm not mistaken.


Sorry, I didn't quite mean that, :). I have a text object as part of the GUI which tells you which enemy is attacking you, and it reports the target name and speed by grabbing enemy_sml_f.Car.speed and enemy_sml_f_name. The name currently is a static object name they all have, being 'Blade-class Light Fighter' but I'm wanting to have the game randomly pick a designation for the ship. The way I've just thought of doing it is to have two array objects, and to fill one array with the Alpha, Beta etc. designation, and the other will be the Faction. The number designation will be generated through round(random(2,51)) - so it's between 1 and 50 - and then to add those into the instance variable after each is generated by System -> Create Object.

Can you think of perhaps a more efficient method for generating this, if any?
Last edited by inquiesco on Wed Sep 03, 2014 7:20 am, edited 1 time in total.
Founder of Jadelight Studio, Developer of Libertas Aedifex, regular C2 Help contributor and fan of ambient music and Star Wars.
B
12
S
4
Posts: 255
Reputation: 2,008

Post » Wed Sep 03, 2014 7:19 am

inquiesco wrote:Can you think of perhaps a more efficient method for generating this, if any?

You got the right idea on doing that, but in term of efficiency, it's quite abstract, can't really tell which is better which is not, it's all depends on how comfortable it is for you to use it.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Wed Sep 03, 2014 7:24 am

DuckfaceNinja wrote:
inquiesco wrote:Can you think of perhaps a more efficient method for generating this, if any?

You got the right idea on doing that, but in term of efficiency, it's quite abstract, can't really tell which is better which is not, it's all depends on how comfortable it is for you to use it.


Thanks for all the help, it's been a good discussion. I might end up pinning text above the AI anyway, that's a good idea for an item upgrade. Yeah, it's the only way I could think about doing it using my past web development experience, ha ha, I don't know if there are more efficient methods in OOP languages as I've never learned an actual non-web language yet.

A moderator can close this now as the particular issue in the OP has been resolved.
Founder of Jadelight Studio, Developer of Libertas Aedifex, regular C2 Help contributor and fan of ambient music and Star Wars.
B
12
S
4
Posts: 255
Reputation: 2,008


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests