How do I prevent variable checks on too many instances?

0 favourites
  • 15 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I have a lot of objects that have an Instance Variable called 'Status' .

    The objects will change their behavior based off of 'Status'.

    0 = Idle - Does nothing.

    1 = Creation - Animate when being created.

    2 = Destroy - Animate when being destroyed.

    The objects don't move and don't have health, they are created and destroyed by variable changes.

    The player Creates or Destroys using an in-game Item.

    Each object is created into a group and each group is given a 'GroupID' Via Instance Variable.

    If group 6 is destroyed every object with 'GroupID' 6 will be destroyed at the same time.

    If group 5 is destroyed the next group created will be given 'GroupID' 5.

    Only the highest 'GroupID' can only be destroyed at any given tick, but so can the next 'GroupID'.

    Most of the time 'Status' will be 0. There is a chance a player can Create or Destroy quickly.

    Is there a way to only enable a 'Function' if a 1 or 2 in the variable 'Status' is present in the layout?

    I don't want it checking 800+ objects every single tick, but if they are destroyed or created they need to react instantly.

    The only thing I could think of was creating a 'leader' in each group and then checking the variables needed on the 'leader' object. If the leader objects has the required variable then apply create/destroy to all of the same objects with the same 'GroupID'. Is that a proper solution or is there one better? I assume the 'leader' objects would have to be a 2nd object to prevent the the extra variable checks.

    I only came up with it because of groups, is it possible to do it without groups?

  • first of all my English is not good and may be I do not understand it properly and my answer will depend on what i understand

    example capx

    If this was not your question then please let me know.

  • Have you thought about the conditions "on object created" and "on object destroyed" ?

    Though "on destroyed" will not allow you to animate their destruction, because it is triggered after they are removed, but you could work around this by animating their destruction into a separate object type that is spawned "on destruction" and then destroyed when its animation ends.

  • Unconnected

    if the objects ONLY get created or destroyed as a direct response to the player's input, then you don't need to check their status every tick, or ever - unless the player takes action.

    When the objects are created (and given an initial status of 1) you would start the creation animation. You would also need an event: On Animation "CreateObject" Finished to set the status to idle (and show the idle animation if necessary).

    When the player destroys a group you can set the status to 2 and start the destroy animation. Then an On Animation "DestroyObject" finished can remove the object from the screen.

    By using triggers like that, there is no need to constantly check what is happening to the status of an object.

  • Sorry I didn't realize how poorly I explained what I was needing, so I will try again.

    I meant every tick the trigger is running. So does Construction 2 group objects by what a variable equals? Or does a trigger still check every object as long as it's running, or just when it starts? I'm not sure how it would know which objects to trigger and how many times if it didn't check all objects at least once to get info. I try to only use triggers, just not 100% sure how they work yet.

    Can a 'Function' run multiple times at the same time? If Group 6 and Group 5 both call it a few ticks apart?

    If so then can someone show me how to have a loop in a function that loops as long as an objects animation frame is between 0 and the highest frame available (25). A loop based off of animation frame was my main issue, should I have created a 'CurrentFrame' Instance Variable? I currently have a 'SetFrame' Instance Variable that can be -1, or 1 so frame can be changed accordingly based of of 'Status' Variable.

    I know how to make a loop repeat a specific amount of times, just not sure how to make a trigger loop that runs until a variable is equal to a specific value.

    I have thought about on created/destroyed, that is basically what the Capx was (On click play animation "create" on click play animation "destroy").. The Capx was very similar to what I needed. I need more control of the objects frame in case it gets destroyed during creation animation.

    I am aware of Setting 'AnimationSpeed' but once the object reached it's max frame it seemed to ignore any commands.

  • No solutions, since i have no darn idea what you are up to. Just ideas 4 you. How to use animation triggers, functions and an array.

    In this one only the oldest created can be destroyed. Creation happens kinda random when clicked on an empty position. Newly created groups can be destroyed when it is their turn to be the oldest group. ... .capx?dl=0

    It's not sorting many objects, nor rearranging them, it just sorts a GroupId. And picks according.

  • I asked how triggers work, if it checks every object while it is running, if it checks all objects only once, or if it knows which objects are needed by some kind of list or something.

    Can a 'Function' run more than once at the same time for different objects if it is called more than once within a few ticks apart?

    I asked for an example of a while loop, because it seems to be what I need. I can't find an example because nobody uses them anymore it seems.

    I need a loop that will run every 0.5 seconds while a variable is not equal to a specific number. I only know how to run a loop a specific amount of times, or once for every object. I can't seem to find a way around this.

  • I found out you can use Repeat (Variable <> 1)

    Right now I have:

    'Repeat' loop with a condition of 'less than 25'.

    The actions are 'wait X Seconds' and 'add to'

    See below:

    Event/Condition -------------------------------------------------------- Actions

    Repeat (Variable is not equal to 1) Times---------------------- Wait 0.5 * loopindex Seconds.

    'GroupFrame' is less than 25-----------------------------------------Add 1 to 'GroupFrame'

    0.5*loopindex is supposed to slow down the loop so it adds 1 to 'GroupFrame' every 0.5 seconds.

    What it is doing is ignoring the 'wait' function. When I run the function in debugger 'GroupFrame' jumps to 25 automatically.

    Can someone show me how to make the loop slow down?

    I am okay if you change the loop completely if it will be better. I am kind of just Frankenstein-ing it together right now anyway.

  • what about doing it backward such as create a variable call it max creation and you could do something if max creation is not zero then every 5 sec spawn this and then -1 to the max creation.

    so that you could effectively loop it without looping and it gives you the room to adjust how fast and how slow it can create something and you can do the same for destroy as well. You can also do something like when player trigger something add 10 to max creation and then that code will keep going until it hit 0 which is ten sprite.

    And this can just be a control group. I hate using loops it tend to mess with me. so which is why i do that trick

    Is that what you want ?

  • I figured I was probably going in the wrong direction so I created a Group Leader and every group leader gets checked every few seconds. Every group is made using a number sequence. 8, 24, 48, 80, 120, 168, 224, etc... I am able to actively check 12 objects instead of 624. If the group leader has a variable change the whole group changes accordingly.

    I still find it hard to believe I was having trouble finding a 'While' or 'Repeat' loop example with an efficient 'Wait' built in. I finally found a solution on a forum and it wasn't working for me for some reason, it even looked exactly like what was posted.

    I would have rather had a Trigger Loop with a timer, but I couldn't figure it out. This will do for now.

    Starting to think of it, the problem in the loop was probably a variable type.

    I know when I make a 'Zoom' Variable it has to be global or it won't work... Maybe it is the same issue.


    Not fully but I did something similar and was posting about it when you typed your post. Thank you for your time.

  • Unconnected

    It would really help if you posted a sample capx file and let us make some suggestions.

    You had trouble finding a loop with Wait built in because you can not slow down a loop. That is not what Wait does.

    The code on an event sheet runs really fast - 60 times every second. And the code can execute thousands of commands every tick without any trouble.

    Wait defers the code immediately after it, but the event sheet continues to run at the same speed (blazingly fast). That is why GroupFrame was getting to 25 so fast - before you can even blink.

    I get the feeling you are making this more complex than it needs to be. You seem concerned about having to do too many variable checks, but Construct2 can easily check 624 objects per tick. The problem may be that it does it too fast. That is why you have to structure things in entirely different ways to slow things down.

  • if you want to add 1 to GroupFrame every 0.5 seconds, you dont need to create a loop, and slow the loop down...

    you just create an event: Every 0.5 Seconds, with an action: Add 1 to GroupFrame

    (The event sheet as a whole is already a loop... you don't need to put a command like that inside another loop.)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can have wait in loops. I have seen a few examples and I even had a few example Capx that worked with wait in a loop. You just have to do is add:

    "wait '1*loopindex' seconds"

    Into the loop. It seems to work for every loop except for what I was trying to do. I can only assume I had something structured wrong.

    I have a lot of complicated groups in the capx to make things appear in certain ways and it is a project I have been working on for a long time. I don't want to just make my project Public Domain by adding it online. I have a ton of my own art and my own sounds etc in it. I can't just copy a few groups or events out into a new capx, because it is intertwined rather well. I suppose I could post a photo, but then I would have to explain every Variable. I will admit sometimes I will over complicate something, but I always come up with a more simple solution, or someone points it out. I may have an extra unneeded variable here and there that isn't needed, but all groups and functions are done rather simply and function very well.

    I found it would be better to ask for an example or a solution to a problem. I was having an 'expression' issue, mostly because I am still learning how Construct 2 handles things and sometimes forget how flexible Construct 2 is.

    I have never needed a loop that ran until a variable is not equal to X.

    I found out it can be done with 'Repeat'

    I have never needed a loop to have 'wait' in it before. It is possible because I've seen it done and know how to do it now. It wasn't working in my 'Repeat' loop, but it was working in other loops I tested out. My structure was wrong on that 'Repeat' loop probably, but I am not using it anymore.

  • I explained my issue in my capx and gave the needed info and asked for a function that worked only when a variable is equal to 1 or 2, and I didn't want the function to have to check every single instance when it ran. I gave the answer of groups in the original post. I was only trying to find a solution that didn't have to add a group leader and didn't run all the time. I ended up with a mixture of what I wanted and what I didn't. Every half second Construct 2 checks 12 objects instead of 624. I was wanting it as a trigger, but it is becoming to much trouble. This solution is simple and works.

    The point of the slowed loop was, I was going to put it in a trigger event.

    This prevents needless checks on ticks.

  • Thank you guys for helping.

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