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?
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)
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().