Skip to content

ElSuicio/Shared-Memory-Godot

Repository files navigation

Shared Memory - Godot

Warning

This addon does not provide synchronization methods. Concurrent access must be coordinated by the user.

This is an addon for Godot 4.4+ that adds the SharedMemory class for creating, opening and directly accessing shared memory segments for inter-process communication (IPC).

Binary downloads

The official binaries for Godot can be found at itch.io.

How to use

Write (Godot → Python):

# Godot Shared Memory Example.
extends Node

const NAME : String = "SharedMemoryExample"
const SIZE : int = 1024

func _ready() -> void:
	var shm : SharedMemory = SharedMemory.new()

	if shm.create(NAME, SIZE, SharedMemory.LOCAL_SCOPE) == OK:
		shm.write(("Hello world from Godot Shared Memory!").to_utf8_buffer())
	
	await get_tree().create_timer(10).timeout
	
	shm.close()
	shm.unlink()

Read (Python):

# Python Shared Memory Example.
from multiprocessing import shared_memory
from multiprocessing import resource_tracker

NAME : str = "SharedMemoryExample"
SIZE : int = 1024

def main() -> None:
	shm : shared_memory.SharedMemory = shared_memory.SharedMemory(name = NAME, create = False, size = SIZE)
    
	resource_tracker.unregister(shm._name, 'shared_memory')
    
	data : bytes = bytes(shm.buf[:]).rstrip(b'\x00')

	print(data.decode("utf-8"))

	shm.close()

if __name__ == "__main__":
    main()
Hello world from Godot Shared Memory!
Write (Python → Godot):
# Python Shared Memory Example.
from multiprocessing import shared_memory
import time

NAME : str = "SharedMemoryExample"
SIZE : int = 1024

def main() -> None:
	shm : shared_memory.SharedMemory = shared_memory.SharedMemory(name = NAME, create = True, size = SIZE)

	data : bytes = b'Hello world from Python Shared Memory!'

	shm.buf[0:len(data)] = data

	time.sleep(10)

	shm.close()
	shm.unlink()

if __name__ == "__main__":
    main()
Read (Godot):
# Godot Shared Memory Example.
extends Node

const NAME : String = "SharedMemoryExample"
const SIZE : int = 1024

func rstrip(buffer : PackedByteArray) -> PackedByteArray:
	var out : PackedByteArray = buffer.duplicate()
	
	while out.size() > 0 and out[out.size() - 1] == 0:
		out.resize(out.size() - 1)
	
	return out

func _ready() -> void:
	var shm : SharedMemory = SharedMemory.new()
	
	if shm.open(NAME, SIZE) == OK:
		var data : PackedByteArray = shm.read()
		data = rstrip(data)
		print(data.get_string_from_utf8())
	
	shm.close()
Hello world from Python Shared Memory!

Compiling from source

Requirements

How to build

Build with SCons

From the repository root, run:

scons platform=<platform> target=<target> precision=<precision> arch=<architecture>

Example:

scons platform=windows target=template_debug precision=single arch=x86_64

Build with CMake

From the repository root, run:

Note

The default CMake generator depends on your platform:

  • Windows: Visual Studio (multi-config).
  • Linux / macOS: Makefiles or Ninja (single-config).

Ninja is recommended on Linux / macOS for faster builds and better tooling integration but it is not required.

Basic build (default generator)

cmake -S ./ -B build -DGODOTCPP_TARGET=<target choice>
cmake --build build

Example:

cmake -S ./ -B build -DGODOTCPP_TARGET=template_debug
cmake --build build

Multi-config generators:

# Debug
cmake -S ./ -B build -A x64 -DGODOTCPP_TARGET=template_debug
cmake --build build --config Debug

# Release
cmake -S ./ -B build -A x64 -DGODOTCPP_TARGET=template_release
cmake --build build --config Release

Single-config generators:

# Debug
cmake -S ./ -B build -G Ninja -DGODOTCPP_TARGET=template_debug -DCMAKE_BUILD_TYPE=Debug
cmake --build build

# Release
cmake -S ./ -B build -G Ninja -DGODOTCPP_TARGET=template_release -DCMAKE_BUILD_TYPE=Release
cmake --build build

More details

Links & Support

If you liked it and want to support it, you can donate via PayPal. Your support is very much appreciated and helps me keep going!

[ Contact Email: interdreamsoft@gmail.com ]

About

Shared-Memory IPC for Godot (GDExtension).

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors