Sub heading
Computer augmented development with AI.
Here a style WP Pattern as template for “conversation”
expected end of statement var, got ? instead
– ME
Certainly! The conditional operator
?
is not valid in GDScript, so we need to replace those lines with standardif
statements. Here’s the corrected version of the Base64 encoding function:Complete Script with Fixed Base64 Encoding
extends Node # Replace with your WordPress site URL const WORDPRESS_URL = "https://your-wordpress-site.com/wp-json/wp/v2/comments" # Replace with your WordPress username and application password const USERNAME = "your_username" const APP_PASSWORD = "your_application_password" func _ready(): # Call the function to submit a comment submit_comment("Your Name", "Your comment text here", "post_id_or_url") func submit_comment(name: String, comment: String, post_id: String): var http_request = HTTPRequest.new() add_child(http_request) var headers = [ "Authorization: Basic " + base64_encode(USERNAME + ":" + APP_PASSWORD), "Content-Type: application/json" ] var body = { "post": post_id, # Use the post ID or URL "author_name": name, "content": comment } var json_body = JSON.print(body) var err = http_request.request(WORDPRESS_URL, headers, true, HTTPClient.METHOD_POST, json_body) if err != OK: print("Error sending request: ", err) return # Connect the request_completed signal to handle the response http_request.connect("request_completed", self, "_on_request_completed") func _on_request_completed(result: int, response_code: int, headers: Array, body: PoolByteArray): if response_code == 201: print("Comment submitted successfully!") else: print("Failed to submit comment. Response code: ", response_code) # Custom Base64 encoding function func base64_encode(data: String) -> String: var bytes = data.to_utf8() var base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" var output = "" # Process the input bytes in chunks of 3 bytes for i in range(0, bytes.size(), 3): var b1 = bytes[i] var b2 = 0 var b3 = 0 if i + 1 < bytes.size(): b2 = bytes[i + 1] if i + 2 < bytes.size(): b3 = bytes[i + 2] var combined = (b1 << 16) | (b2 << 8) | b3 output += base64_chars[(combined >> 18) & 0x3F] output += base64_chars[(combined >> 12) & 0x3F] # Check if we have the second byte if i + 1 < bytes.size(): output += base64_chars[(combined >> 6) & 0x3F] else: output += "=" # Padding # Check if we have the third byte if i + 2 < bytes.size(): output += base64_chars[combined & 0x3F] else: output += "=" # Padding return output
Key Changes
- Replaced Conditional Operators: The lines that were using the conditional operator
?
have been replaced withif
statements to check if the second and third bytes are available. If they are not, it appends"="
for padding.- Padding Logic: The padding logic is now clearly separated into
if
statements, making it easier to read and understand.Important Notes
- Make sure to replace
"post_id_or_url"
with the actual post ID or URL where you want to submit the comment.- Test the script in your Godot project to ensure it works as expected.
This implementation should now work correctly for submitting comments to your WordPress site from your Godot application.
. GPT-4o mini
You did it!
– ME
I’m a solo dev. I want to develop in PHP backend for Godot apps (games). My PHP server is on shared hosting and I can’t and don’t want to add more libraries to it
Leave a Reply