How do I add 1 to a global variable dynamically

  • Hi everyone!

    So here's the problem:

    When a user clicks on a certain object, I want to add 1 to the global variable that is mentioned in its instance variable.

    So it goes like this:

    User clicks on a button with instance variable BLUE

    I then add 1 to the global variable called BLUE

    Unfortunately I didn't see any way to dynamically target a variable, I only found through System and its variable options which requires to manually select the target.

    Is there a solution to this?

    Thanks all!

  • Yup. It's a good way to work too - because then you can use one object in lots of different ways.

    Here's an example:

    dansilber.com/blog/exampleButton.capx

  • Just in case there's an issue with the file, here's a picture and a quick explanation.

    I made a variable on the button named whatsMyColor and set one instance to "blue" and the other instance to "red".

    Then I added a sub-event to the click to verify which type of instance to get the instruction.

    Hope this helps!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Just in case there's an issue with the file, here's a picture and a quick explanation.

    I made a variable on the button named whatsMyColor and set one instance to "blue" and the other instance to "red".

    Then I added a sub-event to the click to verify which type of instance to get the instruction.

    Hope this helps!

    Here's the thing though, you manually chose in the interface in System the global variable BlueCount I need to select it dynamically. It works for that color, but what if I have 90 colors... do I really have to do 90 ifs?

  • Something like this?

    System -> Set variable("BlueCount")= number

    Unfortunately, this is not possible in C2.

    If you have 90 colors you should find another way to store them. Dictionary is the first that comes to mind.

  • Something like this?

    System -> Set variable("BlueCount")= number

    Unfortunately, this is not possible in C2.

    If you have 90 colors you should find another way to store them. Dictionary is the first that comes to mind.

    Dammit, well at least now I know! I'll just list all the possibilities and manage it like that. In all cases it's not going to affect performance but it's more of a good "programming" habit than anything.

    Thanks a lot for your answer!

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