Skip to content

Instantly share code, notes, and snippets.

extends HTTPRequest
class_name HTTPFormPost
func example():
post_files("https://example.com", [
FileDef.new().from_file("user://test.txt", "file")
], {
"other_field": "test"
})
extends HTTPRequest
class_name HTTPFilePost
func post_file(url: String, field_name: String, file_name: String, file_path: String, post_fields: Dictionary = {}, content_type: String = "", custom_headers: Array = [], verify_ssl: bool = true):
var file = File.new()
file.open(file_path)
var content = file.get_buffer(file.get_len())
file.close()
post_data_as_file(url, field_name, file_name, content, post_fields, content_type, custom_headers, verify_ssl)
@nisovin
nisovin / Godot Web Server Configs.md
Last active September 8, 2024 04:27
Godot Web Server Configs

Godot Web Server Configs

For Godot 4 and the threads export in Godot 3, you need to set special headers on your web server in order to enable SharedArrayBuffer. There are some examples here for various web servers.

Python

The script below can be used to start a web server that sets the required headers. The easiest way to use this is to place this python script in your export folder and double click it to start the server. Then you can view your game at http://localhost:8000.

@nisovin
nisovin / GlobalInput.cs
Created August 18, 2022 05:24
Godot code for getting mouse and keyboard input while window is not focused
using Godot;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class GlobalInput : Godot.Node
{
private Dictionary<int, string> keyWatches = new Dictionary<int, string>();
private Dictionary<int, bool> keyStates = new Dictionary<int, bool>();
@nisovin
nisovin / ScreenMouse.cs
Last active April 29, 2022 21:50
Godot Screen Mouse Position
using Godot;
using System.Runtime.InteropServices;
// This script should be added as a Singleton in Godot
public class ScreenMouse : Godot.Node
{
public Vector2 mouse_position {
get {
return GetCursorPosition();
@nisovin
nisovin / Example_WebServer_Usage.gd
Last active April 20, 2022 22:24
GDScript Web Server
extends Node
const HTTP_PORT = 8888
var server
func _ready():
# Create the HTTP server
server = WebServer.new()

Godot Threads on Itch.io

UPDATE

As of Feb 2023, itch.io supports SharedArrayBuffer on Firefox, so this document should be unnecessary.

Introduction

Itch.io has recently implemented the ability to use SharedArrayBuffer on its site, which means you can export games with Threads enabled for HTML5. You can learn more about this here. This is great for Godot, as it helps to improve/fix the sound laggyness that sometimes occurs in web builds. It is also now required for Godot 4.

@nisovin
nisovin / random_point_in_shape.gd
Last active April 17, 2022 19:24
Find a random point in a collision shape
var rng := RandomNumberGenerator.new()
func _ready():
rng.randomize()
# example usage
var shape = $CollisionShape2D
for i in 500:
var sprite = Sprite.new()
sprite.texture = load("res://icon.png")
@nisovin
nisovin / GodotWebFilesExample.gd
Last active May 30, 2022 14:02
Godot Web File Open/Save Dialogs
extends Control
func _ready():
WebFiles.connect("file_opened", self, "_on_file_opened")
func _on_Button_pressed():
WebFiles.open_file(".jpg,.jpeg")
func _on_file_opened(file, content):
$Label.text = file
@nisovin
nisovin / LANFinder.gd
Last active March 7, 2021 02:41
Use Godot's UDP multicast feature to find host games on the LAN. Largely untested.
extends Node
class_name LANFinder
const MULTICAST_ADDRESS = "239.10.10.10"
const MULTICAST_PORT = 33221
signal host_found(ip, info)
signal stopped()
var udp := PacketPeerUDP.new()