Author Topic: A guide to using a scripting language in games  (Read 18627 times)

miki151

  • Rogueliker
  • ***
  • Posts: 264
  • Karma: +0/-0
    • View Profile
A guide to using a scripting language in games
« on: March 12, 2014, 10:40:38 AM »
I'm looking for a good tutorial on scripting. Mostly from a design point of view, like what parts of the game belong to the engine, and what parts to the script. Preferably for an experienced programmer. (if you can call yourself experienced and not know anything about scripting) Doesn't have to language specific, but using C++ and Lua would be welcome.
KeeperRL, Dungeon Keeper in roguelike style:
http://keeperrl.com

ekolis

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 186
  • Karma: +0/-0
  • get ye dennis
    • View Profile
    • Ed's home page
    • Email
Re: A guide to using a scripting language in games
« Reply #1 on: March 14, 2014, 12:57:39 AM »
I don't have anything specific for you, but you might want to get in touch with DarkGod. He has extensive experience with Lua scripting for ToME.

One thing I would warn you about, though, is that if you're making a game where the person who writes the script is not necessarily the person who executes it (e.g. a modder writes scripts which other players use in the game), it's important to sandbox the script engine, so you don't have malicious modders writing viruses into their scripts.
The Ed draws near! What dost thou deaux?

>EAT SANDVICH

CaptainKraft

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 60
  • Karma: +0/-0
    • View Profile
Re: A guide to using a scripting language in games
« Reply #2 on: March 14, 2014, 01:37:29 AM »
This is something I'd like to get into someday as well, but sadly I can't help you out quite yet.

However, I recently was recommended to check out Moai for game development which uses C++ w/ Lua scripting. Looking over their docs might be a good way to dive in and figure out how this stuff works.

Sorry I can't be of more help. Hopefully this is helpful.
Build a man a fire, and he'll be warm for a day.
Set a man on fire, and he'll be warm for the rest of his life.

miki151

  • Rogueliker
  • ***
  • Posts: 264
  • Karma: +0/-0
    • View Profile
Re: A guide to using a scripting language in games
« Reply #3 on: March 14, 2014, 06:25:40 AM »
I was planning to write a 7drl or something using the T4 engine just to see how it's designed, but never got around to it. I hoped there is a guide somewhere so I can see if scripting is for me, as my game is pretty big already. Thanks for the tip about viruses, I can't imagine how that would work, but I'll keep it in mind  :)
KeeperRL, Dungeon Keeper in roguelike style:
http://keeperrl.com

ekolis

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 186
  • Karma: +0/-0
  • get ye dennis
    • View Profile
    • Ed's home page
    • Email
Re: A guide to using a scripting language in games
« Reply #4 on: March 14, 2014, 03:45:10 PM »
Well, let's say you expose a Lua interface to your game. If not for sandboxing, what's to stop someone from putting this code (sorry, I don't know Lua, so I'm just guessing as to the syntax) into one of their scripts:

sys.exec("deltree /y c:\windows\system32");

So you need some way to prevent modders from having unrestricted access to the filesystem. I don't know how you'd do that with Lua in C++, but with IronPython in C#, you have something called an AppDomain, and you run all your script code in the AppDomain, which you then apply restrictions to, like "can't access the filesystem".
The Ed draws near! What dost thou deaux?

>EAT SANDVICH

koiwai

  • Rogueliker
  • ***
  • Posts: 99
  • Karma: +0/-0
    • View Profile
Re: A guide to using a scripting language in games
« Reply #5 on: March 16, 2014, 01:07:37 AM »
I see a few uses for a scripts:

First. They can be reloaded on the fly, in the middle of the game, without the need to 1) save the game state, 2) rebuild 3) restart 4) load the game state. So, you can dynamically adjust the game without recompiling the game.

Second. Like javascript in browsers or shell script (like bash) in Unix, you can make a console and call your script functions from inside the game to change its state (spawning mobs, getting items, etc.)

Third. If the scripting language has nice API, you can delegate scripts programming to game designers. I'm thinking of the games like Neverwinter Nights, where you could make your own maps with scripted events. In NWN, I even implemented something like a chessboard with monsters acting like chess pieces. You could give commands to them via a master monster.. So yes, scripts can be quite fun.

requerent

  • Rogueliker
  • ***
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: A guide to using a scripting language in games
« Reply #6 on: March 18, 2014, 03:24:29 AM »
Its poor design to compile data into your game. It's good design to interpret as much logic (game rules*) as possible. The main purpose of scripting is to pull as much data and logic out of your compiled binary.

* Game Rules include algorithms that govern how combat is handled, how AI select behaviors, what items do- etc. They do not include actual number-crunching algorithms (like searching the state tree).

Why? Easier to patch, update, localize, maintain, modify, balance, edit, serialize, network, implement visuals and create supplemental design tools for. Your game is no longer just the executable, but all of the assets, scripts, and data that surround it.

Why not? Fair amount of overhead. Complicates your program's architecture. Slower loading times and run-times.

How? For good experience, make your own parser! You don't need a formal scripting language. Just read lines from a text file and use that information in factory methods to set flags and and initialize a library of items, monsters, potions or spells. Then, you can have another text file provide parameters for level generation-- test it out. Create an interactive console that lets you add something to the library or choose the parameters for the next level to be generated. Scripting is just being able to pass information to a parser that modifies the application state accordingly.


After you're done reinventing the wheel, incorporate Lua into your program. You can shove SQL in there too for no-brainer serialization (though technically you can just store all of your data as Lua tables).

Why Lua? Because writing a parser is hard! Writing a parser with support for flow control is super hard! Lua is fast and easy... and very portable.

Oh, PS: Sandboxing in Lua is the equivalent of setting a flag.
« Last Edit: March 18, 2014, 03:29:56 AM by requerent »

miki151

  • Rogueliker
  • ***
  • Posts: 264
  • Karma: +0/-0
    • View Profile
Re: A guide to using a scripting language in games
« Reply #7 on: March 18, 2014, 10:54:09 AM »
Is Lua compiled when the game is started? If I misspell a function call or something that's exposed in C++ do I get an error on start up or only when the actual code runs?
KeeperRL, Dungeon Keeper in roguelike style:
http://keeperrl.com

requerent

  • Rogueliker
  • ***
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: A guide to using a scripting language in games
« Reply #8 on: March 20, 2014, 08:39:33 AM »
There are typically 2-3 ways you can use scripting languages.

Interpreter-> code is interpreted line-by-line by a VM as it is typed into a console or from a file.
Bytecode-> code is first compiled into bytecode that can later be interpreted by the VM.
Just-In-Time-> code is compiled into bytecode just-before interpretation.

In any case, an interpreter would be linked by your program's executable.

In most cases, you'll want to compile to bytecode first, in which case you get error checking.



Plugging my favorite,
LuaJIT is unequivocally the fastest way to go. It comes very close to performing at native speeds and it includes an extension that parses C-style declarations.
« Last Edit: March 20, 2014, 09:04:02 AM by requerent »

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: A guide to using a scripting language in games
« Reply #9 on: March 20, 2014, 09:12:32 AM »
AFAIK, the names you use to call Lua functions (pure lua or exposed from C) are tied to actual function definition at runtime. You can assign completely different functions to the same name during execution, e. g. alternating between several implementations, therefore no tool can check validity of the call beforehand.
« Last Edit: March 20, 2014, 09:15:22 AM by Cfyz »

Xecutor

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 263
  • Karma: +0/-0
    • View Profile
Re: A guide to using a scripting language in games
« Reply #10 on: March 21, 2014, 01:55:43 PM »
Btw my 7drl2014 entry is a scripting host that exposes pseudoconsole api to the scripting language.
C++ host takes care about drawing the console and reading the input (and passing it to script), as well as some computationally heavy algorithms like fov and pathfinding.
But actual game logic is written in dynamically typed object oriented programming language (created by me :) ).
Once I ironed out all critical bugs, developing the game was quite fast and easy.
In my language call to undefined function will be reported at startup time, but call to function with wrong number of arguments will only be reported at runtime.
I'm using 'sane dynamism' approach in my language.
Accessing undefined data member of an object will raise runtime exception, calling function with wrong number of arguments will raise runtime exception.
There is no automatic type conversion. Attempt to add string to int will raise runtime exception.
This way all the errors typical to dynamically typed languages are exposed much faster.

miki151

  • Rogueliker
  • ***
  • Posts: 264
  • Karma: +0/-0
    • View Profile
Re: A guide to using a scripting language in games
« Reply #11 on: March 21, 2014, 02:05:56 PM »
I think I'm too sloppy to use a dynamically typed language :). How is it in Lua?
KeeperRL, Dungeon Keeper in roguelike style:
http://keeperrl.com

TheCreator

  • Rogueliker
  • ***
  • Posts: 370
  • Karma: +0/-0
    • View Profile
    • Fame
    • Email
Re: A guide to using a scripting language in games
« Reply #12 on: March 21, 2014, 02:34:07 PM »
Btw my 7drl2014 entry is a scripting host that exposes pseudoconsole api to the scripting language.

I don't think that a 7DRL is a strong argument here. Every game seems to be successful for the first 7 days. Try to develop a script-driven 7YRL and then we can discuss 8).
Fame (Untitled) - my game. Everything is a roguelike.

Xecutor

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 263
  • Karma: +0/-0
    • View Profile
Re: A guide to using a scripting language in games
« Reply #13 on: March 21, 2014, 02:47:07 PM »
I think I'm too sloppy to use a dynamically typed language :). How is it in Lua?
Lua is dynamically typed language. And in lua objects are fully dynamic.
If you make a typo in property name:
Code: [Select]
  obj.monstreType='Zombie'
no errors will be raised.

If you want statically typed scripting language - take a look at AngelScript.
It's fast and have syntax almost identical to C++.
But personally I don't see much sense in using statically typed language as scripting language.
It's as verbose as it's bigger relatives, but you loose support of existing tools.

miki151

  • Rogueliker
  • ***
  • Posts: 264
  • Karma: +0/-0
    • View Profile
Re: A guide to using a scripting language in games
« Reply #14 on: March 21, 2014, 03:47:20 PM »
Maybe I'm just being paranoid. Anyway I still don't know where the line should go between the script part and the "core" part in a big game. If it's just LOS, pathfinding, and other basic stuff in the core, then 90% of the game would be scripted. Does anyone know how TOME4 is designed?
KeeperRL, Dungeon Keeper in roguelike style:
http://keeperrl.com