L
lookingforanswer1

  • Feb 9, 2023
  • Joined Jan 7, 2023
  • 0 best answers
  • Hello. I trying to create simple air hockey game with WebRTC multiplayer with Nakama. To makes it easier for me, I decided to add my game over existing source code, because doing the other way around was too difficult for me. While I want to add only single Puck for both players, existing code want to do it twice and after creating one Puck on the board, he fails to do it twice with following error: add_child: Can't add child 'Puck' to 'PlayersSpawnUnder', already has a parent 'PlayersSpawnUnder'.
    With current code it works, but Puck is really laggy and can be in wrong position sometimes.

    Source code - Link

    GameManager.gd

    extends Node2D
    export var player : PackedScene
    var Puck = preload("res://Puck.tscn").instance()
    var ReadyPlayers = {}
    func _ready():
    	setupGame()
    	pass # Replace with function body.
    
    func setupGame():
    	for id in GameManager.Players:
    		var currentPlayer = player.instance()
    		var currentPuck = Puck
    		currentPlayer.name = str(id)
    		$PlayersSpawnUnder.add_child(currentPlayer)
    		$PlayersSpawnUnder.add_child(currentPuck)
    		currentPlayer.set_network_master(GameManager.Players[id].peer_id)
    		currentPlayer.position = get_node("SpawnPlayerPositions/" + str(GameManager.Players[id].peer_id)).position
    		currentPuck.position = get_node("SpawnPlayerPositions/3").position
    
    	var myID = OnlineMatch.get_my_session_id()
    	var player = $PlayersSpawnUnder.get_node(str(myID))
    	player.playerControlled = true
    	rpc_id(1, "finishedSetup", myID)
    	
    mastersync func finishedSetup(id):
    	ReadyPlayers[id] = GameManager.Players[id]
    	if ReadyPlayers.size() == GameManager.Players.size():
    		print("start game all players are ready")`

    Player.gd

    extends KinematicBody2D
    
    export (bool) var playerControlled = false
    var updateFrames = 6
    var currentFrames = 0
    
    # Called when the node enters the scene tree for the first time.
    func _ready():
    	pass # Replace with function body.
    
    func _physics_process(delta):
    	if playerControlled:
    		self.position = get_global_mouse_position()
    		look_at(get_global_mouse_position())
    		
    #		if Input.is_action_just_pressed("shoot"):
    #			rpc("shoot", $Position2D.global_transform, name)
    #			pass
    		currentFrames += 1
    		if updateFrames <= currentFrames:
    			rpc("UpdateRemotePlayers", position, rotation)
    			currentFrames = 0
    
    #remotesync func shoot(shootPos, playerWhoShot):
    #	var bullet = Bullet.instance()
    #	get_tree().get_nodes_in_group("GameWorld")[0].add_child(bullet)
    #	bullet.transform = shootPos
    #	bullet.playerWhoShot = playerWhoShot
    
    
    puppet func UpdateRemotePlayers(currentpos, currentRotation):
    	$Tween.interpolate_property(self, "position", global_position, currentpos, .1, Tween.TRANS_LINEAR)
    	$Tween.start()
    	$Tween2.interpolate_property(self, "rotation", rotation, currentRotation, .1, Tween.TRANS_LINEAR)
    	$Tween2.start()

    Puck.gd

    extends RigidBody2D
    
    export (bool) var playerControlled = false
    var updateFrames = 6
    var currentFrames = 0
    
    
    func _physics_process(delta):
    	currentFrames += 1
    	if updateFrames <= currentFrames:
    		rpc("UpdateRemotePuck", position, rotation)
    		currentFrames = 0
    
    
    # Called when the node enters the scene tree for the first time.
    func _ready() -> void:
    	pass # Replace with function body.
    
    puppet func UpdateRemotePuck(currentpos, currentRotation):
    	$Tween.interpolate_property(self, "position", global_position, currentpos, .1, Tween.TRANS_LINEAR)
    	$Tween.start()
    	$Tween2.interpolate_property(self, "rotation", rotation, currentRotation, .1, Tween.TRANS_LINEAR)
    	$Tween2.start()

    Any help is appreciated!

  • Hello. I trying to create simple air hockey game watching "Godot Over WebRTC! Using Nakama Matchmaking With Godot!" tutorial. To makes it easier for me, I decided to add my game over existing source code, because doing the other way around was too difficult for me. Here is my current version, where I cannot understand how to add shared and synced Puck object(RigidBody2D) for both players.

    Commented code in this image = my futile efforts to make it work.
    My project in archive - link
    In order to test it you have to run local Nakama server via Docker Desktop and launch 2 game instances via
    Thanks for your attention!

  • DaveTheCoder
    Yes, even that is not executing.

    Regarding code, honestly I don't quite get this part of GDscript, gonna look into that.

  • Hello! I'm trying to make a simple air hockey game(while grasping the basics) in Godot 3.5.1, but stuck with this signal issue.

    My current goal is to send current speed of player_paddle to a puck("ball" in my project) in order to change speed of puck object.

    ball.gd

    extends KinematicBody2D
    
    var ball_speed = 100
    var direction = Vector2(1,1)
    var pdl1_speed = 100
    
    func _physics_process(delta):
    
    var velocity = pdl1_speed * direction * delta
    var collision = move_and_collide(velocity)
    
    if collision != null:
    	
    	direction = direction.bounce(collision.normal)
    
    func _on_player_paddle_sent_speed(paddle_speed) -> void:
    pdl1_speed = paddle_speed
    print("Signal connected")

    player_paddle.gd

    extends KinematicBody2D
    
    signal sent_speed(paddle_speed)
    
    var move_direction = Vector2(0,0)
    var velocity = 0
    var speed = 2000
    
    func _physics_process(delta):
    move_direction.x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
    move_direction.y = (int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))) / float(2)
    var motion = move_direction.normalized() * speed
    velocity = motion
    move_and_slide(velocity)
    
    func _on_Area2D_body_entered(body):
    if body.name == "ball":
    emit_signal("sent_speed", speed)
    print("hit detected")

    But no matter what, on collision ball speed is still the same.