Reworked object command tables.

Object commands used to require re-adding every call in the script parent's __init__ or factory functions, adding the commands to a new command table directly on the object. Since all other attributes can be set up in at_object_creation(), this was both inconsistent and a bit confusing to work with. There is now a method add_commands() directly defined on all objects. It takes the same arguments as the normal add_command()o but use a reserved attribute to create and update a command table on the object. This has the advantange of completely removing the __init__ call in the script parent, all definitions can now be kept in at_object_creation() and are, more importantly, persistent without having to be recreated every call.
- I updated the examine command to show all the commands defined on an object (if any).
- I updated gamesrc/parents/examples/red_button.py considerably using the new command methodology and also using the updated Events.
.
Griatch
This commit is contained in:
Griatch 2009-12-03 00:41:53 +00:00
parent 5f6454ea1e
commit c7cbc4854e
7 changed files with 252 additions and 53 deletions

View file

@ -241,12 +241,16 @@ def cmd_set(command):
if not attrib_name:
source_object.emit_to("Cannot set an empty attribute name.")
return
if attrib_name == "__command_table__":
string = "This is a protected attribute, choose another name."
source_object.emit_to(string)
return
if not Attribute.objects.is_modifiable_attrib(attrib_name):
# In global_defines.py, see NOSET_ATTRIBS for protected attribute names.
source_object.emit_to("You can't modify that attribute.")
return
if attrib_value:
# An attribute value was specified, create or set the attribute.
# An attribute value was specified, create or set the attribute.
target.set_attribute(attrib_name, attrib_value)
s = "Attribute %s=%s set to '%s'" % (target_name, attrib_name, attrib_value)
else:
@ -1630,8 +1634,16 @@ def cmd_examine(command):
string += str("Location: %s" % target_obj.get_location()) + newl
# Render other attributes
cmd_string = ""
attr_string = ""
for attribute in target_obj.get_all_attributes():
string += str(attribute.get_attrline()) + newl
if attribute.get_name() == "__command_table__":
cmdtable = attribute.get_value()
cmd_string = "Commands: "
cmd_string += ", ".join(cmdtable.ctable.keys()) + newl
else:
attr_string += str(attribute.get_attrline()) + newl
string += cmd_string + attr_string
# Render Contents display.
if con_players or con_things: