How do I change a global variable by its name?

0 favourites
  • 2 posts
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • I'm making a fighting game. In the code, I have characters who are hit affecting the global variable "Player1Percentage" or "Player2Percentage" depending on which player value they have for their instance variable.

    Currently, I have 2 separate events for whether a character with "Player = 1" is hit by a hitbox with "Player = 2" and vice versa, then adding the hitbox's damage value to "Player1Percentage" or "Player2Percentage"

    I want to consolidate my code for when I add future characters. My plan right now is to add to a value "Player(character.Player)Percentage" through javascript so I don't need to have multiple events for each character. However, javascript does not seem to like inserting a value into another value.

    How do I affect a variable like this correctly?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • This is not possible with events. The shortest code you can make is two actions:

    Set Player1Percentage to (character.Player=1 ? newValue : Player1Percentage)
    Set Player2Percentage to (character.Player=2 ? newValue : Player2Percentage)
    

    It's possible to refer variables by name in scripting.

    Or instead of global variables you can use a dictionary. Then you will be able to get/set dictionary keys by name like "Player"&character.Player&"Percentage"

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