Advertisement
SKaminari

packet.gd

Oct 25th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Object
  2.  
  3. var action: String
  4. var payloads: Array
  5.  
  6. func _init(_action: String, _payloads: Array) -> void:
  7.     action = _action
  8.     payloads = _payloads
  9.  
  10. func tostring() -> String:
  11.     var serialize_dict: Dictionary = {"a": action}
  12.  
  13.     for i in range(len(payloads)):
  14.         serialize_dict["p%d" % i] = payloads[i]
  15.    
  16.     var data: String = JSON.stringify(serialize_dict)
  17.     return data
  18.  
  19. static func json_to_action_payloads(json_str: String) -> Array:
  20.     var jaction: String
  21.     var jpayloads: Array = []
  22.     var obj_dict: Dictionary = JSON.parse_string(json_str)
  23.    
  24.     for key in obj_dict.keys():
  25.         var value = obj_dict[key]
  26.  
  27.         if key == "a":
  28.             jaction = value
  29.        
  30.         elif key[0] == "p":
  31.             var index: int = key.split_floats("p", true)[1]
  32.             jpayloads.insert(index, value)
  33.  
  34.     return [jaction, jpayloads]
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement