Is there a way to execute command without specify the calling object?

47 views
Skip to first unread message

Zagor

unread,
Dec 21, 2020, 2:49:26 PM12/21/20
to Evennia
For example let's assume I want do some stuff with superuser permission without giving the player some Builder rights.

For example assuming I want to teleport someone in anothe room to implement "magic".
I'm actually using this "workaround" to launch an execute_cmd without making a character doing it.

puppet = create_object("typeclasses.mob.Mob", key="puppet", location=caller.location)
puppet.permissions.add("Builder")
puppet.execute_cmd("teleport xxx to yyy")
puppet.delete()

is there a way to launch from code execute command without specifying the NPC (or caller) before or do I need this workaround? :-P

Thanks!

Kovitikus

unread,
Dec 21, 2020, 3:45:04 PM12/21/20
to Evennia
puppet.move_to

You'll need the location object (the room object) that you want to teleport to. So you might have to search for it.

Zagor

unread,
Dec 22, 2020, 7:34:32 AM12/22/20
to Evennia
Thanks, this helped a lot!

On the other hand I'm still interested in launching a command with superuser permissions. For example executing a command (like spawn) with superuser rights. The point is that execute_cmd() always need an object to be attached.

I was wondering if there is a way to call execute_cmd() without attaching it to an object :-)

Thanks in advance!

Griatch Art

unread,
Dec 22, 2020, 8:08:21 AM12/22/20
to Evennia
A command is just a handler for input arguments from the user. That also npcs can use them with `.execute_cmd` is just for some additional flexibility. In both cases,
commands depend on the 'caller' of the command - that is, the entity/object/character performing the command. Calling a command without an attached object would not make sense.

If you want to (for example) spawn things in code you do so using the raw `spawn` function from the Evennia library and never involve a Command at all.
To get the functionality without a specific caller, you can examine the default commands to see what internal functions they use, then call those directly.
.
Griatch

Kovitikus

unread,
Dec 22, 2020, 8:37:17 AM12/22/20
to Evennia
Zagor, I think what you are really looking to do is utilize a specific set of instructions (you found within a command) to be usable across all of your code. This is not a case for a command, but rather a stand-alone function. The execute_cmd() method is just a shortcut for an object to access a set of instructions, with command specific restrictions (such as checking the permissions of an object). Restriction checks are why a command wants an object, as well as knowing who to send messages to once an action happens! (You can always search for and use objects within any function, if you need to. https://www.evennia.com/docs/latest/api/evennia.utils.search.html#evennia.utils.search.search_object)

Let's say you make a Python module named `my_functions.py` and placed it under the `world` directory. In this module, you can define a function, such as `func teleport(arguments)`. In here, you can copy and paste the code that you find in the actual teleport command https://www.evennia.com/docs/latest/_modules/evennia/commands/default/building.html#CmdTeleport

Now you have a foundation to work from. First off, you'll need to strip out elements that are specific to caller, which is the object executing the command. Once you've done that, you can begin to add your own custom behaviors, or just leave the functionality of it alone and use it as a generic teleport.

Now that you have your custom function in a custom module, any other module you wish to use this in must have the `my_functions.py` module imported. Such as: from world.my_functions import teleport

Now you can use teleport by itself anywhere in that module by calling: teleport(arguments)

Zagor

unread,
Dec 23, 2020, 4:37:20 AM12/23/20
to Evennia
Thanks a lot, clearer now :-) 
Reply all
Reply to author
Forward
0 new messages