Question about in-game python scripts and batch code processor

88 views
Skip to first unread message

Billy Clack

unread,
Sep 2, 2017, 10:46:29 AM9/2/17
to Evennia
Hi,

I've been delving into Evennia past couple days, and I really love the code base and support so far!

I have two questions:

1) It seems that , at least while learning, I have need to execute python code directly from game to do some interactive debug and inspection.  I know I can execute @py commands from terminal, but I was thinking for larger commands that require multiple lines of Python I would put these into a single script that I could repeatedly execute from in-game (instead of calling @py in the game terminal), but would not require me to add them as full-fledged game commands.  I'm thinking more along the lines of commands that I just want to use for development / debug.

What is the best way to do this?  Is it using the "Batch Command Processor", and just keeping these types of scripts as batch command scripts?  Or are their other ways?

2) My second question is about an issue I found while experimenting with the batch command processor:

I made a simple script which counts the number of Character objects there are:

#HEADER

from typeclasses.characters import Character

#CODE

caller.msg("There are {0} "
           
"Characters.".format(Character.objects.count()))


When I try to execute in-game, I get a trace-back:

Running Batch-code processor - Automatic mode for tutorial_examples.test_command ...
01/02: from typeclasses.characters import Character  
02/02: caller.msg("There are {0}"
           
"Characters.".for[...]
>>>
>>> 06: # batchcode code:
>>> 07:
>>> 08:
>>> 09: caller.msg("There are {0}"
>>> 10:            "Characters.".format(Character.objects.count()))
>>> 11:
>>> 12:
>>> 13: Traceback (most recent call last):
>>>
>>>   File "<string>", line 10, in <module>
>>>
>>> NameError: name 'Character' is not defined
>>>


But when I modify the script to move the import from HEADER to CODE, I get correct execution:


#HEADER

#CODE

from typeclasses.characters import Character

caller.msg("There are {0} "
           
"Characters.".format(Character.objects.count()))


Running Batch-code processor - Automatic mode for tutorial_examples.test_command ...
01/02:                                                
02/02: from typeclasses.characters import Character

caller.ms[...]
There are 1 Characters.
End of batch file.
  Batchfile 'tutorial_examples.test_command' applied.

Am I missing something entirely obvious here with how I'm importing that Character class?

Thanks for any info!!

Griatch Art

unread,
Sep 2, 2017, 6:22:59 PM9/2/17
to Evennia
Hi Billy and welcome to Evennia!

I looked at your issue with the batchcode-processor and got the same error. Which was weird! So I examined what the heck was going on. As it turns out there was an issue with the parser if you put the #HEADER block as the very first line of the file (it didn't identify it properly, which mean the header never ender up in the code block and thus Character was not imported there). I have fixed this in the devel branch of Evennia, which will be merging into master in a week or so unless there are any unexpected delays. For now, just start your script with an empty line and it should work fine. Thanks for reporting and sorry for the confusion!

There are several ways to execute and test Python code on the running server.
  • Batch-code is one way, as you figured out. An interesting feature of batchcode is that it supports live reloading. That is, start your batch code with @batchcode/interactive <file>. In interactive mode you can step up and down in your batch code file and run individual commands over and over if you like. And if you enter rr you will reload the file and see any changes you do to it immediately (no @reload needed). Use hh for help in the interactive mode.
  • You can also use @py/edit to go into a vim-like line editor in your game.This allows you to enter and test multi-line python code.
  • Finally, it's very useful to run evennia shell from your game-dir. You will then be in a Python shell where you have access to the database etc. If you use this it's recommended you install ipython, which is a more powerful line-mode for Python. In your virtualenv, just install it with pip install ipython and evennia will use it automatically (if you get color in evennia shell you know ipython works). This is what I myself use for quickly testing out if a Python structure works as I expect.

You can also find more help in our Coding-Introduction.


Hope that helps!
.
Griatch

Billy Clack

unread,
Sep 4, 2017, 5:55:24 PM9/4/17
to Evennia
Thanks Griatch for the help!

Glad to know it was a bug and not user-error :-P  

Related to "evennia shell", this worked really well!  With ipython, I was able to use it to inspect objects , methods, etc from the command line (which helped me to explore the code base a little as well).

One question related to evennia shell, is it possible to do "live editing" while in "evennia shell" and have the values reflected in the game's objects?

For example, if I do this in evennia shell:

t = evennia.search_object("TestPlayer")[0]
print( t.db.stats_character.hp )
>>> 100

And then modify this value in evennia shell, like this:

t.db.stats_character.hp = 20


In the game (running in parallel), if I do @py self.db.stats_character.hp, it still shows "100".  And this happens if I do the reverse (i.e. edit in game while evennia shell is running, evennia shell's version doesn't change).

I'm guessing this is due to the modules etc being cached, but I was wondering if it were possible to do "live editing" of variables in "evennia shell" and see them reflected in the game?

Thanks!

One thing related to ipython and evennia shell though - Is there a way to re-cache the imports in ipython, and vice-versa in the game?  For instance, if I log into the ipython shell and do something like this:

Griatch Art

unread,
Sep 4, 2017, 6:15:38 PM9/4/17
to Evennia
You are indeed modifying the same database from evennia shell, but you are in different processes which means the in-game cache will most likely not pick it up. Changes done from the webserver will work correctly since that is run in the same process. If you @reload the server, you will see changes down from evennia shell.
.
Griatch
Reply all
Reply to author
Forward
0 new messages