I've been making a multiplayer game of TicTacToe to practice skills and to get into client-server model. Now I'm having trouble sending objects using pickle, because when they get unpickled none of the changes to object attributes are made. I only get init values.
Both client and server receives and sends data correctly because when I try to send built-in types everything works as far as I tested. I have no idea how to resolve this issue.
Code is split into 5 files and I will only post the code of the problematic Class.
import pygame as pg
class Field(pg.Rect):
def __init__(self, left, top, width, height, field_id):
super().__init__(left, top, width, height)
self.field_id = field_id
self.empty_flag = True
self.sign = None
def __reduce__(self):
return (self.__class__, (self.left, self.top, self.width, self.height, self.field_id))
def __str__(self):
return str(self.field_id)
def set_sign(self, sign=None):
self.sign = sign
def draw_field(self, window):
pg.draw.rect(window, pg.Color("red"), self)
def field_pressed(self, mouse_pos, sign):
if self.collidepoint(mouse_pos[0], mouse_pos[1]) and self.empty_flag:
self.play_field(sign)
return True
else:
return False
def play_field(self, sign):
self.empty_flag = False
self.set_sign(sign)
def get_id(self):
return self.field_id
def get_sign(self):
return self.sign
def get_empty_flag(self):
return self.empty_flag
So, during the game, self.sign is being changed to one of the values "x" or "o" and based on that the images are being blit into the screen. When Player1 makes move e.g. self.sign = "x", after is sent it is self.sign = None.