A Lua in game console.

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: A Lua in game console.

Post by jacmoe »

After some behind the scenes magic, I can now use a very newbie friendly syntax:

Code: Select all

set-campos 100 200 30
Or (maybe) better:

Code: Select all

set-camera-position 100 200 30
So with that out of the way (the 'horrible' Lisp parentheses) I can now start to craft my own language using Lisp. :)
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
merlinblack
Goblin
Posts: 224
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7

Re: A Lua in game console.

Post by merlinblack »

Very cool! 8)
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
User avatar
merlinblack
Goblin
Posts: 224
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7

Re: A Lua in game console.

Post by merlinblack »

My Lua setup isn't perhaps too newbie friendly, but I guess everyone where would be easily able to figure out what this does. :)

Code: Select all

clock={}
clock.overlay = OverlayManager:createOverlay()
clock.panel = Panel( 0, 738/768, 300/1024, 30/768 )
clock.panel.element:setMaterialName( "gui/dialog.background" )
clock.overlay:add2D( clock.panel.element )
clock.text = Text( 150/1024, 5/786, "Clock" )
clock.panel:addChild( clock.text )
function clock.update()
  while clock.stop == false do
    clock.text.element:setParameter('caption', os.date() )
    wait(1)
  end
end
clock.stop = false
createTask( clock.update )
clock.overlay:show()
I'ts changing over time. Anything that annoys me, after awhile gets updated. Like the add2D function needing a overlay element rather than the Lua class that holds it....
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: A Lua in game console.

Post by jacmoe »

merlinblack wrote:Very cool! 8)
Your Lua solution too ! :)

I like my Lisp version - but if only I could get that damn ECL to stop crashing on me when I use quotes.. :x
I tried escaping them, but it doesn't work. Well.. :wink:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: A Lua in game console.

Post by jacmoe »

Solved! :)
Got ECL 9.10.2 which works - ECL 9.12.3 doesn't.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
merlinblack
Goblin
Posts: 224
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7

Re: A Lua in game console.

Post by merlinblack »

To everyone using Lua, I made an Ogreish package loader function so now I can use

Code: Select all

require 'mylibrary'
to load 'mylibrary.lua' from any of the registered Ogre resource archives, including from a zip file if I want. :)

If you're interested have a look at:
http://github.com/merlinblack/Game-Engi ... resource.h
http://github.com/merlinblack/Game-Engi ... source.cpp

and parts of:
http://github.com/merlinblack/Game-Engi ... system.cpp

The functions from scriptingsystem.cpp are : int luaLibraryLoader( lua_State *L ) and void installLibraryLoader( lua_State *L )

Advanced Tutorial #1 helped a great deal here. Thanks to whoever wrote that!! :D
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: A Lua in game console.

Post by jacmoe »

I did a TinyScheme interpreter as well. :)
TinyScheme is the interpreter used in Script-Fu for the Gimp.
I'll put it up there - or link to it. :wink:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
merlinblack
Goblin
Posts: 224
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7

Re: A Lua in game console.

Post by merlinblack »

jacmoe wrote:I did a TinyScheme interpreter as well. :)
TinyScheme is the interpreter used in Script-Fu for the Gimp.
I'll put it up there - or link to it. :wink:
Gosh! Been busy huh? :)

I was thinking the other day that TinyC would be an interesting idea. Not an interpreter but a quick compiler. I'm not sure how quick it is though, and how well it would embed.
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: A Lua in game console.

Post by jacmoe »

Yes. I've been busy. :wink:
The problem is that both ECL and TinyScheme doesn't handle C++ member functions.
And that means that I need to test yet another candidate: mzScheme.
Apparently, it does support member functions as callbacks, so I just need to test that. :)
Will keep the topic posted..
Why?
Well, because I like multi-paradigm languages with a weight on functional programming - a nice break from imperative languages like C++.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: A Lua in game console.

Post by jacmoe »

I did a mzScheme (PLT Scheme) console:
mzOgre.jpg
Now, I need to wrap the console itself in Scheme. That would be cool. :)
I already have a rather complete Ogre framework - based on the V2 Game Engine:
https://trac.v2.nl/wiki/vge
Found it when searching for mzScheme references - Scheme and Ogre: how awesome is that! :D

The wrapper is called 'The Magick Framework' and is a simple Luabind-like wrapper (using Boost).
I am now wrapping OIS and OgreBites..
You do not have the required permissions to view the files attached to this post.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
merlinblack
Goblin
Posts: 224
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7

Re: A Lua in game console.

Post by merlinblack »

Again, Very Cool! 8)

Although looking at your screen shot, reminded me there is a fix for code that wraps the text. The wiki version skips a character. You may want to implement word wrap anyhow.

Also after seeing your screen shot I remembered somewhere I have a 'Haunted House' text game I wrote in GW-BASIC when I was 11, that would be fun to do in Lua :P I wonder if the floppy drive still works.....
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
KakCAT
Gnoblar
Posts: 15
Joined: Sun Jan 24, 2010 11:40 am

Re: A Lua in game console.

Post by KakCAT »

a little code snippet to accept tabs in your string @ LuaConsole::print

Code: Select all

        else if (*pos =='\t')
		{
			line.push_back (' ');							///< Push at least 1 space
			column++;

			while ((column%tabWidth)!=0)					///< fill until next tabWidth
			{
				line.push_back (' ');
				column++;
			}
		}

to be placed after the

Code: Select all

        if( *pos == '\n' || column > CONSOLE_LINE_LENGTH )
block. Requires a const int tabWidth=X; somewhere.


p.s. I don't know if the problem was in the original LuaConsole because I've modified it so much I don't know if I've introduced new bugs
p.s.2 I don't have access to the wiki, if the patch is interesting (and p.s.1 don't apply) feel free to introduce it for me :)
User avatar
merlinblack
Goblin
Posts: 224
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7

Re: A Lua in game console.

Post by merlinblack »

I was reading your post, and thinking, "hang on, tabs work for me...". :? Then I realised after looking at your code, that perhaps a TextOverlayElement just puts in a number of spaces where the tab character was, and my code was just getting it right accidently. :lol: I'll add this is the wiki soon for you since I'll be in there anyway. However you just use your forum login details to edit the wiki. :)

I have another fix for the LuaConsole::print as well, regarding skipping a character when wrapping to the next line. Word wrap can be left as an exercise for the reader... :P

EDIT: Wkikfied.
LuaConsole::print now looks like this

Code: Select all

void LuaConsole::print( std::string text )
{
    string line;
    string::iterator pos;
    int column;

    pos = text.begin();
    column = 1;

    while( pos != text.end() )
    {
        if( *pos == '\n' || column > CONSOLE_LINE_LENGTH )
        {
            lines.push_back( line );
            line.clear();
            if( *pos != '\n' )
              --pos;  // We want to keep this character for the next line.
  
            column = 0;
        }
        else if (*pos =='\t')
        {
           // Push at least 1 space
           line.push_back (' ');
           column++;
  
           // fill until next multiple of CONSOLE_TAB_STOP
           while ((column % CONSOLE_TAB_STOP )!=0)
           {
              line.push_back (' ');
              column++;
           }
        }
        else
        {
            line.push_back( *pos );
            column++;
        }
        
        pos++;
    }
    if( line.length() )
    {
        if( lines.size() > CONSOLE_MAX_LINES-1 )
            lines.pop_front();

        lines.push_back( line );
    }

    // Make sure last text printed is in view.
    if( lines.size() > CONSOLE_LINE_COUNT )
        start_line = lines.size() - CONSOLE_LINE_COUNT;

    textChanged = true;

    return;
}
That includes my small change and KakKat's tabbing code.
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: A Lua in game console.

Post by jacmoe »

Well, I plan on using CEGUI for a console with proper word wrapping and multi-line commands.
Otherwise this would quickly turn into a GUI library of its own. :)
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
KakCAT
Gnoblar
Posts: 15
Joined: Sun Jan 24, 2010 11:40 am

Re: A Lua in game console.

Post by KakCAT »

I suppose the answer is no, but has anyone had problems with the LuaConsole in release mode? (vs2005, sp1)
I'm getting a fault in frameStarted (in the 'text' object)
User avatar
merlinblack
Goblin
Posts: 224
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7

Re: A Lua in game console.

Post by merlinblack »

KakCAT wrote:I suppose the answer is no, but has anyone had problems with the LuaConsole in release mode? (vs2005, sp1)
I'm getting a fault in frameStarted (in the 'text' object)
I'll *hopefully* give my project a test run on Windows this weekend - and specifically run a test in Release. I think it's vs2005 I have installed :roll: , been awhile since I booted Windows at home! :)
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
Yati
Greenskin
Posts: 115
Joined: Thu Jun 09, 2011 5:41 am
Location: Melbourne, Australia
x 5

Re: A Lua in game console.

Post by Yati »

Hey guys,

I'm having a little problem with this. The text I type does not get detected by the console. But the pressing the tidle, page-up, page-down keys works perfectly.

While stepping through the code I saw that OIS::KeyEvent::text is 0 always.

Since no one else seems to have this problem, I'm assuming I'm doing something wrong. I suspect its the way I initialize OIS.

This is my OIS initialization code:

Code: Select all

		Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
		OIS::ParamList pl;
		size_t windowHnd=0;
		std::ostringstream windowHndStr;

		pWindow->getCustomAttribute("WINDOW",&windowHnd);
		windowHndStr<<windowHnd;
		pl.insert(std::make_pair(std::string("WINDOW"),windowHndStr.str()));

		m_pInputManager=OIS::InputManager::createInputSystem(pl);
		
		m_pKeyboard=static_cast<OIS::Keyboard*>(m_pInputManager->createInputObject(OIS::Type::OISKeyboard,true));
		m_pKeyboard->setTextTranslation(OIS::Keyboard::TextTranslationMode::Unicode);	//I added this thinking it'll help, it didn't :(
		m_pMouse=static_cast<OIS::Mouse*>(m_pInputManager->createInputObject(OIS::Type::OISMouse,true));

		Ogre::WindowEventUtilities::addWindowEventListener(pWindow,this);
		
		m_pKeyboard->setEventCallback(this);
		m_pMouse->setEventCallback(this);
One pretty ugly solution I thought of is set OIS::KeyEvent::text to the right character based on the OIS::KC_** of the key being pressed. But that just sounds stupid even in my head.

Thanks for sharing this console code!
User avatar
merlinblack
Goblin
Posts: 224
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7

Re: A Lua in game console.

Post by merlinblack »

Its been awhile since I've looked at OIS (it's working in my project, so I haven't touched it) but I remember that it only fills in the 'text' member on Key Down events, and that buffered vs non-buffered may have an effect too. Sorry for the vague answer - I'm at work. :wink:
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
Yati
Greenskin
Posts: 115
Joined: Thu Jun 09, 2011 5:41 am
Location: Melbourne, Australia
x 5

Re: A Lua in game console.

Post by Yati »

merlinblack wrote:Its been awhile since I've looked at OIS (it's working in my project, so I haven't touched it) but I remember that it only fills in the 'text' member on Key Down events, and that buffered vs non-buffered may have an effect too. Sorry for the vague answer - I'm at work. :wink:
>_< yup your spot on. I was using key up events so that answer wasn't vague at all!

I do have another question though. In the consolePrint.lua file the function myprint replaces the default print? Override it?
User avatar
merlinblack
Goblin
Posts: 224
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7

Re: A Lua in game console.

Post by merlinblack »

Yati wrote:I do have another question though. In the consolePrint.lua file the function myprint replaces the default print? Override it?
Short Answer: It overrides it, yes.

Long Answer: Functions are treated just the same as tables, numbers and strings etc. What it is really doing is pointing the reference named 'print' to a different function. When other code goes to call 'print', they get the new one. In that file you can see that I've made a new reference (called 'oldprint') to the old function by copying what 'print' was originally to keep the old function around should I need it. Without that, the original would get garbage collected eventually.
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
shanefarris
Greenskin
Posts: 107
Joined: Sun Mar 01, 2009 8:36 pm
x 7

Re: A Lua in game console.

Post by shanefarris »

Merlin, I like the console you created and I took a look at your test engine, and I really like the use of scripts in your engine too. For the most part when I see projects utilizing scripting abilities, most people will use Lua, or whatever as data for level loading and things like that. I think scripting data is ok if that's what you need it for, but you can do so much more with it like actually create logic with it. I like using a DB for data, and scripting for logic, and looks like you've utilized Lua very nicely.
User avatar
merlinblack
Goblin
Posts: 224
Joined: Thu Mar 15, 2007 10:05 am
Location: Sydney, Australia
x 7

Re: A Lua in game console.

Post by merlinblack »

shanefarris wrote:Merlin, I like the console you created and I took a look at your test engine, and I really like the use of scripts in your engine too. For the most part when I see projects utilizing scripting abilities, most people will use Lua, or whatever as data for level loading and things like that. I think scripting data is ok if that's what you need it for, but you can do so much more with it like actually create logic with it. I like using a DB for data, and scripting for logic, and looks like you've utilized Lua very nicely.
Thanks! :D

The Lua GUI code in my project has seen a revision or two, much eased by having a console there to debug, or testing out an idea off the cuff.
"He'd never realized that, deep down inside, what he really wanted to do was make things go splat."
Terry Pratchett, Reaper Man
Image
User avatar
randomcode
Halfling
Posts: 53
Joined: Wed Nov 11, 2015 11:31 am
Location: People's Republic of China
x 5

Re: A Lua in game console.

Post by randomcode »

How can I use this console to control ogre like this video?

User avatar
randomcode
Halfling
Posts: 53
Joined: Wed Nov 11, 2015 11:31 am
Location: People's Republic of China
x 5

Re: A Lua in game console.

Post by randomcode »

Why does I exit the console when using task manager kill it(Because it can't be shutdown normally), I get access violation error in this code?

Code: Select all

~MyFrameListener()
    {
        mKeyboard->setEventCallback(0);
    }
Lax
Gnoll
Posts: 644
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany
x 59

Re: A Lua in game console.

Post by Lax »

Hi all,

I know its an old topic, but maybe someone has an answer:

I'm also want to use the lua console to print out results. For example:

If I print 1 + 2, I want the result placed in the console. In this case 3

print(1 + 2)
\>> 3

Has anbody an idea how to accomplish that?

Best Regards
Lax

http://www.lukas-kalinowski.com/Homepage/?page_id=1631
Please support Second Earth Technic Base built of Lego bricks for Lego ideas: https://ideas.lego.com/projects/81b9bd1 ... b97b79be62