How do I set a global variable value and then grab or use the variable in an instance/sprite?

0 favourites
  • 6 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.
  • Newbie here,

    I have been humming along using instance variables and have found that in order to set a random # I have to put the setting bit of it as a global variable but inside the instance/sprite section of the code. I don't understand why yet but hopefully I will eventually. Then I ran into a roadblock.

    I really need to have a global variable that I set to a rand # at the beginning of the project where I can pass the value to an instance variable. I can set a instance variable = to a global one but then I can't adjust HSL with that instance variable, it only shows global variables as my options. When I use my initially set global variable the adjust doesn't work. You can see my bit of code at the link below.

    I am probably missing something obvious. Thanks for your help.

    https://drive.google.com/file/d/1LYb92WUw8eW_XANYw1TZfcdS3dDYihx7/view?usp=sharing

  • Your on created event is firing before the start layout event since it's already there. That's the reason why it's not changing and it works on the second random, because the number generated is after the adjustHSL. The default value is 0 so 0 is applied.

  • Hmm . . . I thought the code would be executed in order of appearance (my mistake)

    So do I fix this by adding a wait at the beginning of the "on create" for the sprite?

    I tried this and it didn't seem to fix the top statement (which contains my attempt to transfer the variable from global to instance).

    Is there a way to force some code to execute first??

  • I can't just move the setting of the global variable into the on_create for the sprite because I want it to stay the same for the whole game and I will be creating multiple sprites.

  • OK I fixed this by using system>signal in the layout and system>wait for signal in the instance/sprite "on create" code.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi,

    Update to this issue which I could use help on. The wait for signal worked fine to solve the problem I had but created a new one. Now when I create a sprite on a button push the code in "on create" is not applying to the new sprite (this is the one that waits for the signal from the layout to get started. I know the layout is loaded because I have a bunch of sprites that the code applies to just fine in the layout (they look normal). It is just the new sprites that aren't created in the layout for which the "on create" coding isn't applying . . .

    Help please!

    Kate

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