Global variable problem

0 favourites
  • 14 posts
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • 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?

  • 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?

  • 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.

  • 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.

  • 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~ ^_^

  • Wouldn't more logical approach be to use 'on created trigger' for your object?

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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.

  • 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.

  • 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.

    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. :)

  • 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.

  • 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.

  • 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.

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