How do I know which instance has which IID?

Get help using Construct 2

Post » Mon Jan 09, 2012 9:01 am

Thanks for bearing with me, folks!

I take it from what you say is that there's no way in Construct 2 of directly doing what I can do in JavaScript with " 'centreCat.favorite = true' ".

And now that I've confirmed (I think!) that there's no direct and simple way of doing this that I've missed, I'll work on giving a fuller example, together with what seem to me to be the workarounds that you've suggested.

I'll be back and hope you can bear with me a bit longer :)Velojet2012-01-09 09:02:53
B
105
S
20
G
12
Posts: 549
Reputation: 20,320

Post » Mon Jan 09, 2012 6:29 pm

Demo capx

Run as it is, you see no text in the layout.
Go in the event sheet, right click on any group, set as active it does pick the black cat.
It does since in the IDE, I've setted the animation frame to be singular, an instance variable number and an instance variable boolean to be singular (there's only one black cat (anim frame = 1), favorite=true, fave = 1).

This is not a workaround, this is how events work.


At least that's the setting and the solution how I understand it from everything you described so far.Kyatric2012-01-09 18:31:29
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Mon Jan 09, 2012 7:23 pm

@Kyatric Lovely work ! Mille remerciements !

I'm going to have a close look at your demo later today (8:20am here in NZ, and I have to go out on a job now).

Your demo capx gives me a much better way of explaining my query and then going on to look at possible solutions (including yours, of course).

I really appreciate the attention and effort you're giving to this issue.
B
105
S
20
G
12
Posts: 549
Reputation: 20,320

Post » Mon Jan 09, 2012 11:11 pm

Here're a couple of demos of my own. First, a reminder of what I'd like to do:

To have a means of identifying each particular cat at the point of placing them on the layout.

I can do that in JavaScript (or any OO language) by giving each a unique instance name e.g. 'centreCat'.

I can't see any way of doing that in Construct 2. That's why I call any way of achieving the same outcome a 'workaround'.

This capx shows one workaround (following suggestions by Kyatric and Yann) - and one example of why I need to have a means of identifying a particular cat at the point of placing it on the layout. (Thanks again to Kyatric for his graphical interpretation of my verbal description!)

When the game is reset (e.g. when the game is over and we want to play it again), I need to revert to making the centre cat (black) the favourite, by setting its 'favourite' Boolean instance variable to 'true'.

Hence my need to be able to identify that cat at the point of placing it on the layout. And hence this workaround:

Give each cat an additional Boolean instance variable: 'colour'. When resetting, use the 'colour' Boolean to pick the centre cat and set its 'favourite' Boolean to 'true' (having reset all the others to 'false').

This capx shows another workaround (following suggestions by Ashley and Yann): At the outset, I save the centre cat's UID in a global variable 'CentreCatFavourite'. When resetting, I pick that cat instance with its UID and set its 'favourite' Boolean to 'true'.

Thanks again for bearing with me!Velojet2012-01-09 23:14:33
B
105
S
20
G
12
Posts: 549
Reputation: 20,320

Post » Mon Jan 09, 2012 11:32 pm

If you set the instance variable of a particular instance in the property panel at edit time, each time you reload the layout the instance variable of this object will be reset to this value.

So if you always want the center cat to be selected on start of layout, you just need to set it at edittime.

Didn't look at your capx though, but if I understood your post well, all your questions have been answered?
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Jan 09, 2012 11:41 pm

New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Tue Jan 10, 2012 12:40 am

B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Tue Jan 10, 2012 2:52 am

Thanks again, Kyatric and Yann. Shock bis wins on conciseness!

At first glance though, it does indeed look as though each cat requires that additional Boolean instance variable that I gave it ('colour' in this example) so that, when resetting, it can be used to pick the centre cat and set its 'favourite' Boolean to 'true' (having reset all the others to 'false').

Out on another job now, however, so I'll study your demos more carefully tonight and come back with further comments.

B
105
S
20
G
12
Posts: 549
Reputation: 20,320

Post » Wed Jan 11, 2012 8:27 am

@Kyatric, @Yann: Thanks immensely for so patiently and promptly sharing your knowledge! You've helped to clarify a number of issues concerning object instances.

So what have I learnt? (Please tell me if I've got any of these conclusions wrong!):

In order for an event that's dependent on a system condition to affect only a particular object instance that's chosen in advance by the coder at build time, we need to have some means of picking that instance (other conditions can be filtered for particular instances as explained in the Manual). Such system conditions can occur when a game is replayed, or a new level is reached, or a certain time has passed, and we then need to reset the state of a particular instance (e.g. the centre cat in our demo).

Construct 2 offers no way of naming individual object instances at build time (unlike OO languages). (It might be thought that UIDs could be directly used for this purpose, but their value is not known till runtime, so can't be used when building). There are workarounds to ensure that such a system condition will affect only a particular instance:

- One workaround is to set up an event to save the (runtime) value of an instance UID in a global variable, and then use that variable to pick that particular instance.

- Another workaround is to add to an object type an instance variable that can be given a different value for each instance, and then be used to pick a particular instance. This is what we all seem to be agreed on, since each of our demos has the same 'Reset' event:



Again, please tell me if this doesn't make sense - or if there's some simpler way of doing this!Velojet2012-01-11 08:38:17
B
105
S
20
G
12
Posts: 549
Reputation: 20,320

Post » Wed Jan 11, 2012 8:42 am

What if you put cat.variable("color")= "black" as the first part of the event, rather than on keyboard space pressed?
Think of it as a trigger.
Then put the keyboard as a sub-event.
Image Image
B
161
S
48
G
90
Posts: 7,347
Reputation: 66,749

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go, Artpunk, indiegrimes, NRABrazil, PixelatedTrip and 25 guests