I know that static variables apply to an object and an instance variable (usually under the __init__() function) applies to an instance but a question came to mind:
When and where should static variables and instance variables be used in a game? Also, if you change the value of an object's static variable, does it apply to all instances of that object?
Instance attributes should be used if the attributes are to be unique to the instances (which is the case most of the time). Class attributes can be used if the attributes should be shared among all instances, for example if you want to store constants that are related to this class. An example that comes to mind are the states of an entity (finite state machine):
from enum import Enum
class Entity:
# The instances don't need their own `states` enumeration,
# so I create one which will be shared among all instances.
STATES = Enum('State', 'IDLING WALKING JUMPING')
def __init__(self):
self.state = self.STATES.JUMPING
def update(self):
if self.state is self.STATES.JUMPING:
print('jumping')
entity = Entity()
entity.update()
Take care of mutable class attributes, because when you modify them, you modify them for all instances.