Selecting objects based on their name

Get help using Construct 2

Post » Mon Jun 11, 2012 4:32 am

Hi,

I have a variable with which I'd like to be able to select different kinds of objects based on what's specified. Is there any way to target objects based on their name?
B
22
S
5
Posts: 107
Reputation: 2,354

Post » Mon Jun 11, 2012 9:48 am

Maybe create a name variable for that objects, and then write:
if object.variable == "name" to choose that object
B
25
S
5
G
1
Posts: 46
Reputation: 3,347

Post » Mon Jun 11, 2012 10:28 am

That's what I'm trying to do, but how do I do that? I can't find any relevant event triggers.
B
22
S
5
Posts: 107
Reputation: 2,354

Post » Mon Jun 11, 2012 2:22 pm

To help obfuscate your project so it is difficult to reverse engineer, all object names are removed from the project when you export it. This is why there are no features to use the object names: they are not there after export.

The best workaround is to add an instance variable which you manually set to the object's name, then access that.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Mon Jun 11, 2012 3:56 pm

That's a good idea, but how can I access those instance variables then? It doesn't seem like I can do a foreach for every sprite object that exists in my game, or can I?
B
22
S
5
Posts: 107
Reputation: 2,354

Post » Mon Jun 11, 2012 4:19 pm

yeah you can actually...

add a For Each loop then pick your object you want to loop thru then inside the loop do a Compare Instance Variable and see if its Name instance variable (that you added to the object) matches the one your lookin for.

I do this all time. Its how I avoid adding multiples of the same object like Text objects. I just add one and add a Tag instance var then use that tag to set the text for specific ones
B
13
S
5
G
2
Posts: 306
Reputation: 3,262

Post » Mon Jun 11, 2012 5:11 pm

Your example would only compare the loop to *one* object.

I want to compare a value with the instance variable of ALL objects, until i find a match. :-)
B
22
S
5
Posts: 107
Reputation: 2,354

Post » Mon Jun 11, 2012 6:42 pm

this example shows you how to compare multiple objects by varname

by looping tru a family of objects and comparing the namevar

comparename

does this help?
ImageImage
B
61
S
19
G
6
Posts: 809
Reputation: 9,028

Post » Mon Jun 11, 2012 6:58 pm

It does. Thanks a lot! :-)
B
22
S
5
Posts: 107
Reputation: 2,354

Post » Thu Jul 26, 2012 7:04 am

@Ashley:
In which way are the objects renamed on export? Is there some kind of pattern used? Are the same objects always renamed the same way on export?

The reason why I'm asking is that I've created a level editor that saves the sprites using the SpriteBank-plugin. The plugin uses sprite names to reference to the sprites when saving, and if the naming changes when you re-export the project (for updates, for instance) it will mess up previous level saved.

This is not a problem you should worry about - I can replace the names in the save file manually myself, but it would make my life easier if you could tell me how the renaming system works.

Thanks!
B
20
S
5
G
4
Posts: 51
Reputation: 4,139

Next

Return to How do I....?

Who is online

Users browsing this forum: YoHoho and 15 guests