Model documentation

Module containing the model base class.

class mvctools.model.BaseModel(parent, *args, **kargs)

Model base class.

Parameters:
  • parent (BaseModel or BaseControl) – the parent of the model
  • args (list) – custom arguments
  • kwargs (dict) – custom keyword arguments
Note the following:
  • To a given state corresponds a main model.
  • It is possible for a model to have children.
  • A children is automatically registered at initialization.
  • A children is automatically unregistered at deletion.
  • The state automatically update every model registered.
A subclass of BaseModel may override the following method:
  • init: called at initialization (default: create a timer self.lifetime)
  • reload: called when the state is reloaded (default: do nothing)
  • update: called at each tick of the state (default: do nothing)
An instance has the following attributes:
  • self.state: the state that uses the controller
  • self.control: the game that uses the controller
  • self.gamedata: the game data of the control
  • self.key: a unique identifier for the model
  • self.parent: the parent of the model
  • self.children: the children dictionary
  • self.isroot: True if it is the main model
__del__()

Unregister itself.

__getattr__(attr)

Handle the registering functions.

If the model doesn’t have its own corresponding handler, it will recursively look into its children.

Returns:func – a callable that recursively look into the children
Raises:AttributeError – in regular cases.

Warning: no exception is raised if no handler is found. It will be silentely ignored and False will be returned

This choice has been made on purpose, considering the controller might register more types of actions than the model can handle.

__iter__()

Iterator support.

Returns:list – the direct children.
get_model_dct()

Recursively get the dictionnary of all models with their associated key (including itself).

Returns:dict – the (key, model) dictionnary
init(*args, **kwargs)

Method to override.

Called at initialization with the same arguments as the __init__ method, but without the parent. If a model has to be initialized with custom arguments, this is where they should be handled.

The base implementation also start a timer call self.lifetime. It is a non periodic timer with no upper limit and no callback.

reload()

Empty method to override if needed.

Called when the state is reloaded.

update()

Empty method to override.

Called at each tick of the state.

Returns:bool – True to stop the current state, False otherwise.
class mvctools.model.Timer(parent, *args, **kargs)

Timer model class.

Parameters:
  • parent (BaseModel) – the parent of the timer
  • start (int) – start value for the timer (default is 0)
  • stop (int or None) – stop value for the timer (default is None for no upper limit)
  • periodic (bool) – True for the timer to start over after it reaches the stop value (default is False)
  • callback (func) – function to be called when the timer reaches the stop value (default is None)

It uses the current system FPS value to update accordingly. This way, the timer ignore lags or frame rate variations.

get(normalized=False)

Get the current value of the timer.

Parameters:normalized (bool) – normalize the value with start and stop value (default is False)
Returns:float – current value (between 0 and 1 if normalized.
get_interval()

Return the (start, stop) interval as a tuple.

init(start=0, stop=None, periodic=False, callback=None)

Initalize the timer.

Parameters:
  • parent (BaseModel) – the parent of the timer
  • start (int) – start value for the timer (default is 0)
  • stop (int or None) – stop value for the timer (default is None for no upper limit)
  • periodic (bool) – True for the timer to start over after it reaches the stop value (default is False)
  • callback (func) – function to be called when the timer reaches the stop value (default is None)
is_paused()

Return True if the timer is paused, False otherwise.

is_reset()

Return True if the timer reached its start value. Return False otherwise.

is_set()

Return True if the timer reached its stop value. Return False otherwise.

pause()

Stop the timer.

Returns:itself for affectation
reset()

Reset the timer.

Returns:itself for affectation
set(value=None)

Set the timer.

Parameters:value (float or None) – value to set the timer, set to stop if value is None
Returns:model – itself for affectation
Raises:ValueError – if value not between start and stop
start(ratio=1.0)

Start the timer.

Parameters:ratio (float) – speed in unit per seconds (default is 1.0)
Returns:itself for affectation
update()

Update the timer.

Use self.state.current_fps to update the current value accordingly. Also call the callback if needed.

mvctools.model.property_from_gamedata(name)

Build a property from an attribute name in gamedata.

Here is the behavior of this property:
  • If a read access is made, the gamedata attribute is returned.
  • If that attribute doesn’t exist, the decorated function is used as a factory, the attribute is set and the value returned.
  • If a write access is made, the dataattribute is simply set.
  • If a deletion is made, the attribute is cleared from the gamedata.

The main purpose is simplify the communication between the model and the gamedata.

Example:

@property_from_gamedata("player_score"):
def score(self):
    return 0

Previous topic

Gamedata documentation

Next topic

Resource documentation

This Page