G-Earth/G-Python-qtConsole.md
2021-04-13 02:37:42 +02:00

2.5 KiB

Requirements

  • Understand the G-Python extension interface (https://github.com/sirjonasxx/G-Python)
  • Make sure your python version (python --version on commandline) is >= 3.2
  • Execute the following line on commandline: python -m pip install qtconsole pyqt5 jupyter-console g-python to install the required packages

(IMPORTANT: On Linux devices, the installation command might require a sudo, since G-Earth runs in sudo as well.)

Usage

  1. Go to the "Extra" tab on G-Earth and check the "Enable G-Python Scripting" checkbox
  2. Go to the "Extensions" tab and open a G-Python shell

The shell will automatically do the following things for you:

  • Import all G-Python classes & modules (Extension, Direction, HMessage, HPacket, hparsers, htools)
  • Import the sleep() function from the time module
  • Initialize a G-Python extension (named ext) and connect it to G-Earth
  • Create an interface for all methods of the Extension object (for example: ext.send_to_server(packet) can also be executed with just send_to_server(packet))

G-Python shell extension

QtConsole shortcuts

Shortcuts:

  • TAB -> Autocomplete
  • SHIFT+TAB -> Show function arguments + docs
  • ENTER -> Execute the entered python script or continue it on the next line, depending on context
  • CTRL+ENTER -> Same as "ENTER", but always continue the current script
  • SHIFT+ENTER -> Same as "ENTER", but always execute

Commands:

  • %quickref -> list of all qtConsole commands
  • %clear -> clears the window
  • %save -> saves specific lines to a file (example: %save test 4-7, saves lines 4 to 7 to test.py)
  • %load -> loads script from a file (example: load test, loads test.py)

Example

Example 1: send all user signs

example 1

(hint: to save this script: %save all_signs 23-24)

Example 2: wave when typing "wave"

example 2

(hint: if you're going to sleep() during an intercept() callback, do an asynchronous intercept (check g-python repo), or all incoming/outgoing packet flow will be paused)

(hint: in the method signature def on_speech(message: HMessage), : HMessage isn't required but makes auto-completion easier)

Example 3: get all room furniture

example 3

Example 4: get all room users

example 4

(prints the list of all HEntities in the room, in which all of them are mapped to their corresponding string representation)