How do I add 1 to a global variable dynamically

Get help using Construct 2

Post » Sun Oct 01, 2017 9:25 pm

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!
B
11
S
4
Posts: 27
Reputation: 906

Post » Sun Oct 01, 2017 9:45 pm

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
B
15
S
2
Posts: 36
Reputation: 1,339

Post » Sun Oct 01, 2017 9:52 pm

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

Image

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!
B
15
S
2
Posts: 36
Reputation: 1,339

Post » Sun Oct 01, 2017 11:13 pm

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

Image

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?
B
11
S
4
Posts: 27
Reputation: 906

Post » Mon Oct 02, 2017 12:40 am

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.
Check out my awesome game Doptrix
B
17
S
9
G
50
Posts: 829
Reputation: 27,967

Post » Tue Oct 03, 2017 8:58 pm

dop2000 wrote: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!
B
11
S
4
Posts: 27
Reputation: 906


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 17 guests