Code optimization

Godot Version

4.2.1

Question

I’m new here and my coding skills are pretty basic at this point. I am currently trying to learn how to write more efficient and optimized code. I don’t like repeating myself in code, but I find that I do it quite often because I don’t know how to do it differently. How can I write the following code much shorter? Can I dynamically type out the variable names instead? Like “current_player”_total += “current_player”_round or something?

		if current_player == 1:
			player1_total += player1_round
			player1_round = 0
			player1_total_label.text = str(player1_total)
			player1_round_label.text = str(player1_round)

		elif current_player == 2:
			player2_total += player2_round
			player2_round = 0
			player2_total_label.text = str(player2_total)
			player2_round_label.text = str(player2_round)

I would be thankful for any help.

Sincerely,
Magister Werner

Actually, this code can be shorter
First, player1 and player2 need to have the same Script or their Script is extended from the same base.

For example, player1 and player2 has the same Script and you set
class_name = “PlayerScript” in the Script

#here you can use the class_name as the parameter type
#once you transfer player1(just use the player1's node which 
#is attached with 'PlayerScript') to this function, the parameter 
#'player' will point to player1, and when you transfer player2 to 
#this function the parameter 'player' equals player2
func an_example(player: PlayerScript) -> void:
         player.player_total += XXXX
         player.player_round = 0
         player.totoal_label.text = str(player.player_total)
1 Like

You can also make use of your naming convention and utilizing the set, get, and get_node methods along with String formatting.

var player_total_string = "player%d_total" % [current_player]
var player_round_string = "player%d_round" % [current_player]
var player_total_label_string = player_total_string + "label"
var player_round_label_string = player_round_string + "label"

set(player_total_string, get(player_total_string) + get(player_round_string))
set(player_round_string, 0)
get_node(player_total_label_string).text = str(get(player_total_string))
get_node(player_round_label_string).text = str(get(player_round_string))

Or with the String formatting inline:

set("player%d_total" % [current_player], get("player%d_total" % [current_player]) + get("player%d_round" % [current_player]))
set("player%d_round" % [current_player], 0)
get_node("player%d_total_label" % [current_player]).text = str(get("player%d_total" % [current_player]))
get_node("player%d_round_label" % [current_player]).text = str(get("player%d_round" % [current_player]))

Thank you. This is pretty much what I had expected would do the trick, although I couldn’t get it working on my own.

1 Like

Thank you very much for this. I was fiddling around with set() and get() earlier because this seemed possible, but I eventually gave it up.

The inline-version of this became the final solution for my project and works exactly as I wanted although I didn’t use separate nodes and just used get() instead of your get_node().

1 Like