I've been stuck on this for over 12 hours! I've been googling and reading the Valve Networking article, reddit, gamedev topics, etc. I feel like it's time I'd rather just ask for help because I'm so confused :X
My game uses Crystal lang for the server, and Godot for the client.
My server physics step (Default Move Speed is 200). There really isn't any "physics" stuff yet, but trying to just get the basics down to sync the x position (moving towards the right)
property server_tick = 0.00
def physics_loop
delta = 0.0167 # 60 fps
loop do
players.each do |player_id, player_obj|
if player_obj.right
player_obj.position.x += (DEFAULT_MOVE_SPEED * delta)
end
# puts player_obj.position.x
end
@server_tick += delta
sleep delta
end
end
Client side input sending (In Godot). I've heard JSON is not a good choice for bidirectional communication, but honestly, just using it right now because it's keeping everything simple for me. I will most likely use something else later
var MSG_BUFFER_GAME = {}
var player_states = {right = 0}
var old_player_states = {right = 0}
var game_tick = 0
var last_message_sent_game = 0
var last_message_received_game = 0
func _input(event):
if event.is_action_pressed("d"):
player_states.right = 1
if event.is_action_released("d"):
player_states.right = 0
if old_player_states.right != player_states.right:
send_to_game_server({ right = player_states.right}, "MOVE")
old_player_states.right = player_states.right
func send_to_game_server(message, cmd = "PING"):
MSG_BUFFER_GAME.cmd = cmd
MSG_BUFFER_GAME.message = message
MSG_BUFFER_GAME.tick = ("%.2f" % game_tick)
#print("Sending.. %s" % to_json(MSG_BUFFER_GAME))
TCP_SERVER_GAME.put_utf8_string(to_json(MSG_BUFFER_GAME))
last_message_sent_game = OS.get_ticks_msec()
func _process(delta):
var speed = 200
var motion = Vector2()
if player_states.right:
motion = Vector2(1, 0)
$player.global_position += motion * (speed * delta)
game_tick += delta
func _on_tcp_timeout():
send_to_game_server("", "PING") # fake ping to sync game_tick from server
func MessageGameHandler(msg):
last_message_received_game = OS.get_ticks_msec()
var json = JSON.parse(msg).result
var cmd = json.cmd
var new_message = json.message
if cmd == "PONG": # sync local game_tick with server's
game_tick = new_message
The server's MOVE command just basically sets the player_obj.right to true or false.
The server's PING command just sends the correct game_tick:
when "PING"
client.send server_tick, "PONG"
end
The problem is, I don't know what to with this almighty game_tick value! Because when `right` is active, the x value on the server doesn't match the x value on my client (not synced). Here is a photo (server console on right).
I've read in this reddit post that I need to get the difference in the game_ticks, and "adjust accordingly"? What does that mean?
Do I need to add the game_tick difference in my _process's delta on the client, or on the server's delta? (Assuming that's what that means). My mind just goes blank at this point , any help is appreciated!