Global Sprites and their instance variables

0 favourites
  • 3 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.
  • I've been looking through the forums to see if anyone's mentioned this, but as of yet, I haven't found any topics on this.

    My problem is this. I'm creating a game that you can dig for rocks and minerals and such. Each "spot" you can dig is a sprite. I have one sprite that I copied and pasted around. I gave that sprite an instance variable, so that each time you clicked on it, it would add to that variable, and once a specific number was reached, that object would be destroyed. It was nice, with the instance variable, because then each was independent of one another and worked great. Until I left that layout, and came back. Then they were all back, even if they had already been destroyed. (I wouldn't mind them coming back eventually, being "replenished", but not instantly) So I figured it had something to do with it being on that layout, and getting reset when i left and came back. So I made them global, so their variables would be saved even if I left and came back. That worked... except (dun dun duuuuuh)... when i left the layout they were placed in, they seemed to disappear. (really they were just going to the very very back, because if i went to a layout that didn't have a tile background, "blank" i could see them. And even though i couldn't see them on the layout i made them on, i could still click on them (once if found their position) and their instance variables stayed stored nicely.

    *whew* sorry for the long description. Anyways. I would like to keep them global, but I want them to stay on top of the layout i want them to be seen in, and not seen in the others.

    This will help me when i want a type of hud that i can carry though the other layouts, and i will defiantly want them visible. Thanks for your help.

    *edit: I am aware that I could make global variables and assign them as different sprites, and have each global variable linked to each though the events page. But that seems way too repetitive. (I'm willing and able to do that, just want to find out if there's a way to get it working this other way) It seems really really close to what i want, not sure if there's something I'm missing thats keeping it for doing exactly what i want. (and I have tried making them in a layer above everything else in the layout I have them on, to see if that kept them above the "ground" and they still move under the tiled background) Thanks again

  • Use a global array to store the values for each instance.

    On start of layout, apply the value to the instances according to the value stored in the array.

    You can store the value and the UID of the instance in the array to make it easier to pick the correct instance on start of layout.

    To know more about arrays check the how do I FAQ section "arrays" and about the UID picking check the section "Picking/Selecting an instance".

    Amongst the items listed you should find all the explanations you need.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I've written a tutorial about that - remember values of instance variables - to keep it simple i've used a global text variable for storing, array is certainly better and should work similar.

    remember values of instance variables

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