How do I change global variables by name

Get help using Construct 2

Post » Fri Oct 02, 2015 12:43 pm

I'd like to make an object (myButton) with 2 variables:
- variableName
- variableValue

And then if player presses this object change global variable thats name is [myButton.variableName] to [value myButton.variableValue].
Now I can only change global variables selected from dropdown list.

Is it possible?
B
55
S
14
G
10
Posts: 287
Reputation: 7,677

Post » Wed Oct 07, 2015 1:54 pm

What result you want to achieve with all this? Is there any need to change variable name.
Tell your aim and maybe I will tell you other solution.
B
9
S
1
Posts: 65
Reputation: 861

Post » Wed Oct 07, 2015 3:46 pm

I'm confused, you want to change the name of the variable, or you want to change the variable that the button effectss
B
10
S
4
G
3
Posts: 223
Reputation: 3,458

Post » Wed Oct 07, 2015 9:09 pm

As result I'd like to have universal button that can change global variables. When building my options menu I'd like to put one object (button) that determines what variable to change and how.

Example:
I have global variables:
1. setupSound
2. setupMusic
3. setupSpecialEffects
and so on.

On the options layout I'd like to put 3 instances of sprite object - buttons. In every button I determine what variable to change and how:

Button instance 1:
- variableName = "setupSound"
- variableValue = 1

Button instance 2:
- variableName = "setupMusic"
- variableValue = 1

So pressing Button instance 1 will set global variable "setupSounds" as 1.
B
55
S
14
G
10
Posts: 287
Reputation: 7,677

Post » Wed Oct 07, 2015 11:35 pm

You can use a dictionary for this, but you can't address global variables by name.
ImageImageImage
B
71
S
22
G
247
Posts: 3,756
Reputation: 137,558

Post » Wed Oct 07, 2015 11:53 pm

tecbug wrote:I'd like to make an object (myButton) with 2 variables:
- variableName
- variableValue

And then if player presses this object change global variable thats name is [myButton.variableName] to [value myButton.variableValue].
Now I can only change global variables selected from dropdown list.

Is it possible?


If they are not in the drop down list then you may have accidentally turned it into a local variable by moving it under an event block. A good rule of thumb is to put them at the top of the event sheet, and leave them there. Global has a earth icon, the local one has a diamond icon.

The global variable might be under the block as well, and this just wont do. C2 reads the sheet from top to bottom iirc.

I try to keep the Global variables on their own event sheet. That way to does not matter wher I put them on that sheet. Makes it easier to reference your variables too. You have one simple list where you go to add the tweeks, rather than scrolling through a bunch of event sheets.
B
29
S
8
G
4
Posts: 767
Reputation: 5,985


Return to How do I....?

Who is online

Users browsing this forum: robit_studios and 47 guests