Restricted Python
Moderator: Forum Moderators
Thanks, that'd be great. Pickling objects - yes, and I think this'll be a fairly common thing to want to do. In my case I'm pickling a graph representation of the map, for example.
I did manage to get things working in the end - I had a problem pickling classes that are in the main script file, because pickle was seeing them in the __builtin__ module when pickling but then was unable to find them when unpickling again. I got around it by creating a second module that I use for the persistent datatypes. It's probably important that this file doesn't import/use wesnoth, since I suspect pickle can't auto-import that, either (I could be wrong, I'm no python expert).
Re: location objects - pickling these as an (x,y) tuple and looking them up with wesnoth.get_location() after unpickling works just fine.
Other wishlists for the Python API would be:
* Let the AI send a chat message (outside debug mode) - lots of taunting fun
* Let the AI read chat messages - give your teammates orders!
* Print information on exceptions using wesnoth.log(), with file and line number!
I did manage to get things working in the end - I had a problem pickling classes that are in the main script file, because pickle was seeing them in the __builtin__ module when pickling but then was unable to find them when unpickling again. I got around it by creating a second module that I use for the persistent datatypes. It's probably important that this file doesn't import/use wesnoth, since I suspect pickle can't auto-import that, either (I could be wrong, I'm no python expert).
Re: location objects - pickling these as an (x,y) tuple and looking them up with wesnoth.get_location() after unpickling works just fine.
Other wishlists for the Python API would be:
* Let the AI send a chat message (outside debug mode) - lots of taunting fun
* Let the AI read chat messages - give your teammates orders!
* Print information on exceptions using wesnoth.log(), with file and line number!
I see, so I guess it's a complete class.. so should actually use pickle.In my case I'm pickling a graph representation of the map, for example.
Ok, the first one should be easy. Second one I don't know, I guess it would require hooking into the chat system and buffering all messages so the AI can then read them in its turn. That is, unless you want the AI to be able to chat outside of its turn as well.. no idea how that would work.Other wishlists for the Python API would be:
* Let the AI send a chat message (outside debug mode) - lots of taunting fun
* Let the AI read chat messages - give your teammates orders!
* Print information on exceptions using wesnoth.log(), with file and line number!
And file and line number for an exception definitely should be displayed, it's on my todo list for quite some time.. will try to look into it again (somewhat tricky due to how parser.py works, must rework that a bit).
Ok, in the version currently in SVN, there's now instructions in safe.py and parse.py how to disable the safety checks, some more modules are whitelisted (and it got easy to whitelist even more, just add to the list in parse.py) and get/set_variable work with arbitrary python objects (as long as they can be marshaled by Python).