Godot Version
4.3.stable
Question
im trying to detect when x player is the last one standing and i cant do it right, this is a multiplayer game in lan. everything works fine but i cant manage to find a way to do that and ive tried many things.
code of the player
extends CharacterBody2D
var SPEED = 130.0
const JUMP_VELOCITY = -460.0
var acc = 10.0
var dacc = 0.15
var gravity = 28
var animspd := 0.0 # animation speed
var anac := 0.15 #animation desaceleration
var anamx := 3 # animation max acceleration
var can_play := true
var jump = false
var caminando = false
var detectar_pared := true
var y = 0.0
var can_change_direction = true
var push_force = 40.0
var spawn := Vector2(0,0)
@onready var id = multiplayer.get_unique_id()
func _ready() -> void:
if not is_multiplayer_authority() :
return
$state_zone.health = 1
$Camera2D.enabled = true
spawn = position
if not multiplayer.is_server() :
await multiplayer.connected_to_server
rpc_id(1, "request_register", id)
print("requested")
else: Global_variables.alive_players.append(id)
@rpc("authority") func request_register(pid) :
print("recived")
if pid not in Global_variables.alive_players:
Global_variables.alive_players.append(pid)
func _physics_process(delta: float) -> void:
print(Global_variables.alive_players)
if not is_multiplayer_authority() :
return
$state_zone/AnimationPlayer.play("normal mario hitbox")
$AnimatedSprite2D.speed_scale = animspd
y = abs(velocity.y)
if abs(velocity.x) >= 10 and can_play == true and not is_on_wall():
$AnimatedSprite2D.play("walk")
if abs(velocity.x) <= 16 and not jump:
$AnimatedSprite2D.play("idle")
if abs(velocity.x) >= 130 and Input.is_action_just_pressed("d") and is_on_floor():
caminando = true
$AnimatedSprite2D.play("change_direction")
can_play = false
$Timer.start()
detectar_pared = false
else : detectar_pared = true
if abs(velocity.x) >= 130 and Input.is_action_just_pressed("a") and is_on_floor():
can_play = false
caminando = true
$AnimatedSprite2D.play("change_direction")
$Timer.start()
detectar_pared = false
else : detectar_pared = true
velocity.y += gravity
if Input.is_action_just_pressed("salto") and is_on_floor():
velocity.y = JUMP_VELOCITY
$"Mario-jump-sound".play()
jump = true
if jump == true and not is_on_floor(): $AnimatedSprite2D.play("jump")
if is_on_floor() and not Input.is_action_just_pressed("salto") : jump= false
if Input.is_action_pressed("a") :
$AnimatedSprite2D.flip_h = can_change_direction
velocity.x = max(velocity.x - acc, -SPEED)
animspd = max(animspd - anac, -anamx)
elif Input.is_action_pressed("d") :
$AnimatedSprite2D.flip_h = can_change_direction
velocity.x = min(velocity.x + acc, SPEED)
animspd = min(animspd + anac, anamx)
else:
velocity.x = lerp(velocity.x, 0.0, dacc)
animspd = lerp(animspd, 0.0, anac)
if is_on_floor() and Input.is_action_pressed("a"):
can_change_direction = true
if is_on_floor() and Input.is_action_pressed("d"):
can_change_direction = false
if Input.is_action_pressed("correr"):
anamx = 5.5
SPEED = 200
acc = 8
dacc = 0.1
else:
acc = 10
anamx = 3
SPEED = 130
dacc = 0.15
if $state_zone.health > 0:
move_and_slide()
# after calling move_and_slide()
for i in get_slide_collision_count():
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal() * push_force)
func _on_timer_timeout() -> void:
can_play = true
func _on_timer_2_timeout() -> void:
pass # Replace with function body.
func _on_starman_timer_timeout() -> void:
pass # Replace with function body.
# Damage detection
func _on_player_area_area_entered(area: Area2D) -> void:
if area.is_in_group("damage_area") and not $state_zone.invunerable and not $state_zone.is_multiplayer :
$state_zone.health -= area.damage
death()
func death():
if $state_zone.health <= 0:
$"Mario-jump-sound".volume_db = -1000
$"death sfx".play()
$AnimationPlayer.play("death")
$"death timer".start()
$state_zone.invunerable = true
func _on_death_timer_timeout() -> void:
get_tree().reload_current_scene()
code of the scene
extends Node2D
const PLAYER = preload("res://mario.tscn")
var peer = ENetMultiplayerPeer.new()
var players: Array[Player] = []
@onready var iptextbox = $UI/ip_textbox
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
$MultiplayerSpawner.spawn_function = add_player
if Global_variables.join_or_host == "host" :
$Camera2D.enabled = false
peer.create_server(Global_variables.port)
multiplayer.multiplayer_peer = peer
multiplayer.peer_connected.connect(
func(pid) :
print("Peer " +str(pid) + " has joined the game")
$MultiplayerSpawner.spawn(pid)
)
$MultiplayerSpawner.spawn(multiplayer.get_unique_id())
if Global_variables.join_or_host == "join" :
$Camera2D.enabled = false
peer.create_client(Global_variables.ip, Global_variables.port)
multiplayer.multiplayer_peer = peer
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if Global_variables.can_music_play :
$Music.stream_paused = false
else :
$Music.stream_paused = true
print(Global_variables.alive_players)
func _on_host_pressed() -> void:
$Camera2D.enabled = false
peer.create_server(int($UI/host_port_textbox.text))
multiplayer.multiplayer_peer = peer
multiplayer.peer_connected.connect(
func(pid) :
print("Peer " +str(pid) + " has joined the game")
$MultiplayerSpawner.spawn(pid)
)
$MultiplayerSpawner.spawn(multiplayer.get_unique_id())
func _on_join_pressed() -> void:
$Camera2D.enabled = false
peer.create_client(iptextbox.text, int($UI/port_textbox.text))
multiplayer.multiplayer_peer = peer
func add_player(pid) :
print("SPAWNNNNNNNNNNNNNN")
var player = PLAYER.instantiate()
player.name = str(pid)
player.global_position = $Level.get_child(players.size()).global_position
players.append(player)
return player
func _on_initial_wait_timeout() -> void:
pass # Replace with function body.