Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node
- @export var enabled = true
- @export var _replicatedNode: NodePath
- @onready var replicatedNode:Node = get_node(_replicatedNode)
- @export var replicatedValues:Array[String] = []
- # should be renamed packet_id
- var network_id = 0
- var last_recieved_network_id = -1
- var update_override = false
- func _ready():
- var parent = self.get_parent()
- if NetworkUtil.is_multiplayer_active() and not parent.is_multiplayer_authority() and enabled:
- rpc("requestUpdate")
- var old_rep:Array
- # Called when the node enters the scene tree for the first time.
- func _process(_delta):
- var parent = self.get_parent()
- if NetworkUtil.is_multiplayer_active() and parent.is_multiplayer_authority() and enabled:
- var rep:Array = prep_replicate_values()
- if old_rep == null:
- old_rep = rep
- if update_override or not compareRepArrays(rep, old_rep):
- #FIXME
- #rpc_unreliable("replicate_properties", rep, network_id)
- rpc("replicate_properties", rep, network_id)
- update_override = false
- old_rep = rep
- network_id += 1
- if network_id > 1000000000:
- network_id = 0
- func prep_replicate_values() -> Array:
- var rep_values = null
- var node = get_node(_replicatedNode)
- if node:
- var size = replicatedValues.size()
- if size > 0:
- rep_values = []
- rep_values.resize(size)
- for i in range(size):
- if replicatedValues[i] in node:
- rep_values[i] = replicatedNode[replicatedValues[i]]
- pass
- return rep_values
- func compareRepArrays(arr1, arr2):
- var size = arr1.size()
- if size == arr2.size():
- for i in range(size):
- if arr1 != arr2:
- return false
- return true
- @rpc func requestUpdate():
- update_override = true
- @rpc("any_peer") func replicate_properties(values:Array, net_id):
- if(last_recieved_network_id < net_id or net_id+100000 < last_recieved_network_id):
- last_recieved_network_id = net_id
- #self.get_parent().set_global_transform(g_transform)
- for i in range(values.size()):
- replicatedNode[replicatedValues[i]] = values[i]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement