Interactive Python Shell in Construct

This forum is currently in read-only mode.
From the Asset Store
Casino? money? who knows? but the target is the same!
  • EDIT:

    5/01/10 - Updated the screen shot and attachment with an improved version.

    5/02/10 - Updated again with a few minor bug fixes and features. See my next posts for details.

    Hello. I've been messing a bit with Construct for the past few weeks, and I was happy to see that Construct supported Python scripting. I've always wanted to learn Python, but never had a compelling reason to do so until now, so I've spent the last couple of weeks learning Python.

    I must say, I really like this language. It's been ages since I've programmed anything, and I've never used object-oriented programming, but I've found it quite easy to learn Python and OOP so far. My previous programming experience is quite dusty (from Commodore Amiga era) in ASM, Basic, C, and ARexx.

    I've had a hard time figuring out how to use Construct objects from Python scripts, so I had an idea for getting some info on them. I created a simple interactive shell from which I could query object attributes and methods, and also interact with Construct objects step-by-step. It's also o.k. for just messing around with Python interactively, but not quite as nicely as Python's own IDLE shell.

    Anyway, I thought I'd post this in the event that it may be helpful to one of the other Python users some time. Here's a screen shot that shows a sample of usage:

    I've found the dir() and help() commands quite helpful, and if you re-size/move things a bit, you can plop Construct objects in there and play with them through Python, too. It's not as useful as it could be as a plugin of some sort, maybe, but it's helpful as a simple tool. Usage is pretty straightforward, I think.

    Cap is attached for download.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I like it! Very useful.

  • Very nice addition! Thanks for creating and sharing.

  • Thanks for the comments. I've updated this a bit.

    I've found myself using this more often, so I set about making it a bit more user friendly. After a fair amount of trying to force Construct's EditBox object to do my bidding, the result is what I consider to be a fairly respectable command line shell. It works in a manner similar to the Windows version of the Python IDLE Shell, with a command history that works similarly to older shells. Some notable features:

    • Like the Python IDLE Shell, when a command block is entered, if it evaluates to an expression, the shell will print a representation of the result (if it is not the value None.) Otherwise, it is simply executed.
    • Auto indentation similar to the Python IDLE Shell.
    • Keeps a display history of (approximately) the most recent 90,000 characters in the shell. This is able to be changed easily. Higher numbers may slow the shell's responsiveness. This display history may not be changed, but may be moved through in order to copy portions and such.
    • Keeps a command history of all command blocks that have been entered. These commands are recalled in a manner similar to the way that old unix shells work. In this case, 'control - up arrow' moves in reverse through the commands, and 'control - down arrow' moves forward through the commands. Recalled commands replace any current command entry, and may be edited (or not) and executed again.
    • All variables used in the code here were added to the 'Shell' object so that they would not pollute the global namespace. Otherwise, there is a PyShellOutput object name added to handle output.

    I had also hoped to thread the output so that it didn't display all of the output only after the command block was done executing, but ran into deadlocks when trying to access Construct objects from a thread. I scrapped that, as it's not of much consequence.

    I set the window/layout size to 640 x 480, but it's easy enough to change to one's preference. I decided against making window resizing possible, as I couldn't get it to work to my satisfaction.

    The screen shot and attachment are updated in the original post for this version.

  • Oops. After using this more, I found a bug in the way I handled detection of the 'enter' key, which also affected command editing around newlines. I also decided to add a few minor changes along with that fix.

    • Fixed the bug with 'enter' key handling, and cleaned up some unnecessary code along with it.
    • Added a control to reset the shell (control - delete.) This returns the shell to it's initial state, but leaves the Python environment the same. Any classes/functions/variables assigned will remain.
    • The shell now loses focus when a command is executing. This serves as a visual cue, and prevents extraneous input. It also regains focus automatically after exceptions are raised.
    • The first entry in the command history is now an empty string, to serve as an indicator that the beginning of the history has been reached when cycling through it. Also, other empty and exact duplicate commands are now discarded instead of being added to the command history.

    I can't seem to break it now, so hopefully that will do it.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)