Global variable problem

Get help using Construct 2

Post » Mon Feb 11, 2013 4:10 am

There must be something I don't understand about how GV's behave in C2 because mine isn't behaving as I'd expect.

I have my global variable, and I set it to -1 every loop. If an object is created, I set the GV to the object's UID, so that I can then retrieve the UID later on and work with the new object (I'm doing this because of the issue where a newly created object doesn't become accessible until a new top-level event... it's necessary in my case).

So I then do a test, which is basically saying, if my GV is NOT -1, a new object was created, so I can work with it. But for some reason, when I set the GV and then test it, it doesn't respond.

So like this.

Every tick - GV = -1
Event - Create object and set GV to the new UID.
Next top-level event - If GV <> -1, do stuff.

It doesn't work. When a GV is altered, is it altered immediately or do I need to wait for new events or something?
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Mon Feb 11, 2013 5:01 am

Your psudocode there should work. If you alter a global in an event, it will be that way for later events. Work flow is linear.

Can we see a capx or screen shot?
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Mon Feb 11, 2013 5:16 am

That's the problem really, workflow is not linear when it comes to creating new objects. You need to back up to a new top-level event before it registers. It's a real pain and constantly causing me problems. My solution here is messy and it doesn't even work.

With a GV, does it IMMEDIATELY alter? Will the new value be in place regardless of the indentation of the next check etc? Is it something to do with minus numbers? I'm clutching at straws here but this just makes no sense. I'll try to strip some code out and post a screenshot.
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Mon Feb 11, 2013 5:33 am

https://www.dropbox.com/s/ouo3me42a8ygh95/c2bug.png

As you can see, the top line sets the variable to -1. If the mouse is clicked it then creates the new object and sets the variable to the new object's ID. It should then follow that the next line is actioned, since the variable is set to the UID so the sound should play, but it doesn't.

Obviously this is very simplified, but I can confirm that it doesn't work and the sound is fine elsewhere. The reason I'm doing seperate 'Is layer visible' top-level events is because the code is more complex than this but it requires that to get the new object to register.
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Mon Feb 11, 2013 6:43 am

I see~
Because on your line 1123 & 1124~
You force the NewTileUID to -1 when layer is visible~
So that is impossible to reach line 1128~
Bacause the NewTileUID is always -1~

Try rearrange the code as below:
If (Layer is visible)
----If (Some condition)---------{Set NewTileUID to -1}
----If (NewTileUID = -1)----{Do something}
----Else--------------------{Do something}

Hope it help~ ^_^
B
13
S
5
G
3
Posts: 63
Reputation: 3,989

Post » Mon Feb 11, 2013 6:53 am

Wouldn't more logical approach be to use 'on created trigger' for your object?
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Mon Feb 11, 2013 7:11 am

I would tuck your sound check as an else event to event number 1126 and drop the variable all together.

Unless I'm reading it wrong, the only time the sound plays in that block is if 1126 is false.theubie2013-02-11 07:11:41
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Mon Feb 11, 2013 4:12 pm

It's not attempting to play if the variable is -1, it attempting if it's NOT -1. In other words, if the variable was set to the UID above, then it should play.

Also I can't use an else event here, remember this is stripped-down code and the reason I'm using this approach is that I need to work with the newly created object later on, but C2 won't allow that until you go back to a top-level event. This is a major problem I'm having regularly with C2. You need to either manipulate the object immediately inside the event-chain it was created in, or go back to a top-level event. So in this case, I'm storing the UID, then going to a top-level event and if the global variable has a UID stored instead of -1 it should play the sound.

That's the code exactly as I'm running it, when I click the button, the variable is set to the new UID, meaning the sound should play in the next block, but it doesn't.

I'll take a look into triggers, but I should point out that I'm aware that the code above isn't the best approach. I'm using this approach because I need to store the newly created object, then work with it in a new top-level event which is a C2 issue.farflamex2013-02-11 16:15:02
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Mon Feb 11, 2013 4:19 pm

Just to explain what I'm trying to do here.

When the button is clicked, a new object is created. It can be one of 10 or so different objects, which are all in the same family. When they are created, they need a couple of variables set which are unique to that object, but then LOTS of variables set which are required for each type.

So, what I wanted to do was, create the object, set it's variables, then find it in the family immediately and start setting all the standard variables. This part doesn't work in C2 - if you try to find it in the family at this point C2 doesn't find it because it's not registered until the next top-level event.

So my solution (as mentioned on the forums for this kind of thing) is to create the object, do the couple of changes it needs, then save it's UID, start a new top level event, find it again in the family, then do all the other changes.

The only alternative I'm seeing right now is to do the large number of changes for each object type, which would work but would be very laborious.
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Mon Feb 11, 2013 4:22 pm

And if you check the code again, forget all that stuff about UID's and objects, but can someone explain why that's not working? Look at what it does - the variable is set to -1. If the button is clicked, the object is created and the variable set to the UID (that part works and it does change the variable). Then it asks 'Is the variable NOT -1' and for some reason, it thinks it is. I don't see how that can be possible if I've just changed it to the UID of the new object.
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Next

Return to How do I....?

Who is online

Users browsing this forum: anty21ro, Magistross, mallorcaredes, PavleNikolic and 23 guests