How do I check if an Instance's Variable value exists?

0 favourites
  • 8 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Just a simple problem I can't figure an answer to. I have an object that creates groups. Each group is given a unique GroupID via Instance Variable, and can be erased/destroyed at any time.

    The GroupID gets re-used if a group is destroyed.

    If GroupID 8 gets destroyed a Group created can have GroupID 8.

    I can't use UID or IID because objects are not created in a specific order.

    Each group has a different amount of objects.

    I can't use 'Pick Highest' because GroupID 8 won't always be the highest.

    Group 7 can exist, Group 8 can be missing and Group 9 can be existing... etc.

    In Events how do I check to see if GroupID 7 exists? I don't have any collisions enabled and would like to not use them if possible, I am trying to limit collisions to only objects it is required to. I am sure this can be done with a variable anyway.

    I don't have much of a capx to give an example, because I am not 100% sure how to approach this.

    I am only wanting an example. I am just not sure of what Expression/Event to use, just a point to the right direction is usually enough. I didn't seem to find what I was looking for by searching.

  • IID actually is a perfect fit for what you are attempting to do it seems.

    They automatically ID an instance of an object type. If you delete the instance and then create a new instance of the same object, this object will have the "missing" ID without you having to check whether it was existing or not in the first place.

    Another solution I'd go along with, in the case where I'd want to handle my order system as you seem to be willing to do is using an array.

    The index of the array acts as "GroupID" and the value of the array would be the UID of the "Group".

    When creating the array, make it as wide as you already have Group in your layout. At worst, start it at width 0 and push a new index for each group this way you can assign an index and the UID in the array.

    When deleting a Group instance, check in the array the index for the UID you are currently deleting, and modify the value from the UID to something like -1.

    This would be your "empty" value.

    When creating an instance, you would go through the array to check whether you have -1 values or not. If you do, place the UID value in this index. If you don't, push a new index.

    The array would then grow dynamically when the "max amount" is reached, but otherwise would allow you to use existing indexes.

    Using functions to automate looping through the array would be of help.

    But again, you are basically trying to redo the IID system.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I don't think I am trying to redo the IID. I assigned an object to each group so Construct 2 usually only has to check 12 objects every few seconds and not 500+.

    Each group has an ID assigned to it. If group 9 is created and then group 1 is destroyed then group 1 will have higher IIDs wouldn't it? If an IID is destroyed does it's IID get re-issued?

    I can use IID as you said. I would have to apply it to the groups leader and not the group itself. How do I check to see if an IID value exists then? I will admit it is my fault I left that part out, sorry.

  • [quote:2yld2tek]If group 9 is created and then group 1 is destroyed then group 1 will have higher IIDs wouldn't it?

    Nope

    [quote:2yld2tek]If an IID is destroyed does it's IID get re-issued?

    Yes

    Hence, no need to check if the index is free or not, existing instances keep their IID, new one gets assigned the free IIDs.

    IID in the manual.

  • I am still needing to check if an object exists or not. I need to use use it's GroupID because it is already used in other events and it isn't equal to it's IID. IID starts at 0 and GroupID starts at 2 for offset reasons. It isn't always a guarantee the Groups will be created in order as well. If IIDs are in order from left to right as 1, 8, 3, 4, 5, 6, 7, 2, then things will start to get confusing. I suppose this would happen if group 8 and 2 get destroyed and group 8 is recreated first.

    I would rather have an extra variable that gives a needed value than to use IID and still have to make a variable for offset to figure the value, or have to complicate other events with offsets.

    I need to check if GroupID X exists or not. If it exists I have to change a variable to prevent a new group from being made on top of it.

    I know how to check an existing variable on an existing object, I just don't know how to check if an object exists. I would suppose I could try blindly sending a variable change. If the variable changes it exists, if it doesn't it won't change. Either way I will know what to trigger. I am not sure what would happen if you try to change something that doesn't exist though. Perhaps a way of just having it check each existing object type for a given variable's value.

  • Just do a pick checking for GroupID = X. There are various ways to do this.

    Object.Compare instance variable, where variable is GroupID and value is X. Then add an Else. The Else will run if there are no GroupID = X.

  • Okay that was simple. It was what I was needing. Can't believe I wasted that much time trying to figure it out, and that search results didn't bring up "compare instance variable". I probably typed in the wrong keywords. I guess I didn't realize that, that action could be applied if the variable value was missing. Thanks again.

  • Everything is working correctly now. If a group is already in existence, it won't create a new group on top of it.

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