Global variable problem

Get help using Construct 2

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

Post » Mon Feb 11, 2013 4:25 pm

[QUOTE=farflamex]
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.[/QUOTE]

I'd still recommend you use 'on created' trigger, no need to save UID's or other stuff anywhere. It's a very coderish approach. :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Mon Feb 11, 2013 5:52 pm

There was a reason I didn't do this originally (because the tiles are also being created by other parts of the program) but I'm sure I can get around it by testing which screen I'm on for example. Thanks, I'll go with that - I'm banging my head against a brick wall with the above approach so I need to try something else anyway, and it does seem like a better solution here.

Still confused as to why the code doesn't work though, it really should.
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Mon Feb 11, 2013 9:25 pm

You're setting NewTileUID inside a trigger event and triggers don't run inline with other events. I found by experimenting that the triggers run before the normal events.

When you click the mouse, the 'on clicked' trigger runs setting NewTileUID, then the normal events run setting it back to -1 before checking to see if it's not equal to -1. It would work if you swapped the order of events 1124 and 1128.
B
54
S
28
G
18
Posts: 1,520
Reputation: 24,990

Post » Mon Feb 11, 2013 10:23 pm

Thanks for clearing that up. It's another little peculiarity of C2 which makes it very complicated when these problems pop up, especially since there's no debugger. Still, I know now, thanks.
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Previous

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 26 guests