+2 votes

hi, i want to show live score on the two screens of two players who play my fighting game ;)
i have two problems here:
1- sometimes character feels the punch (it shows with animation), but i dont see any score for that.
2- sometimes it shows two point for one punch (i disable monitoring/monitarable in animation player when the punch shape goes back, so there is no chance that this problem happens when shape is going back or something like that)

and i should mention that there isn't any problem in offline mode and it works perfectly.

this is the script for counting and showing scores on screen:

extends Node2D

onready var player1 = get_node("Player01")
onready var player2 = get_node("Player02")
onready var p1score = get_node("P01Score/Point01")
onready var p2score = get_node("P02Score/Point02")
onready var game_countdown = get_node("GameCountDown")
onready var timer_label = get_node("TimerLabel")


var count1 = 0
var count2 = 0
var minutes = 1
var seconds = 60

func _ready():
    game_countdown.start()
    if (get_tree().is_network_server()):
        player2.set_network_master(get_tree().get_network_connected_peers()[0])
    else:
        player2.set_network_master(get_tree().get_network_unique_id())
    print("unique id: ",get_tree().get_network_unique_id())

func _process(delta):
        score_update()
        rset("count1", player2.counter)
        rset("count2", player1.counter)

sync func score_update():
    if get_tree().is_network_server():
        if str(player2.counter) != p1score.text:
            count1 = player2.counter  #counter which i get from collision function
            p1score.set_text(str(player2.counter))
    else:
        if str(player1.counter) != p2score.text:
            count2 = player1.counter  #counter which i get from collision function
            p2score.set_text(str(player1.counter))

func _on_GameCountDown_timeout():  #this one is just a countdown for the game
     if seconds >=0 && minutes == 1:
        seconds -= 1
        timer_label.set_text(str(minutes," :",seconds))
     if seconds >=0 && minutes == 0:
        seconds -= 1
        timer_label.set_text(str(minutes," :",seconds))
     if seconds == -1 && minutes == 1:
        seconds = 59
        minutes = 0
        timer_label.set_text(str(minutes," :",seconds))

and if you curious about that how scores send to script above, i use function like this one in players scripts:

sync func _on_Fist_area_entered(area):
     if head_fr.get_collision_mask() == 2 && block == 0:
        animations.play("Hurt")
        counter+=1   # we get this one for counting score

THANK YOU :)

in Engine by (76 points)
edited by

1 Answer

+1 vote

hello,
I think you may do some confusion in your code.
If I understand correctly, this code is running on all clients and server. So, your update will be called by the salves as well.
The following code should only be done by the master:

rset("count1", player2.counter)
rset("count2", player1.counter)

Then, the slaves will see their "count1" and "count2" by synchronized. By the way, count1 should not be player1.counter?

In general, don't change count1 and count2 on the slaves, only read these values. So try to separate the logic of the slaves from the logic of the master.

by (64 points)

nice, don't hesitate to ask if you have more question.

hi dant4..
im thinking about how transfer area2Ds to server.
could i instance my players to ring, and then add area2Ds to them for hit/hurt box? does it mean area2Ds are outside of clients reach?

Ring node

Player01 (Instanced Node)

Area2D - 01
Area2D - 02
.
.
.

or i should create area2Ds in ring and make them follow player head and fists? but i think this one make a big lag in game and we have many missed collisions.

sry to bother you and thank you for your time :)

I think you should keep the area on the client as well, because you will need them to play some local fx/feedbzck when a hit happens.but only for fx!!
About how to do it, I can have a look this weekend unless someone kowns

i dont wanna bother you dant 4. ive managed to do this before and players detected and sent collision signals. i just need some guidance about how to do these things to make game secure, I really appreciate your help.. :)

Networking is all about hidding the latency and minimizing your packets size.
When you are more familiar with that, you can think advanced anti-cheat. It will probably increase your packet size lol, so you can start over and optimize again. Only keep in mind, that you can't let the client tell the result (= don't send the target of the shoot, but the input of the shoot)
If you want to learn more about this, you can have a look about the GGPO middleware
https://en.wikipedia.org/wiki/GGPO

It's what almost all fighting game use.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.