How do I know which instance has which IID?

Forum Home Forum Home > Construct 2 General > How do I....?
 Post Reply Post Reply Page  <1234>
Author
13,223 Rep
Post Options Post Options   Quote Velojet Quote  Post ReplyReply Direct Link To This Post Posted: 09 Jan 2012 at 9:01am
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 :)

Edited by Velojet - 09 Jan 2012 at 9:02am
Back to Top

Moderator
28,466 Rep
Post Options Post Options   Quote Kyatric Quote  Post ReplyReply Direct Link To This Post Posted: 09 Jan 2012 at 6:29pm
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.

Edited by Kyatric - 09 Jan 2012 at 6:31pm
Back to Top
13,223 Rep
Post Options Post Options   Quote Velojet Quote  Post ReplyReply Direct Link To This Post Posted: 09 Jan 2012 at 7:23pm
@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.
Back to Top
13,223 Rep
Post Options Post Options   Quote Velojet Quote  Post ReplyReply Direct Link To This Post Posted: 09 Jan 2012 at 11:11pm
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!

Edited by Velojet - 09 Jan 2012 at 11:14pm
Back to Top
11,750 Rep
Post Options Post Options   Quote Yann Quote  Post ReplyReply Direct Link To This Post Posted: 09 Jan 2012 at 11:32pm
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?
Back to Top

Moderator
28,466 Rep
Post Options Post Options   Quote Kyatric Quote  Post ReplyReply Direct Link To This Post Posted: 09 Jan 2012 at 11:41pm
Back to Top
11,750 Rep
Post Options Post Options   Quote Yann Quote  Post ReplyReply Direct Link To This Post Posted: 10 Jan 2012 at 12:40am
Back to Top
13,223 Rep
Post Options Post Options   Quote Velojet Quote  Post ReplyReply Direct Link To This Post Posted: 10 Jan 2012 at 2:52am
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.

Back to Top
13,223 Rep
Post Options Post Options   Quote Velojet Quote  Post ReplyReply Direct Link To This Post Posted: 11 Jan 2012 at 8:27am
@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!

Edited by Velojet - 11 Jan 2012 at 8:38am
Back to Top
16,468 Rep
Post Options Post Options   Quote newt Quote  Post ReplyReply Direct Link To This Post Posted: 11 Jan 2012 at 8:42am
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.
Back to Top
 Post Reply Post Reply Page  <1234>

Forum Jump Forum Permissions View Drop Down