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

Get help using Construct 2

Post » Thu Jun 30, 2016 9:06 am

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.
B
67
S
37
G
13
Posts: 241
Reputation: 12,760

Post » Thu Jun 30, 2016 9:16 am

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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
293
S
117
G
96
Posts: 7,310
Reputation: 71,069

Post » Thu Jun 30, 2016 9:20 am

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.
B
67
S
37
G
13
Posts: 241
Reputation: 12,760

Post » Thu Jun 30, 2016 10:51 am

If group 9 is created and then group 1 is destroyed then group 1 will have higher IIDs wouldn't it?
Nope
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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
293
S
117
G
96
Posts: 7,310
Reputation: 71,069

Post » Sat Jul 02, 2016 9:25 pm

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.
Last edited by Unconnected on Sat Jul 02, 2016 10:27 pm, edited 1 time in total.
B
67
S
37
G
13
Posts: 241
Reputation: 12,760

Post » Sat Jul 02, 2016 10:25 pm

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.
ImageImageImage
B
71
S
22
G
247
Posts: 3,756
Reputation: 137,558

Post » Sat Jul 02, 2016 10:31 pm

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.
B
67
S
37
G
13
Posts: 241
Reputation: 12,760

Post » Sun Jul 03, 2016 12:05 am

Everything is working correctly now. If a group is already in existence, it won't create a new group on top of it.
B
67
S
37
G
13
Posts: 241
Reputation: 12,760


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 3 guests