Call a function so that another function has a constant first

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ZeralldGames

Hello everyone! I would like to know something:
I have _on_HTTPRequest_request_completed in my code which outputs data from json format, well classic HTTPRequest. And I have a function refresh_lobby () in which you need to display the variable “rank” from the first function. I don’t understand how to do it. I already searched on Google, but I understand almost nothing and nothing works for me.

My code:

extends Panel

var playerRank = ""

func _ready():
	gamestate.connect("player_list_changed", self, "refresh_lobby")
	refresh_lobby()
	$HTTPRequest.connect("request_completed", self, "_on_HTTPRequest_request_completed")
	$HTTPRequest.request("https://example.com/userapi.php?username="+gamestate.get_player_name())
func _on_HTTPRequest_request_completed(result, response_code, headers, body):
	var json = JSON.parse(body.get_string_from_utf8())
	playerRank = json.result["playerRang"]
	return(playerRank)
func refresh_lobby():
	var rank = playerRank
	var players = gamestate.get_player_list()
	players.sort()
	var isPlayerYou = " (You)"
	get_node('List').add_item(gamestate.get_player_name() + isPlayerYou + " (Rank "+rank+")")
	for p in players:
		get_node('List').add_item(p)
:bust_in_silhouette: Reply From: rossunger

It looks like you’re refreshing the lobby before that httpRequest is completed… if you just move the refresh_lobby() call to the end of the _on_HTTPRequest_request_completed() function, then it might work correctly :slight_smile:

Also looks like you might have a typo with json.result[playerRang]

I tried different combinations, but nothing works. Can it be possible to implement this in some other way?

ZeralldGames | 2022-02-07 09:46