Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Use LUA for ingame computers  (Read 2113 times)
0 Members and 1 Guest are viewing this topic.
Online LostWarrior
« Posted 2013-03-25 16:45:22 »

Hello forum,

I want to implement computer in my game with LUA as the language. So you can have computers, based on LUA and program it the way you want.
A perfect example is the Computer Craft mod for Minecraft. It's almost exactly what I want: functional computer with LUA.

So do I have to make my own compiler or can I implement LUA an easier way?

Cows aren't sheep, because pigs already are.
Offline nsigma
« Reply #1 - Posted 2013-03-25 16:52:17 »

This page might be useful - http://stackoverflow.com/questions/2113432/how-can-i-embed-lua-in-java

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Online LostWarrior
« Reply #2 - Posted 2013-03-25 17:00:00 »

I already found this article, but I don't know how it would fit in my game. I need to have custom functions to and I don't really know how to do it with a complete LUA implementation.

Cows aren't sheep, because pigs already are.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2013-03-25 19:15:19 »

perhaps you can consider javascript, which is included in the JRE by default.

you can pass a string which holds a javascript command to a ScriptEngine which executes it. This buildin feature also allows you to pass java object to JS and vice versa
Offline Dejay

Senior Newbie


Medals: 3



« Reply #4 - Posted 2013-03-25 19:36:50 »

Javascript is probably the best choice and by far the easiest. Also most players will know javascript because of html.

Another choice would be to compile Java with the runtime compiler. You just combine your "script" code string with a class/method definition implementing an runnable interface and execute that method.
http://stackoverflow.com/questions/199241/java-in-memory-compilation
Online LostWarrior
« Reply #5 - Posted 2013-03-25 20:34:43 »

But one of the biggest problems with the language is, is that it has to be also compatible with the "low minds". I mean, I even can't script javascript, I probably could do it if I put some time in it, but most gamers don't script and don't want to have people to just quit because they can't manage their computer (which is going to be a big part of the control, It'll be almost the biggest control) so I can't just pick a random language and just expect they can learn it and use it.

Cows aren't sheep, because pigs already are.
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2013-03-25 20:38:21 »

I guess Javascript will be much more known to your users than Lua.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Online Roquen
« Reply #7 - Posted 2013-03-25 21:06:55 »

JavaScript is a pretty reasonable choice..esp due to NashHorn.  Lua & JavaScript are really very close to one another.  The downside of Lua is that it has a quirky syntax whereas JavaScript is more mainstream.  If you want joe-blow folks to be able to script stuff the then real solution is be data-driven and have a good tool.
Online LostWarrior
« Reply #8 - Posted 2013-03-25 21:12:17 »

And what do you mean with data-driven? And I searched a couple of examples of JavaScript and I think it is reasonable to use it. Are their any good ways to implement it and maybe scratch something of it, like those "oh shit" code you can possibly forget and which is not really needed?

Cows aren't sheep, because pigs already are.
Offline Sammidysam
« Reply #9 - Posted 2013-03-26 01:11:08 »

You could try to utilize the command line/terminal to compile code that you write instead of using Java explicitly (it might be easier that way), though then you would probably need to package the required items with the game (such as if you need to compile java, javac and I don't know if you're allowed to do that).  I feel like this seems easier than the inside Java compilation I saw.  Either way, it doesn't seem very easy.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dejay

Senior Newbie


Medals: 3



« Reply #10 - Posted 2013-03-26 19:23:51 »

Probably most programmers or script kiddies are going to like and/or had the idea of having a programmable game.
But if you're concerned about players not wanting to program, maybe don't put in the feature at all? A programming game will be a very niche game, few will like it, but if you appeal to those they might like it a lot.

Of commercial interest could be some kind of "edutainment" software that is targeted towards "yuppie parents" who want their kid to learn "IT stuff" early. Something like "Creative programming for kids is fun!". As a six year old I would have loved that =)

It also totally depends on what you can control with the virtual computer. Some combat bots? Or maybe make it so that only some players will program, and others who don't want to program can just use and combine their modules ingame. But that is basically modding.

Dragon Age had a very simple interesting module to "programm" the reactions of your NPCs. Very simple grammar, just a list of "if" event then do some slightly configurable action.
http://media.giantbomb.com/uploads/0/3661/1734446-dragonage2_2011_03_13_20_41_05_49.jpg

The only other widespread easy to use programming language I can think of is excel. Or a simple variant of graphical programming languages where you connect inputs/output of modules. Like in the blender game engine. Or maybe in the even simpler form of most procedural texture generation toolkits. For example werkkzeug you just stack modules on top of each other to connect them. Very easy to grasp.
http://pcg.wikidot.com/pcg-software:werkkzeug

Or logo, but that's just an outdated iterative basic.
Anyway, just thinking, because the idea sounds cool.
Online Roquen
« Reply #11 - Posted 2013-03-27 14:16:32 »

Pretty much any game that has over some small threshold of behaviorally complexity needs data-driven & scripting like notions to be manageable.  Most simply don't expose that to the end-user.

The DragonAge screenshot is misleading as that is "how" a non-programmer can build some behavior without needing to know how to program...in other words some pure data-driven behavior building.  If you know how to program, from NeverWinter Nights they have had a C like program language.  I've never looked at DragonAge, but NWN was pretty full featured with tons of engine interactions possible.  Pretty much all of the game mechanic was written in the scripting language.
Online LostWarrior
« Reply #12 - Posted 2013-04-08 19:19:38 »

I could create my own simple language with only very simple statements like IF, and simple variable declarence so that you don't have to assign data types. If I make it event based, so that you can do simple interactions with things outside the computer and then execute events or change variables. Is that something that is realistic? But how can I write an compiler for that?

Cows aren't sheep, because pigs already are.
Online Roquen
« Reply #13 - Posted 2013-04-08 19:27:46 »

Not worthwhile unless the point is a learning exercise.
Offline Agro
« Reply #14 - Posted 2013-04-08 19:31:08 »

It would be better if you just wrote a command processor for now since none of us probably have an idea about writing good compilers.

[command] [parameters]

You can still add things like if, else, goto, and other stuff, but yeah, its the best first step.

Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #15 - Posted 2013-04-08 19:37:24 »

I could create my own simple language with only very simple statements like IF, and simple variable declarence so that you don't have to assign data types. If I make it event based, so that you can do simple interactions with things outside the computer and then execute events or change variables. Is that something that is realistic? But how can I write an compiler for that?

You don't know what ice floe you are stepping on there. It's another piece of science. Really. R-e-a-l-l-y.
If you mind either waiting until JDK 8 is somehow popular, or ship your own jdk8 with your product, you can use nashorn. I've experimented with it myself.

In the end nashorn compiles to java bytecode. It's incredibly fast that way. Here is an example of how to use it:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
import netscape.javascript.JSObject;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class JsNashornTest {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine nashorn = manager.getEngineByName("nashorn");

        String javascriptSource =
                "var exampleObject = {\n" +
                "    awesomeProperty: 'ohmygosh',\n" +
                "    callFromJava: function (bool) {\n" +
                "            if (bool) {\n" +
                "            return 'oh, you truly called me!';\n" +
                "        } else {\n" +
                "            return 'you liar!'\n" +
                "        }\n" +
                "    }\n" +
                "};";
        System.out.println("JS code: \n" + javascriptSource);

       nashorn.eval(javascriptSource);

        JSObject jsFunction = (JSObject) nashorn.get("exampleObject");
        System.out.println("exampleObject.awesomeProperty = " + jsFunction.getMember("awesomeProperty"));
        System.out.println("exampleObject.callFromJava(true) = " + jsFunction.call("callFromJava", new Object[] { true }));
        System.out.println("exampleObject.callFromjava(false) = " + jsFunction.call("callFromJava", new Object[] { false }));
        System.out.println("MAGIC!");
    }
}


Remember, this needs JDK 8!

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline sproingie

JGO Kernel


Medals: 202



« Reply #16 - Posted 2013-04-08 23:29:18 »

Looking at
javascriptSource
kinda makes you wish they would add multiline string support to the language too, n'est pas?

Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #17 - Posted 2013-04-09 18:05:29 »

Looking at
javascriptSource
kinda makes you wish they would add multiline string support to the language too, n'est pas?

Hm. I don't think so. Even though scala's multiline support is sometimes a handy feature, i think that in those cases these strings should actually be put in files.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline nsigma
« Reply #18 - Posted 2013-04-09 18:16:48 »

If you mind either waiting until JDK 8 is somehow popular, ... you can use nashorn.

In the end nashorn compiles to java bytecode. It's incredibly fast that way.

Or there's Rhino (JDK 1.4+) or dyn.js (JDK 7+).  They all compile to bytecode.  That does not mean they'll perform equally well!  Compilation to bytecode is not in itself a guarantee of being "incredibly fast".  Wink

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Online LostWarrior
« Reply #19 - Posted 2013-04-09 18:49:52 »

I think I should define the implementation of what I want a bit more. So, imagine that I have a 3D world, where you can do stuff with a vehicle. In the vehicle I want people to have computers. These computer have to be programmable so people can actually control their vehicle very accurate with everything they want.
So this procces has to be advanced so people can actually do what they want, but it has to be straight-forward for other people who don't want do mess around to much. And for these PC's I have to design a language that is appropriate to control the vehicle in a rather advanced but simple way. I hope that this also cleans up my own suggestion for an event based language with direct interactions with the vehicles and stuff.

Cows aren't sheep, because pigs already are.
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #20 - Posted 2013-04-09 19:30:26 »

If you mind either waiting until JDK 8 is somehow popular, ... you can use nashorn.

In the end nashorn compiles to java bytecode. It's incredibly fast that way.

Or there's Rhino (JDK 1.4+) or dyn.js (JDK 7+).  They all compile to bytecode.  That does not mean they'll perform equally well!  Compilation to bytecode is not in itself a guarantee of being "incredibly fast".  Wink

I probably should have pointed out, that it uses the new 'invokedynamic' instruction from JDK 7. The problem 'before invokedynamic' was, that you had to use reflection to call methods and make those methods always rely on the same type: Object. And reflection isn't very fast compared to a normal method call.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline nsigma
« Reply #21 - Posted 2013-04-09 20:10:36 »

I probably should have pointed out, that it uses the new 'invokedynamic' instruction from JDK 7.

So does dyn.js - clue in the name!  Wink Not tried it yet. Be interesting to see how it compares with Nashorn. At least you don't need JDK8.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #22 - Posted 2013-04-09 20:40:39 »

I probably should have pointed out, that it uses the new 'invokedynamic' instruction from JDK 7.

So does dyn.js - clue in the name!  Wink Not tried it yet. Be interesting to see how it compares with Nashorn. At least you don't need JDK8.

I'm not even sure Nashorn needs JDK 8. The demo from some time ago where nashorn was shown first, was running with JDK 7. invokedynamic is from JDK 7.

Also, nashorn uses a library from one of their members, which is called 'Dynalink'. So I guess this is a project very similar to dyn.js.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #23 - Posted 2013-04-09 20:56:53 »

Aye, FWIW, you can "script" in JavaScript in Java 6 and 7 also, just ask for the "javascript" engine instead of "nashorn".  I believe it uses some older Rhino version.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (39 views)
2014-10-17 03:59:02

Norakomi (30 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (58 views)
2014-10-14 00:35:47

TehJavaDev (48 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!