How do I use variables and instance variables??

0 favourites
  • 6 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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

  • Try Construct 3

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

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

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

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

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

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

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