Global Sprites and their instance variables

Get help using Construct 2

Post » Fri Mar 30, 2012 4:26 am

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 againmuffinmyst2012-03-30 04:30:50
Posts: 2
Reputation: 254

Post » Fri Mar 30, 2012 5:16 am

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.Kyatric2012-03-30 05:16:58
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Posts: 6,938
Reputation: 56,920

Post » Fri Mar 30, 2012 8:36 am

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 variablesJoe72012-03-30 08:42:29
Posts: 173
Reputation: 4,742

Return to How do I....?

Who is online

Users browsing this forum: farfetchd83, lainshi, MrWhizm, nav, RazHarmy and 8 guests