pythonraspberry-pi3godotgdscriptgodot4

Trying to connect to a raspberry pi web socket with GDScript


I wrote Python code on a Raspberry Pi that sends data when the sound sensor is triggered. when I connect to it using Python script everything works fine but for some reason Godot 4 doesn't recognize buttons pressed through t keyboard.press_and_release() so I thought about making the connection directly in Godot's gdscript. For some reason, the status is always connecting even when the serverside says it is connected.

I'm using WebSocketPeer in GDScript to connect to the URL. if there are any alternatives I would love some advice or maybe just a way to register the responses received by the py script as Godot input

Solution

i created a global file that connects to the pi server when the game starts:

extends Node

var players = []
var scene
const port = 8880
var ip = "192.168.4.1"
var connection 

func _ready():
    connection = StreamPeerTCP.new()
    connection.connect_to_host( ip,port )

then I just use the responses and simulate buttons where I need:

func _process(delta):
    GameData.connection.poll()
    
    var state = GameData.connection.get_status()
    
    if GameData.connection and state == GameData.connection.STATUS_CONNECTED:
        if GameData.connection.get_available_bytes() > 0:
            var s = GameData.connection.get_utf8_string(GameData.connection.get_available_bytes())
            print(str(s))
            if str(s) == "1":
                Input.action_press("1")
            elif str(s) == " ":
                Input.action_press("space")

Solution

  • The answer is in the question as well now just to make it easier to find but ill post it here:

    i created a global file that connects to the pi server when the game starts:

    extends Node
    
    var players = []
    var scene
    const port = 8880
    var ip = "192.168.4.1"
    var connection 
    
    func _ready():
        connection = StreamPeerTCP.new()
        connection.connect_to_host( ip,port )
    

    then I just use the responses and simulate buttons where I need:

    func _process(delta):
        GameData.connection.poll()
        
        var state = GameData.connection.get_status()
        
        if GameData.connection and state == GameData.connection.STATUS_CONNECTED:
            if GameData.connection.get_available_bytes() > 0:
                var s = GameData.connection.get_utf8_string(GameData.connection.get_available_bytes())
                print(str(s))
                if str(s) == "1":
                    Input.action_press("1")
                elif str(s) == " ":
                    Input.action_press("space")