Creating a command line similar to MS-DOS.

Hello all, I’m in the middle of working on a game, ‘Pixel-Space: The Next Adventure’ as you know it, and I’m attempting to implement computers into the game. Does anyone have any good advice or shared code that might be helpful?

I attempted it a while back and was stunned at how many different things you would need to think about. All help is appreciated.
Basically, I’m referring to the blank command line that you can perform commands on. Think of it like trying to create a console from scratch, mainly using java.awt.graphics.

Thanks in advance. :slight_smile:

Maybe you have already done this, but first you should always google everything you can about the problem at hand. There is actually a lot about this sort of thing out there.

making a command line java

I have a project that is very similar to what you are looking for here. It is currently getting documented with Javadoc, and you can compile it by running the command “ant” in the project directory, or you can just run ./launch.sh (if not on Windows) and it will call ant and them launch the jar. If you are on Windows, after compiling you can launch it with the command “java -jar bin\FakeJavaOS.jar -textmode”. It will help a ton to get command interpreting done. Good classes are the Matchers for Arguments and Commands and Argument and Command classes.

You need to run it with the argument --init the first time running. This is kind of annoying, and after documentation is done that will be fixed.

It doesn’t use Java2D graphics yet, but once I finish the actual command line I am going to go that.

here’s a very simple method. I just implemented cd and cat (cat’s linux, but it’s simple to implement).

http://pastebin.java-gaming.org/337f81d276c

just create a file c:\hello.txt and put some stuff in it for a demout you could easily use a virtual . this uses the filesystem of the computer you’re running on, but you could use some sort of fake file system too.

the main thing to look at is the commandline.java file. It just splits the command name from parameters and executes the appropriate command class the same way java does with main(String[] args)

Also, if you play minecraft, you can check out the computercraft mod, implements computer terminals with Lua inside minecraft, as well as other goodies. Might give you an idea of how advanced you want to go with your project, i.e. whether or not to have a full-blown file system with stored programs or just some static commands.

That is EXACTLY what I was thinking of diving into.

I might look into that. I thought however instead of using LUA, I would try to make my own little language called Ke-script, or something like that. So much work in that though… :emo:

The separate scripting language is only necessary because of the existence of stored programs in a virtual file system. If you don’t go that far, then you don’t need it at all. If you do want that, then you could write your own DSL, or you could use one of these, or another language with a pre-made interpreter in Java.

Example procedure from JUEL
Seems pretty plug-and-play. Never used it before, though.

Well, If you are not afraid of tedious work, here is how I would approach it…
So you have a console class, with this basic layout (WARNING: Very rough, but you should get the basic idea):

I hope this helps!

If it’s just a simple command line, write down a list of commands you need.
Then when the user enters anything into the command prompt check the entered string against a array for stored commands, if the command is valid proceed to doing what ever it is the command is supposed to do.

Is there anything in particular that you need help with?

Ugh, listing all the commands in a huge array is quite inefficient to me. Why not just create a Command class and then use reflection to dynamically load all commands (in classes starting with Command, such as CommandStop, CommandChangeDirectory, etc.) which extend the Command class?

Yes, your way is more efficient, no doubt. I would suggest your way. I just don’t like using up all that memory with classes. It works, and takes up less space. Depends on what you are going for.

Well you can always just generate simple Java and run that straight from your code. I did that for an RPG and it worked pretty well. Because Java uses a JIT, you can do this with no issues.

Well, your console method only uses one class, right? I feel like a little extra RAM consumption is better than a 20,000 line class when you have tons of commands.