missionpinball/mpf-docs

Update logic_blocks player_variable documentation

avanwinkle opened this issue · 2 comments

I'd like to update the documentation for logic_blocks to correctly reflect the behavior of player_variable, but am uncertain as to where the documentation/code might be incorrect.

player_variable:


By default, the current "state" (or progress) of sequence logic blocks
are stored in a :doc:`player variable </game_logic/players/index>` called
*<counter_name>_count*.
For example, a logic block called "logic_block_1" would store its state
in a player variable called *logic_block_1_count*.

However, you can use the ``player_variable:`` setting to change this to
any player variable you want.

The main difference between the documentation and code is that the code only stores the logic block in a player variable if persist_state: true is set. Without persist_state, the variable is stored only internally to the logic block. However, any attempt to access a player variable automatically instantiates that variable, so to the user it may appear as though the player variable exists with a value of 0 when in fact it was only created by the attempt to access it. Unfortunately this player value will remain at zero while the logic block value changes, which can be confounding.

if self.config['persist_state']:
    if not player.is_player_var(self.player_state_variable):
        player[self.player_state_variable] = LogicBlockState(self.get_start_value())
    self._state = player[self.player_state_variable]
else:
  self._state = LogicBlockState(self.get_start_value())

I would like to update the documentation to reflect this behavior if it is the desired behavior, otherwise I would like to update the code to instantiate and maintain the default player variable. In either case, I'd also like to note in the documentation that the default player variable name ends in _state for all logic block types and that the value of the logic block is accessible for dynamic values via devices.<blocktype>.<name>.value.

This is a tricky one. We cannot update the player variable without persist_state because it might be used in a non-game mode and then there might be no player (or the players could change turns and the value would be on the wrong player). This was different earlier but I yielded errors and confusion. So I guess the behavior is desired as it is. Any updates to the documentation are welcome.

Closing this issue because the docs have been updated/clarified in 0.50.