+1 vote

Sorry if the title is confusing, I don't know how else to ask this. Basically I have five variables: p1health, p2health, p3health, p4health, and currentp (with the p standing for player). Let's say I want to decrease the health of the current player. I want to write something like

{"p" + currentp + "health"} = {"p" + currentp + "health"} - 1

instead of

if (currentp == 1) :
p1health = p1health - 1
elif (current p == 2):
p2health = p2health -1

That way I only need one line of code instead of eight. However, I can't figure out any syntax that will get this to work, and I don't know how to describe my question easily enough for Google to know what I'm talking about.

Is this possible, and if so, how? Thanks in advance for any help!

in Engine by (16 points)
reshown by

To my knowledge, I don't think that's possible in GDScript (though it's possible in some languages, like ActionScript 2). I think the closest thing you have for that effect is the dictionary, where you could do:

p_health["p" + currentp + "health"] -= 1    # you can use '-=' too

Although, since you can create new entries in a dictionary with that same syntax, you'll probably want to guard against potential mistakes:

var p_name = "p" + currentp + "health"
assert( p_health.has(p_name) )  
p_health[p_name] -= 1

assert stops the game from running if the returned value is false, so if you don't want that just replace it with an if statement.

Thank you for your response! Honestly I've just started this so recently that I'm not even sure what the dictionary is, so I get something new to research, yay!
Thanks again!

Here's a short introduction to dictionaries in Python. GDScript dicts have different methods which you can see here.

1 Answer

+2 votes
Best answer

I see three options:

  1. As mentioned in the comment, you could use a dictionary that contains the health values of all players:

    var current_player = 1
    var players = {'player1': 25, 'player2': 52}
    players['player%s' % current_player] -= 1
  2. Create separate dictionaries for each player and assign the current player's dictionary to the current_player variable:

    var player1 = {'health': 25}  # Could also contain additional key-value pairs.
    var player2 = {'health': 52}
    var current_player = player1  # This is a reference to the player1 dictionary now.
    current_player['health'] -= 1
  3. Create nodes for your players (give them a health property) and assign the current node to the current_player variable:

    var current_player = $Player1
    current_player.health -= 1
by (72 points)
selected by

Thanks! This is very helpful.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.