How do I use variables and instance variables??

Get help using Construct 2

Post » Sun Jun 15, 2014 2:02 am

Hello,
I am wondering if someone has a little time to explain variables and instance variables.
I've watched a few tutorials on youtube but .... here I am.

Let's say I have a game that consists of four sprites.
Each sprite has three animation frames (each frame has a different color).

It is easy to bring in a touch object and tie the touch (On touched) to advance any of the four sprites to one of the available frames.
It is also easy to do something else when the touch ends.

I am wondering how I could use variables so that I don't end up with tons of "Touch | On touched | lines?

If someone could make a simple sample file and comment it well, it would help me a lot to make my event sheet shorter and easier to manage.

Thank you,

V :)
B
14
S
4
Posts: 190
Reputation: 1,735

Post » Sun Jun 15, 2014 3:25 am

Hmm, could you attach a file that shows your example using "tons of" Touch | On touched events? Then we can see if there's a good way to simplify it. I think that without a good context, it's hard to know how to use variables to simplify the code.

For example, if the 4 sprites all behave the same way, you can add them all to a Family and then make one Touch | On touched event for the Family.
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Sun Jun 15, 2014 8:00 am

Hi @vancouver

I try to explain it to you, but first have you red the manuel already?

Event Variable (=Global/Local Variables)
Instance Variable

What are the diffrences?
First a Instance Variable is only available for objects (sprite, tiles). You can use them to identify your object, for instance we have 3 buttons.
  • Button 1 has the Instance Variable 1
  • Button 2 has the Instance Variable 2
  • Button 3 has the Instance Variable 3

So now you can use the one Button sprite for doing 3 diffrent jobs. So lets see how the event will look like

One Touched Object "Button"
  • Compare Instance Variable = 1 do Action 1
  • Compare Instance Variable = 2 do Action 2
  • Compare Instance Variable = 3 do Action 3

So you don't need 3 diffrent buttons, one for each action. Awesome right?

So now to the global variables. These variables aren't bound on any objects. You can use them to store your scores, define some stats or what else you want to do ;) Also you can safe the stats of variables in many diffrent ways, for example in a WebStorage. But note you can only safe numbers in webstorage.

If you want to learn more about the variables and how to use them check the manuel links above, try some capx file demos or ask us, the community :)

Good luck with your projects!
Image Image iTunes: Match3 | Google Play: Match3
B
106
S
39
G
31
Posts: 1,172
Reputation: 29,233

Post » Mon Jun 16, 2014 12:10 am

Dalal wrote:Hmm, could you attach a file that shows your example using "tons of" Touch | On touched events? Then we can see if there's a good way to simplify it. I think that without a good context, it's hard to know how to use variables to simplify the code.

For example, if the 4 sprites all behave the same way, you can add them all to a Family and then make one Touch | On touched event for the Family.

@Dalal
I have done that and cleaned up the event sheet. I like families and will use them from now on.

Thank you for suggesting that. :)
B
14
S
4
Posts: 190
Reputation: 1,735

Post » Mon Jun 16, 2014 1:03 am

@AndreasR
Thank you for shedding some new light on variables.
I have checked the manual but for some reason, I have never learned anything in my life from a manual.


I've combed YouTube, looked at Udemy etc ....
Every time someone explains something I get 1% closer but still, I can't find a way to learn how to use C2 efficiently.

Most tutorials are for games which I nether play nor make.

The reason why I would like learn about variables and how C2 implements them is because I want to make an app that can "read" a series of sprite's frame numbers and trigger actions according to that. It is kind of complex but doable. Will see ...

Anyway ... thanks. :)
B
14
S
4
Posts: 190
Reputation: 1,735

Post » Mon Jun 16, 2014 2:44 am

Regarding your last statement, the Sprite: Compare frame condition is not sufficient?

I'm confused why you're using a game creator but not creating games? :)

When you say something is complex, that might well be true. Or, you might not be 'getting' a certain feature Construct 2, and you're maybe wanting to re-invent some wheels.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 6 guests