- Made many small bugfixes to the @parent and @create functions as well as their underlying methods.

- Made it so user #1 is also affected by the on_player_creation() function.
- Added an event folder for custom events, including a working example
- Expanded the example commands and parents to include the changes to how they should be initialized.
- Added an optional ansi scheme (not active by default)
This commit is contained in:
Griatch 2009-04-25 20:51:12 +00:00
parent a32840002c
commit a9dbac8aae
16 changed files with 397 additions and 35 deletions

View file

@ -19,4 +19,4 @@ def class_factory(source_obj):
source_obj: (Object) A reference to the object being scripted (the child).
"""
return BasicObject(source_obj)
return BasicObject(source_obj)

View file

@ -0,0 +1,55 @@
"""
Simple example of a custom modified object, derived from the base object.
If you want to make this your new default object type, move this into
gamesrc/parents and set SCRIPT_DEFAULT_OBJECT = 'custom_basicobject'
in game/settings.py.
Generally, if you want to conveniently set future objects to inherit from this
script parent (not as a default), this files and others like it need to be
located under the game/gamesrc/parent directory.
"""
from game.gamesrc.parents.base.basicobject import BasicObject
class CustomBasicObject(BasicObject):
def at_object_creation(self):
"""
This function is called whenever the object is created. Use
this instead of __init__ to set start attributes etc on a
particular object type.
"""
#Set an "sdesc" (short description) attribute on object,
#defaulting to its given name
#get the stored object related to this class
obj = self.scripted_obj
#find out the object's name
name = obj.get_name(fullname=False,
show_dbref=False,
show_flags=False)
#assign the name to the new attribute
obj.set_attribute('sdesc',name)
def at_object_destruction(self, pobject=None):
"""
This is triggered when an object is about to be destroyed via
@destroy ONLY. If an object is deleted via delete(), it is assumed
that this method is to be skipped.
values:
* pobject: (Object) The object requesting the action.
"""
pass
def class_factory(source_obj):
"""
This method is called by any script you retrieve (via the scripthandler). It
creates an instance of the class and returns it transparently.
source_obj: (Object) A reference to the object being scripted (the child).
"""
return CustomBasicObject(source_obj)

View file

@ -0,0 +1,67 @@
"""
This is an example of customizing the basic player character object.
You will want to do this to add all sorts of custom things like
attributes, skill values, injuries and so on.
If you want to make this the default player object for all players, move it
into gamesrc/parents and set SCRIPT_DEFAULT_PLAYER = 'custom_basicplayer'
in game/settings.py.
"""
from game.gamesrc.parents.base.basicplayer import BasicPlayer
class CustomBasicPlayer(BasicPlayer):
def at_player_creation(self):
"""
Called when player object is first created. Use this
instead of __init__ to define any custom attributes
all your player characters should have.
"""
#Example: Adding a default sdesc (short description)
#get the stored object related to this class
pobject = self.scripted_obj
#set the attribute
pobject.set_attribute('sdesc', 'A normal person')
def at_pre_login(self, session):
"""
Called when the player has entered the game but has not
logged in yet.
"""
pass
def at_post_login(self, session):
"""
This command is called after the player has logged in but
before he is allowed to give any commands.
"""
#get the object linked to this class
pobject = self.scripted_obj
#find out more about our object
name = pobject.get_name(fullname=False,
show_dbref=False,
show_flags=False)
sdesc = pobject.get_attribute_value('sdesc')
#send a greeting using our new sdesc attribute
pobject.emit_to("You are now logged in as %s - %s." % (name, sdesc))
#tell everyone else we're here
pobject.get_location().emit_to_contents("%s - %s, has connected." %
(name, sdesc), exclude=pobject)
#show us our surroundings
pobject.execute_cmd("look")
def class_factory(source_obj):
"""
This method is called by any script you retrieve (via the scripthandler). It
creates an instance of the class and returns it transparently.
source_obj: (Object) A reference to the object being scripted (the child).
"""
return CustomBasicPlayer(source_obj)

View file

@ -1,35 +1,110 @@
"""
An example script parent for a
An example script parent for a nice red button object. It has
custom commands defined on itself that are only useful in relation to this
particular object. See example.py in gamesrc/commands for more info
on the pluggable command system.
Assuming this script remains in gamesrc/parents/examples, create an object
of this type using @create button=examples.red_button
This file also shows the use of the Event system to make the button
send a message to the players at regular intervals. Note that if you create a
test button you must drop it before you will see its messages!
"""
from game.gamesrc.parents.base.basicobject import BasicObject
#you have to import the event definition(s) from somewhere covered by @reload,
# - this is as good a place as any. Uncomment to turn off event system.
import game.gamesrc.events.example
#
#commands on the button object
#
def cmd_push_button(command):
"""
An example command to show how the pluggable command system works.
"""
# By building one big string and passing it at once, we cut down on a lot
# of emit_to() calls, which is generally a good idea.
retval = "You have pushed the button on: %s" % (command.scripted_obj.get_name())
This is a simple command that handles a user pressing the
button by returning a message.
"""
retval = "There is a loud bang: BOOOM!"
command.source_object.emit_to(retval)
def cmd_pull_button(command):
"""
An example of a second defined command (for those who
don't know how a button works ... ;) )
"""
retval = "A button is meant to be pushed, not pulled!"
command.source_object.emit_to(retval)
#
#The object itself
#
class RedButton(BasicObject):
def __init__(self, scripted_obj, *args, **kwargs):
"""
This is called when class_factory() instantiates a temporary instance
of the script parent. This is typically not something you want to
mess with much.
"""
# Calling the super classes __init__ is critical! Never forget to do
# Calling the super class' __init__ is critical! Never forget to do
# this or everything else from here on out will fail.
super(RedButton, self).__init__(scripted_obj, args, kwargs)
# Add the command to the object's command table.
# Add the commands to the object's command table (this is about
#the only thing you should use the __init__ for).
self.command_table.add_command("pushbutton", cmd_push_button)
self.command_table.add_command("pullbutton", cmd_pull_button)
def at_object_creation(self):
"""
This function is called when object is created. Use this
preferably over __init__.
In this case all we do is add the commandtable
to the object's own command_table variable; this makes
the commands we've added to COMMAND_TABLE available to
the user whenever the object is around.
"""
#get stored object related to this class
obj = self.scripted_obj
obj.set_description("This is your standard big red button.")
obj.set_attribute("breakpoint", 10)
obj.set_attribute("count", 0)
def blink(self):
"""If the event system is active, it will regularly call this function to make
the button blink. Note the use of attributes to store the variable count and
breakpoint in a persistent way."""
obj = self.scripted_obj
try:
count = int(obj.get_attribute_value("count"))
breakpoint = int(obj.get_attribute_value("breakpoint"))
except TypeError:
return
if count <= breakpoint:
if int(count) == int(breakpoint):
s = "The button flashes, then goes dark. "
s += "Looks like the lamp just broke."
else:
s = "The red button flashes, demanding your attention."
count += 1
obj.set_attribute("count",count)
obj.get_location().emit_to_contents(s)
def class_factory(source_obj):
"""
This method is called any script you retrieve (via the scripthandler). It
This method is called by any script you retrieve (via the scripthandler). It
creates an instance of the class and returns it transparently.
source_obj: (Object) A reference to the object being scripted (the child).
"""
return RedButton(source_obj)
return RedButton(source_obj)