Godot Version
Godot 4.3
Question
Hello! So i'm trying to add a function where when the "FacebookButton" is pressed it opens a new tab and auto-adds your text of stats from the games that you played in ultimate tic tac toe and simple tic tac toe, any ideas?
Here is all my code relevant to it
StatsPopUp.gd (the one with the button)
extends Popup
var stats: Dictionary = {} # To store the stats
var stats_file_path = "user://Stats.cfg" # Use consistent filename
func _ready():
# Ensure VBoxContainer contains the StatsLabel and buttons aligned vertically
var vbox = $VBoxContainer # Access the VBoxContainer
vbox.size_flags_vertical = Control.SIZE_EXPAND_FILL # Set size flags to expand
load_stats() # Load stats when ready
display_stats() # Display stats on the label
# Make the ShareTo label visible and set its text
$VBoxContainer/ShareTo.visible = true # Ensure it's visible
$VBoxContainer/ShareTo.text = "Share your stats below" # Set the desired text
# Connect the reset button signal if not already connected
if not $VBoxContainer/ResetButton.is_connected("pressed", Callable(self, "_on_ResetButton_pressed")):
$VBoxContainer/ResetButton.connect("pressed", Callable(self, "_on_ResetButton_pressed"))
func load_stats():
var file = FileAccess.open("user://Stats.cfg", FileAccess.READ) # Use consistent file name
if file:
stats.clear() # Clear current stats before loading new ones
while not file.eof_reached():
var line = file.get_line()
if line.begins_with("["):
continue # Skip section headers
var key_value = line.split("=")
if key_value.size() == 2:
stats[key_value[0].strip_edges()] = int(key_value[1].strip_edges())
file.close()
else:
print("Failed to open stats file for reading.")
func display_stats():
var stats_text = "**WON**\n"
stats_text += "Ultimate Tic Tac Toe AI Won: %d\n" % stats.get("Ultimate_Tic_Tac_Toe_AI_Won", 0)
stats_text += "Ultimate Tic Tac Toe Yourself Won: %d\n" % stats.get("Ultimate_Tic_Tac_Toe_Yourself_Won", 0)
stats_text += "Simple Tic Tac Toe AI Won: %d\n" % stats.get("Simple_Tic_Tac_Toe_AI_Won", 0)
stats_text += "Simple Tic Tac Toe Yourself Won: %d\n\n" % stats.get("Simple_Tic_Tac_Toe_Yourself_Won", 0)
stats_text += "**LOST**\n"
stats_text += "Ultimate Tic Tac Toe AI Lost: %d\n" % stats.get("Ultimate_Tic_Tac_Toe_AI_Lost", 0)
stats_text += "Ultimate Tic Tac Toe Yourself Lost: %d\n" % stats.get("Ultimate_Tic_Tac_Toe_Yourself_Lost", 0)
stats_text += "Simple Tic Tac Toe AI Lost: %d\n" % stats.get("Simple_Tic_Tac_Toe_AI_Lost", 0)
stats_text += "Simple Tic Tac Toe Yourself Lost: %d\n" % stats.get("Simple_Tic_Tac_Toe_Yourself_Lost", 0)
$VBoxContainer/StatsLabel.text = stats_text # Display stats on StatsLabel within VBoxContainer
func _on_ResetButton_pressed():
reset_stats() # Call the reset_stats function
func _on_CloseButton_pressed():
queue_free() # Close the popup when button is pressed
func reset_stats():
# Reset all values in the dictionary to 0
stats = {
"Ultimate_Tic_Tac_Toe_AI_Won": 0,
"Ultimate_Tic_Tac_Toe_Yourself_Won": 0,
"Simple_Tic_Tac_Toe_AI_Won": 0,
"Simple_Tic_Tac_Toe_Won": 0,
"Ultimate_Tic_Tac_Toe_AI_Lost": 0,
"Ultimate_Tic_Tac_Toe_Yourself_Lost": 0,
"Simple_Tic_Tac_Toe_AI_Lost": 0,
"Simple_Tic_Tac_Toe_Lost": 0
}
save_stats() # Save the reset stats back to file
display_stats() # Refresh the display after reset
func save_stats():
# Write current stats to the stats file
var file = FileAccess.open(stats_file_path, FileAccess.WRITE) # Use WRITE to overwrite
if file:
for key in stats.keys():
file.store_line("%s=%d" % [key, stats[key]])
file.close()
else:
print("Failed to open stats file for writing.")
func _on_XOpen_pressed():
var stats_message = generate_stats_message()
var x_url = "https://twitter.com/intent/tweet?text=%s" % url_encode(stats_message)
OS.shell_open(x_url) # Opens the URL in the default web browser
func generate_stats_message() -> String:
# Create a formatted message with your stats
var message = "Stats In Monka's Tic Tac Toe Glory\n"
message += "**WON**\n"
message += "Ultimate TTT AI Won: %d\n" % stats.get("Ultimate_Tic_Tac_Toe_AI_Won", 0)
message += "Ultimate TTT Yourself Won: %d\n" % stats.get("Ultimate_Tic_Tac_Toe_Yourself_Won", 0)
message += "Simple TTT AI Won: %d\n" % stats.get("Simple_Tic_Tac_Toe_AI_Won", 0)
message += "Simple TTT Yourself Won: %d\n\n" % stats.get("Simple_Tic_Tac_Toe_Yourself_Won", 0)
message += "**LOST**\n"
message += "Ultimate TTT AI Lost: %d\n" % stats.get("Ultimate_Tic_Tac_Toe_AI_Lost", 0)
message += "Ultimate TTT Yourself Lost: %d\n" % stats.get("Ultimate_Tic_Tac_Toe_Yourself_Lost", 0)
message += "Simple TTT AI Lost: %d\n" % stats.get("Simple_Tic_Tac_Toe_AI_Lost", 0)
message += "Simple TTT Yourself Lost: %d\n" % stats.get("Simple_Tic_Tac_Toe_Yourself_Lost", 0)
message += "#MonkasTicTacToeGlory"
return message
func url_encode(text: String) -> String:
var encoded = ""
for c in text:
if c == " ":
encoded += "%20"
elif c == "\n":
encoded += "%0A"
elif c.is_valid_identifier() or c in "_-.~": # Allowed characters in URLs
encoded += c
else:
# Use `String.get_utf8_char()` to get the UTF-8 value of the character
var ascii_value = c.to_utf8_buffer()[0] # Get the ASCII value of the character (assuming single-byte UTF-8)
encoded += "%" + String("%02X" % ascii_value) # Format as hex with two digits
return encoded
func _on_FacebookOpen_pressed():
var stats_message = generate_stats_message()
var facebook_url = "https://www.facebook.com/sharer/sharer.php?u=https://example.com"e=%s" % url_encode(stats_message)
OS.shell_open(facebook_url) # Opens the URL in the default web browser
Here is _on_FacebookOpen_pressed()
as itself:
func _on_FacebookOpen_pressed():
var stats_message = generate_stats_message()
var facebook_url = "https://www.facebook.com/sharer/sharer.php?u=https://example.com"e=%s" % url_encode(stats_message)
OS.shell_open(facebook_url) # Opens the URL in the default web browser
Any help or answers would be much appreciated, thank you!