How do I use instances right?

0 favourites
  • 12 posts
From the Asset Store
Awesome drifts on auto generate way,tap to steer right.
  • Hey folks, I am a bit confused on how I do use object instances correctly.

    I created 4 buttons in my layout by hand. The buttons are instances.

    When the game is running, I want to make some buttons visible, and some invisible when something in the game changes. But I don't really see how I can pick a specific instance (e.g. button instance 2) in a function.

    I already read about the system command "for each". I could check if a button instance should be visible with that, but that would run all the time in the background, right? I only want to change visiblity when a certain event or function is called. Is there a way to do that? Am I using instances wrong? Should I make 4 distinct separate buttons that are not instances?

  • When an instance is created, C3 identifies it with a UID and IID. For example, you can make a pick of an instance using the appropriate event by referring to its UID.

    Here is the link to the manual page where it is explained what an instance UID and IID is: construct.net/en/make-games/manuals/construct-3/project-primitives/objects/instances

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Picking of instances is one of the most important concepts in Construct.

    There are lots of ways you can pick one or multiple instances. Most of object events do this, for example "Sprite is visible" will pick all visible instances. There are also several System events for more advanced picking.

    It seems like in your case the best option would be creating an instance variable ID on the button object. You can set manually ID values from 1 to 4 for buttons, and then you'll be able to pick by this variable:

    Button Compare Instance variable ID=2
    

    You can also pick by UID, but this is a bad practice. It may work for a quick prototyping, but I don't recommend doing this in a real project. Or you might end up with code like this.

  • I've read the manual entry, but I do not really know what to make of it. Is there a way to pick a specific instance in the visual coding? I now created this:

    It works, but I feel like this can't be the way its supposed to be used. I have just used the visual coding up until now.

  • You don't need "For each" in this case.

    Note, that this event will run on every tick (~60 times per second). If you have lots of instances and lots of event like this, it may be bad for performance.

    C3 comes with tons of templates and tutorials, I suggest studying a few of them. This should give you an idea of how events work.

  • You can Clone object instead of copy paste and that will allow you to give each one their own name. This will make it easier to tell which one you are trying to interact with for events and actions. You would also need to individually update each button object you make which is slower and easier to mess up.

    You can copy paste the objects:

    You can add a variable to each button of your own design.

    You can go based off the UID.

    This video will go into more details and give you some examples:

    youtu.be/hGcJyjRoClc

  • You don't need "For each" in this case.

    Note, that this event will run on every tick (~60 times per second). If you have lots of instances and lots of event like this, it may be bad for performance.

    C3 comes with tons of templates and tutorials, I suggest studying a few of them. This should give you an idea of how events work.

    I know that "for each" runs with every tick, thats why I created this thread in the first place. I looked for forum entries on how to use instances with events, but I could only find examples with "for each". Is there a specific tutorial that covers instances in events that you can point me to?

  • > You don't need "For each" in this case.

    > Note, that this event will run on every tick (~60 times per second). If you have lots of instances and lots of event like this, it may be bad for performance.

    > C3 comes with tons of templates and tutorials, I suggest studying a few of them. This should give you an idea of how events work.

    I know that "for each" runs with every tick, thats why I created this thread in the first place. I looked for forum entries on how to use instances with events, but I could only find examples with "for each". Is there a specific tutorial that covers instances in events that you can point me to?

    thanks I'll check out the video

  • Is there a specific tutorial that covers instances in events that you can point me to?

    Within the editor there are many tutorials in the "Beginner" section that can help you understand how construct works.

    This is simple tutorial that might help you: editor.construct.net

  • I know that "for each" runs with every tick, thats why I created this thread in the first place. I looked for forum entries on how to use instances with events, but I could only find examples with "for each". Is there a specific tutorial that covers instances in events that you can point me to?

    Not just "for each", but all non-triggered top-level events run on every tick. (Triggered events have a green arrow in the icon, for example "On collision")

    And in most situations you don't need to use "for each" loop, you can pick instances without it.

    Like I said, check out the official templates. This is the best way to learn Construct.

  • Thanks to winstreaks video I was able to figure out how to put the check for my instance variable into a function that I only call when I need it. Thank you!

    This is how it looks right now:

    It checks all instances of the buttons and compares their button number to the number of buttons that are supposed to be shown.

  • Functions are mostly used to avoid repeating the same code in multiple events. If you are only calling that function from one event, you don't need a function. Create a sub-event and pick the instances there.

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