How do I know which instance has which IID?

  • Shock bis

  • 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.

  • 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:

    <img src="http://www.millercrawford.com/images-offsite/construct2/reset.png" border="0" />

    Again, please tell me if this doesn't make sense - or if there's some simpler way of doing this!

  • 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.

  • nope I think you summed up things nicely. Although I think the second one is really what you were looking for.

    What I mean is, in a way, setting an instance variable and putting a distinctive value during edit time (what you call build time) is semantically the same as naming an instance.

    So using a condition to pick this proper instance is, in my opinion, exactly the same as doing a:

    if(instance.name == "black")
    { 
      instance.favorite = true;
    }
  • Yann: Many thanks for your positive feedback.

    Yes, your code is semantically the same as the Construct 2 code. But that's not what I'd do in JavaScript. Instead, I'd write:

    var instance = new ObjectType();
    ...
    instance.favorite = true;

    ... which is both semantically and syntactically different. There's no longer any dependence on the condtional test of an instance variable. That's why I called the Construct 2 solution a workaround.

    In fact, I'd go further and call it a kludge, and like all kludges it has a real downside. In this case, it's that the assignment depends on the value of a variable, which is always vulnerable to change. There's no such problem with a named instance, of course - the name is the identifier of its essence and simply can't be changed.

    newt: I'm sorry. I don't get your point.

  • Hi Velojet,

    I wouldn't normally bother bringing up a post that is over a year old, but it has been listed in the General FAQ and I wouldn't want people new to the platform to be misinformed about the design of Construct 2's event system.

    As you're aware, Construct 2's events allow users to "Pick" one or more objects implicitly, rather than assign specific objects to variable names, as you would in a language like Javascript. This is no more a kludge than how the "this" keyword in Javascript is implicitly assigned within a function to a particular object based on how the function was called. They are meant as conveniences. There's a very popular language used to sort, filter, and modify tables of data, called SQL, which behaves very similarly: you ask it to select rows based on the contents of the rows, and then you tell it what to do with each of those rows. SQL does this job wonderfully, and to do the same using Javascript with objects, arrays, and for loops all over the place would be a nightmare. It would feel like a kludge.

    Javascript is not the ultimate language. Nor is SQL, C++, C, or even Assembly. The purpose of a programming language is to translate human intention into something a computer can act on. Yes, depending on your needs, it may be difficult or impossible to do what you want with just Construct 2 events, and in that situation, you may need to move to Plugins so that you can code it in Javascript. But for the most part, Construct's method of implicit selection and foreach-style actions is a very powerful and more natural way of expressing intent for the sort of things we want it to do. It only starts to feel like a kludge if you insist on writing your events in the same style you would write Javascript code.

    Anyway, I hope you get my point. There are many ways to represent your intent to a computer, and depending on the language chosen, some intents will be easy to express while some will be hard. I always recommend learning more languages (C, Java, Javascript, Lisp, SQL, RegEx) so that one can have a broader understanding of what tools we have available to us as programmers.

  • Cowdozer

    Thanks for keeping this discussion alive!

    RegEx a language? Isn't it a pattern matching mechanism that's implemented in many languages (even SQL)?

  • Yep and yep. It's certainly not a Turing complete language (you can't make it do everything), but it's a great example of a domain-specific language. For Regular Expressions, its domain is pattern matching and replacing text. I'm not sure if SQL has RegEx support... I know there is a LIKE clause, which uses a simple (but non-RegEx) syntax. But Javascript definitely has RegEx, and it makes parsing strings very easy compared to using nested for loops, if statements, and variables pointing to different indices. It's not going to blow your mind in the same way that Lisp might blow a Java programmer's mind, but it is a prime example of why learning more languages gives you more effective tools as a programmer.

    BTW, I see that you've done a plugin tutorial video! That's greatly appreciated, and I'm going to have to check it out soon. :)

  • Cowdozer

    Definitely not a programming language in the same category as C, Java, Javascript, Lisp and SQL.In fact, I think you'd be hard put to find any authority describing it as a language.

    ... I'm not sure if SQL has RegEx support ...

    See, e.g., www.ibm.com/developerworks, www.regular-expressions.info, ...

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • On Wikipedia's definition of "Programming Language":

    programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term "programming language" to those languages that can express all possible algorithms.

    So yes, it is debatable as to whether RegEx is a language since it's not Turing complete (but neither is plain SQL). I agree that RegEx and SQL are not at all in the same category as the others we've listed, but I personally don't mind if someone calls them programming languages or something else. I did not intend to be misleading by using that term.

    Thanks, I see from your links that Oracle's databases have functions and in IBM DB2 you can add user-defined functions. Definitely worth using if you have a need for it and aren't worried about making your SQL queries slightly platform-dependent.

    I watched your Plugin tutorial and it was a nice intro. :) Construct 2 seems very easy to extend that way!

  • instead of starting a new thread I have a similar thing I'm trying to figure out. I have 32 sprites on the layout all of which are stationary. I kneed to change the active frame of the one clicked and cannot figure out how to set the frame on a sprite based on uid of the one clicked. Trying to avoid using 32 sprites. They are for buildings and clicking on one upgrades it so I need to know exactly which one was clicked

    only been working with Construct 2 for 3 days so reply in very beginner terms :)

    Thanks for any help

  • briermay You don't need to mess around with UIDs or IIDs for this purpose. Construct will always use the instance of an object that is picked by the condition of an event. See How events work in the manual. The picked instance also extends to sub-events for further filtering. See Sub-events in the manual.

    For beginners, as long as you have your action in the same event as your condition, you can freely call the object as a whole and it'll use only the instance that triggered the event.

    Example capx [R118].

  • I watched your Plugin tutorial and it was a nice intro. :) Construct 2 seems very easy to extend that way!

    Cowdozer

    Thanks! Yes, C2's extensibility through plugins is one of its attractive features. I haven't used a regex in any of my plugins, though - yet ;)

    briermay

    Another of C2's attractive features is the way in which, as GeometriX says, it will always use the instance of an object that is picked by the condition of an event - without any more work on your part. (Don't be misled by the earlier discussion in this thread, which revolved around a rather esoteric problem.)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)