Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: LuaConsole (command console for Lua, with completions)  (Read 24460 times)

0 Members and 1 Guest are viewing this topic.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
LuaConsole (command console for Lua, with completions)
« on: August 03, 2014, 12:17:51 am »
Lua Console

Code, instructions and minimal example here:
https://github.com/FRex/LuaConsole





I'm open to ideas and suggestions about features/API.
The console itself is independent of rendering and input, they are provided by additional classes (SFML implementation of both is of course in the repo and is used by the demo code in main.cpp).
« Last Edit: May 03, 2016, 07:33:55 pm by FRex »
Back to C++ gamedev with SFML in May 2023

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: LuaConsole (command console for Lua, with completions)
« Reply #1 on: August 04, 2014, 06:51:49 pm »
Seems to work fine for me :)

One thing I'd like to see is a clean interface for injecting functions and object methods into the parser, so that syntax something like the following would be possible:


setFramerate(24);
Framerate set to 24
entity = world.getEntity(12);
Entity does not exist
entity = world.getEntity(1);
Entity ID 1 (Type: Car) selected
entity.destroy();
Entity ID 1 (Type: Car) destroyed
...


That sort of thing I could see being really useful, especially for testing purposes.

Compilation throws up a ton of warnings with -Wall on, mostly about comparing signed and unsigned, but that's by the by.
« Last Edit: August 04, 2014, 07:06:48 pm by select_this »
Follow me on Twitter, why don'tcha? @select_this

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: LuaConsole (command console for Lua, with completions)
« Reply #2 on: August 04, 2014, 10:47:35 pm »
I'm not sure I understand what parser you mean... Tab completes using the tables and metatables from Lua state, better or worse (usually worse if command is a bit longer or more complex and it's tricky to find right tables since Lua is so flexible)... for example, if you do even in the example from github:
file = io.open("txt", "w")
file.
 
and then press tab you will (correctly) get a hint line:
read close flush setvbuf write lines seek
If you wrote: 'file.c' and then pressed tab, only 'close' would match, so instead of printed hint, you would get completion and your text would change to 'file.close' automatically.
I have not written a single line of special code to hint for that particular class methods.
Tables and tables that have metatables will work too.
Other types with per type metatable (as string in lua has by default) work too.


As for warnings - yes, I compile with '-Wall -Wno-sign-compare -Wno-switch' so there are errors for missing defaults/switch cases and for signed/unsigned comparison. I'll fix them soon.
Edit: Done, no more warnings under Wall and it didn't make code uglier too much.
« Last Edit: August 04, 2014, 11:48:57 pm by FRex »
Back to C++ gamedev with SFML in May 2023

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: LuaConsole (command console for Lua, with completions)
« Reply #3 on: August 05, 2014, 11:02:18 am »
I'm not sure I understand what parser you mean...

Sorry, I don't think I explained myself clearly.

Presumably someone using this console would be expecting to be able to call functions defined in the C++ via the console, not just stuff defined in the lua file, so it would be nice to have a clear interface for doing this without requiring the user to do too much boilerplate. By extension, tab-complete would need to know about these too.
« Last Edit: August 05, 2014, 02:52:19 pm by select_this »
Follow me on Twitter, why don'tcha? @select_this

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: LuaConsole (command console for Lua, with completions)
« Reply #4 on: August 05, 2014, 09:18:27 pm »
Quote
expecting to be able to call functions defined in the C++ via the console
Who would expect to call C++ functions... via a Lua prompt..? ;D

There are tons of ways to (easily) bind code to Lua already: http://lua-users.org/wiki/BindingCodeToLua or you can roll your own.
This is not a binding library at all, it doesn't care if you bind normally or using a library, it will try and work out the hints for you and it will manage your input, drawing, pretty printing and all, but it will not help you bind your code to Lua.

If you mean for the console itself to manage C++ function pointers/objects and do conversion and calls then well.. it's a Lua console for now but I'd not mind making it able to use any language through an interface class (I actually though about this already and about much much more) but that is only a possibility for far future, not something that can happen right now.
« Last Edit: August 05, 2014, 09:21:17 pm by FRex »
Back to C++ gamedev with SFML in May 2023

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: LuaConsole (command console for Lua, with completions)
« Reply #5 on: August 06, 2014, 11:09:32 am »
Quote
Who would expect to call C++ functions... via a Lua prompt..?

Anyone who uses lua as a scripting language front-end for their logic...?

The second paragraph is closer to what I was attempting to put across - being able to read the game world and remember state via the console etc. (thinking along the lines of The Sims' console here). Some form of interface to be able to bind concepts as opposed to functions, I guess. I appreciate it could be quite a lot of work, but it would be quite nifty if the end user only had to worry about the C++ side of things (I can think of a few ways to do this, though some may not be feasible - I'm no lua expert by any means).
« Last Edit: August 06, 2014, 02:30:18 pm by select_this »
Follow me on Twitter, why don'tcha? @select_this

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: LuaConsole (command console for Lua, with completions)
« Reply #6 on: August 06, 2014, 02:42:49 pm »
Any scripting system worth it's salt can be interacted with both via script files, but also just executing arbitrary strings it's given. It's useful for debugging.

Almost every commercial game engine out there has a debug command console. Source engine, the Unreal Engine, even the elder scrolls games all give you access to a console for commands. It's a practice that dates all the way back to Doom (okay, probably in games before Doom too but "dates all the way back to Doom" just sounds nice).

It's useful for developers for testing the game, and sometimes gamers will use it to either get out of a bugged situation (turn off collision detection if they get stuck in a wall, for example) or just to cheat (enable god mode, for example).
« Last Edit: August 06, 2014, 02:44:41 pm by MorleyDev »
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: LuaConsole (command console for Lua, with completions)
« Reply #7 on: August 07, 2014, 05:42:32 am »
Quote
Anyone who uses lua as a scripting language front-end for their logic...?
If they use it for their logic then of course there is no problem if they call Lua that calls C++.
But you can't call C++ directly (with stock Lua 5.x) from a Lua console, how/why would that work?

Quote
Some form of interface to be able to bind concepts as opposed to functions, I guess. I appreciate it could be quite a lot of work, but it would be quite nifty if the end user only had to worry about the C++ side of things (I can think of a few ways to do this, though some may not be feasible - I'm no lua expert by any means).
That'd be hard as hell and probably not time well spent since there are so many generators/binding libraries for Lua already.
Again: this is not a binding library at all and you are free to use any binding library with it.

This console is basically luaL_dostring plus bonuses (chunks, hints, history, 'pretty' ASCII art interface, possibly in future unicode handling etc.), nothing else.
Back to C++ gamedev with SFML in May 2023

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: LuaConsole (command console for Lua, with completions)
« Reply #8 on: August 07, 2014, 10:04:24 am »
If they use it for their logic then of course there is no problem if they call Lua that calls C++.

That's what I meant.

That'd be hard as hell and probably not time well spent since there are so many generators/binding libraries for Lua already.
Again: this is not a binding library at all and you are free to use any binding library with it.

I'm not talking about the actual binding itself, but how the console interacts with the binding once it's there (rather than just being able to call standalone functions in a dumb way).
Follow me on Twitter, why don'tcha? @select_this

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: LuaConsole (command console for Lua, with completions)
« Reply #9 on: August 07, 2014, 10:34:28 am »
Quote
bind concepts as opposed to functions
Quote
I'm not talking about the actual binding itself, but how the console interacts with the binding once it's there (rather than just being able to call standalone functions in a dumb way).
I don't understand what you mean. There is a way to print to console(in future, possibly in color) and you have entire Lua at your hands via the console so what else should happen?

Maybe this (or other) console is more fitting for your needs: https://github.com/arpg/GLConsole
Also: updated hinting to make it a tiny bit better.
« Last Edit: August 07, 2014, 10:42:16 pm by FRex »
Back to C++ gamedev with SFML in May 2023

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: LuaConsole (command console for Lua, with completions)
« Reply #10 on: August 22, 2014, 12:16:22 am »
Some new development is going on. ;)
I made ctor take bitflag that controls few things: default history file, init script and font.
By default all of that is enabled, only skipping default font option is implemented so far.
Also now there is way to use your own font, set and get history and history line callback class.
All this is to allow customization while not skipping on convenience: you can take over history, font and init lua file yourself, but if  you don't care about it then you don't have to write a dozen lines of set up code to have a simple quick lua console that 'just works'.
Back to C++ gamedev with SFML in May 2023

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: LuaConsole (command console for Lua, with completions)
« Reply #11 on: September 20, 2014, 09:59:35 pm »
I've made more changes (again), they are pretty substantial.
A minor change is that I added colored text to model yesterday (+ default red for errors, yellow for your lines, green for hints).
The major change is that there is no more single class console, there now are three classes (or more like one + two helpers/interfaces): Model, View and Input.
1. Model is not dependant on anything except Lua now and provides all the console stuff.
2. View has to read messages and colors from Model and prepare drawing data (and then draw it when needed) out of them.
3. Input has to call the public model functions to move cursor, input text, etc.
4. View and Input are of course one pair per library (SFML, SDL, etc.) but they don't deal with any Lua at all.
4a. Of course there is SFML input and view class in the repo. :)
5. The Model + View + Input is not enforced (but is really idiomatic IMO), you can do anything with the LuaConsoleModel class via its' public API.

Here's a screen of colors in action (much clearer already, IMO):

Also, I only typed "h" before pressing tab and completion (which is tiny bit smarter that way now) seen that all hints have common prefix of "hi" so it completed up to that for me as well as display the hints themselves.
There is also a piece of trivia called "LinesOfCode.txt" which has output from cloc ran in pre-commit hook.
« Last Edit: September 22, 2014, 04:30:11 pm by FRex »
Back to C++ gamedev with SFML in May 2023

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: LuaConsole (command console for Lua, with completions)
« Reply #12 on: October 20, 2014, 09:51:29 am »
I have again cleared up, added, removed and renamed things, both internally and in API over the last weekend.
I also removed the need for C++11 and commented out the interface fully (just commented though, not made doxygen comments). Every single function and type in blua::LuaConsoleModel has a small explanation above itself now.
Back to C++ gamedev with SFML in May 2023

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: LuaConsole (command console for Lua, with completions)
« Reply #13 on: May 03, 2016, 08:19:17 pm »
I removed most of the text from the first post and just left the github link and a screen there.
The README in the repo is very clear and there is no point repeating all this in here and trying to keep it up to date.

I've added some significant stuff over 2015 and again today.
You can read the code or the commits because I try to be clear in them and do one thing at a time but here's a simple list of changes since 2014 (the first two are from today):
  • Resizing the console (minimum size is 10x4)
  • Completing in the middle of the prompt and not just at the end
  • Additional key controls
  • Respecting RenderStates so a shader and a transform can be used (but not a texture becuase the font texture is used)
  • History displaying and clearing using '--history' and '--clear' Lua comments (they work only in prompt, having such comments in Lua files will do nothing)
  • Made it so simply typing a variable or expression will print its result, like Lua 5.3 cli program does (this is of course toggleable in the console)
  • Made it so Lua 5.3 works with the console too
  • Other small adjustments to colors, rendering, history, hinting, not leaving Lua stack dirty, code and header files, ect.
Back to C++ gamedev with SFML in May 2023

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: LuaConsole (command console for Lua, with completions)
« Reply #14 on: October 31, 2017, 06:11:15 pm »
I've made a 32 bit Windows exe of the demo program if someone wants to check it out: https://ln.sync.com/dl/0a66445c0/6d8guxuu-9hx9ihtj-cy7jseu8-e6dxdvv9

See repo readme for controls and features.
Back to C++ gamedev with SFML in May 2023

 

anything