How to script in games | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to script in games

I want to do coding in game how I do that

23rd May 2018, 4:13 PM
Divyansh
Divyansh - avatar
3 Answers
+ 16
You can do this quite simply in Java 6. exhibit your objects/functions in your Java app into the scripting environment For example you can expose your character or the game world into the scripting environment and allow the script to control it. invoke script functions/methods from Java environment Allow the script to customize behaviours, add new actions, etc. Another use of this is to override the default build in game rules and load your own house rules. Another use is as a macro facility. Typically this would be part of the main game loop use the script as a data/level file For example, in a game likes SpaceHulkwhere it is mission based, you can use a script to define a game specific DSL, and us this DSL to define new missions. When the game starts up, it evaluates the data file and create a model of the game. To integrate a scripting engine into Java, simply do this ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine jsEngine = mgr.getEngineByExtension("js"); jsEngine.eval("print('Hello world!'); To expose an object into the script environment jsEngine.put("avatar", avatarObject); //An avatar object is now available in myscript.js jsEngine.eval(new FileReader("myscript.js"); To invoke a function in the script jsEngine.eval("function hello(name) ... "); Invocable inv = (Invocable)jsEngine; inv.invokeFunction("hello", "Fred"); One of the problem with scripting is that, the script has access to your entire application. You can restrict what the script can 'see' by setting a classloader when you create the ScriptEngineManager instance. Java 6 comes with a bundled JavaScript engine. However I prefer Groovy and loading Groovy basically mean changing 'js' in getEngineByExtension() to 'groovy'. I know that a lot of people say that Java is not fast for games, but if you are writing turn based games or social games like Farmville then I think it is more than adequate. For C/C++ based games, I think the preferred script engine is Lua. I've not used it so I cannot comment on it.
23rd May 2018, 4:18 PM
Abhivarshini Maddala
Abhivarshini Maddala - avatar
0
Look into unity and C# JavaScript won’t get you far and you don’t need to much c# to start coding simple games
23rd May 2018, 4:44 PM
Max
Max - avatar
0
thanks for your help I am not using the app from someday's so that's why I had not seen your answer thanks for your help (Abhi ) you really helped me thanks 🙋
4th Jun 2018, 9:55 AM
Divyansh
Divyansh - avatar