Press button - add 1 to local var

0 favourites
  • 9 posts
From the Asset Store
Kids Game
$49 USD
New Sounds Added Update: 115 new sound effects added for no additional cost!
  • I feel like this might actually be a bug - but I'm not sure. It's very simple. I'm just trying to add 1 to a local variable when I click on a button. Here's the entirety of the code:

    <img src="http://i.imgur.com/wBnv5.png" border="0">

    And here's a capx file: http://dl.dropbox.com/u/28484936/add_to_local_var.capx

    For some reason it won't add to the "number" variable.

    Anyone have a clue? Thanks/

  • Locals only apply to that event. Since clicking a button is a trigger once type of event, nothing will happen.

    Try using a global, or adding a variable to the button object itself.

  • I think this has to do with the 'repasting' of triggered events. On export, as far as I understand it, having a triggered condition in a subevent causes the event to get moved to the end of the event sheet with all the preceeding conditions of the parent events pasted in. As such, I think the local variable isn't getting pasted with it.

    Ashley - what do you think?

  • move local number before event 1 and it will work, also you dont need event 2 and 3 as subevents of event 1 to achieve what you wont

  • if you arrange events like this it will be triggered only once

    <img src="http://dl.dropbox.com/u/37009800/Construct%202/Help%20Others/local%20number.png" border="0" />

    seems that local number is here a condition - if it is 0 then add 1

  • I know that if I make it a global variable (which is what happens if I move it above event 1)it will work. But, this is just a simple example to show what I'm trying to achieve. In my game I want to avoid making global variables when possible.

    It seems like what I'm trying to do (have an triggered event that changes a local variable) should be possible. Maybe it's just not implemented yet?

    Thanks for your help. I guess for now I'll switch it to a global variable.

  • Local variables get reset every tick. Quote from the manual:

    ------

    1. Local variables reset their value to the initial value every tick. This is by design, and intended to match how local variables work in real programming languages.

    ------

    If you need to store the variable more than 1 tick, it's got to be global.

    One trick I'm using (because I don't like to have a lot of global variables either) is this:

    1. Create a sprite on the layout where you need the "local" variable and give it some "meaningful" name. For example I need some local variables that would store their value across ticks on my "Play" layout, I create a sprite called "varsPlay" and add it to the Play layout, somewhere out of view (-100, -100 coordinates or something)

    2. On this sprite, add all the local variables you need, as Instance variables. Like in your example, you'd add a "number" variable to the sprite varsPlay, and in your event you'd have varsPlay -> Add 1 to instance variable "number", and Text -> set text to varsPlay.number.

    I hope this helps you keep your design tidy, it does help me do that. One more advantage you get if you do this is that you can use "boolean" variables too, which you can't if you use proper variables.

    Cheers!

  • Try Construct 3

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

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

    Thank you! That explains it very nicely actually. Using a "local variables" sprite seems like a good way to go. I appreciate you taking to time to explain your method.

  • Sure, no problem, glad it helps <img src="smileys/smiley1.gif" border="0" align="middle" />

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