# HG changeset patch # User Beliar # Date 1330609749 -3600 # Node ID 3abd31885f0f0ea5a31455898ac8b9f8e7027399 # Parent 5d47ad053aefb39d6a23c7c03b80efd180048295 Added a method to the ScriptingSystem that returns the environment(locals, globals) in which scripts are run. diff -r 5d47ad053aef -r 3abd31885f0f systems/scriptingsystem.py --- a/systems/scriptingsystem.py Sun Feb 26 01:58:36 2012 +0100 +++ b/systems/scriptingsystem.py Thu Mar 01 14:49:09 2012 +0100 @@ -50,8 +50,7 @@ action_data = self.running_actions.popleft() action = self.system.actions[action_data[0]] action_params = eval(action_data[1], - self.system.funcs, - self.system.vals + *self.system.getEnvironment() ) if not (isinstance(action_params, list) or isinstance(action_params, tuple)): @@ -60,7 +59,7 @@ self.wait = action_data[2] if len(action_data) >= 4: vals = ( - eval(action_data[4], self.system.funcs, self.system.vals) + eval(action_data[4], *self.system.getEnvironment()) if len(action_data) > 4 else () ) @@ -84,12 +83,12 @@ def __init__(self, commands, actions): """Constructor""" - self.funcs = {} + self.__funcs = {} self.common_funcs = { "sqrt":math.sqrt, "log":math.log, } - self.vals = {} + self.__vals = {} self.commands = commands self.actions = actions self.game_state = None @@ -100,7 +99,18 @@ self.scripts = {} self.conditions = [] + def getEnvironment(self): + """Returns the globals and locals used by the scripting system""" + return self.__vals, self.__funcs + + def updateEnvironment(self): + self.__vals.clear() + self.__vals.update(self.game_state.getObjectDictOfMap(self.game_state.current_map_name)) + self.__funcs.clear() + self.__funcs.update(self.game_state.funcs) + self.__funcs.update(self.common_funcs) + def step(self, dt): """Execute a time step for the system. Must be defined by all system classes. @@ -108,21 +118,14 @@ :param dt: Time since last step invocation :type dt: float """ - self.vals.clear() - self.vals.update( - self.game_state.getObjectDictOfMap( - self.game_state.current_map_name) - ) - self.funcs.clear() - self.funcs.update(self.game_state.funcs) - self.funcs.update(self.common_funcs) + self.updateEnvironment() for condition_data in self.conditions: condition = condition_data[0] script_name = condition_data[1] if not self.scripts.has_key(script_name): return script = self.scripts[script_name] - if eval(condition, self.funcs, self.vals) and not script.running: + if eval(condition, *self.getEnvironment()) and not script.running: script.running = True for script in self.scripts.itervalues(): assert(isinstance(script, Script))