What are Benefits of Adding a Scripting Language to a Game?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

What are Benefits of Adding a Scripting Language to a Game?

Post by xx6heartless6xx »

Can someone tell me why it would be a good idea to add scripts to your game like how the ES team adds Lua to their game? Are their some things you cant do unless you have scripts or is it just used to make coding the game easier? Would learning Lua be a good idea?
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by k1net1k »

adding scripting support makes it easier to generate content and gameplay. consider it shorthand code for functions deeper in your engine.

also allows non-programmers (eg level designers, artists etc) to be able to interact with the engine without having to know 100% of what goes on in the background.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by dandymcgee »

They can also be changed while the game is running. For large projects compile time can be quite long, so being able to test new game content without recompiling the entire project every time can save heaps of time.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by wtetzner »

The usefulness of embedding a scripting language depends on which language you're using for your engine.

For example, if you're using C or C++, then embedding a scripting language will give you the benefit of being able to describe game logic in a higher level language, as well as allowing you to change the code at runtime.

However, if you were writing a game in Common Lisp or Clojure, there really wouldn't be any benefit to embedding another language. In these languages, you can already redefine functions at runtime, and they are about as high level as you can get. Especially since you can create new language constructs using macros.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by avansc »

Id also like to see the person who wanted to use lisp to make a game, Clojure is a little different since the whole java hooo haa. kinda side note, but Clojure is pretty friggen fantastic.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by MadPumpkin »

Basically, I'm not trying to repeat anything that above people said, but I'm OCD and need to number lists. :P
  1. Easily Maintainable Code: Run time code will be separate from compile time code, this makes it quickly obvious which ones happen when. So if a code has a problem, you will easily tell if you need to rebuild or not even if you're a beginner.
  2. Short Compile Times: Run times are slightly slowed down depending on your system for the scripting, but the project will compile a lot quicker
  3. High-Level Functionality: The code will be much easier to write, it may be a lot larger (in terms of lines of code). But will make it so that it's easier to understand things if you were to show a laymen your code. Which in turn means that anyone code quickly learn how to develop for your game and fix errors. Given of course that you release the scripts in text format.
  4. Run Time Source Changes: Probably my favorite feature of the whole list... You can edit your scripts even while your game is running, and as soon as the script is reloaded it will effect the game with your desired changes. I once sat for 2 hours with a text based game using C and LUA that I made, and created the game as I went through it, with very few rebuilds of the project. All I had to do, was set up the C engine to run my LUA scripts and reload them when I asked it nicely.
And other things too that other people can explain, but these are what came to my mind right now.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by TheBuzzSaw »

Did anyone link to Falco's dev-a-thon? He describes precisely why Lua is so important.

http://www.youtube.com/watch?v=JjS-tVm_AJE

Basically, you want your low level code taking care of technology-related things (graphics, sound, networking, etc.) and the high level scripting language taking care of content (story, quests, events, etc.).
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by Falco Girgis »

TheBuzzSaw wrote:Did anyone link to Falco's dev-a-thon? He describes precisely why Lua is so important.

http://www.youtube.com/watch?v=JjS-tVm_AJE

Basically, you want your low level code taking care of technology-related things (graphics, sound, networking, etc.) and the high level scripting language taking care of content (story, quests, events, etc.).
^ This. ;)
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by TheBuzzSaw »

The deeper I dig into game development, the clearer it becomes where a scripting language becomes useful (if not outright necessary). For my little RPG projects, I used to put virtually everything into C++. For whatever reason, I wanted a super elegant class that carefully wrapped all of my player's functionality as tightly as possible. Today, I am finding that, if the logic is not running every frame, it is probably in Lua now. Why lock myself into a compiled set of character stats? Why not let Lua take charge? What if I decide to add a charisma stat later on? Do I really wanna add a variable, a setter/getter, etc.? Only the content of the game would care about charisma. Oh, and that other content? More Lua!

A good analogy would be an orchestra. All the musicians represent the engine. The conductor represents Lua. The musicians are trained to play their instruments as skillfully possible. They need to be ready for any musical requests that come their way. The conductor is focus; he is physically incapable of wielding 200 instruments at once, but he can interface with 200 musicians and ask them for certain kinds of results (switch songs, play louder, play faster, stop, moar bass, etc.).

In other words, don't have Lua running every frame making OpenGL calls. Do have Lua running periodically updating quest logs, stats, and AI.
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by xx6heartless6xx »

TheBuzzSaw wrote:Did anyone link to Falco's dev-a-thon? He describes precisely why Lua is so important.

http://www.youtube.com/watch?v=JjS-tVm_AJE

Basically, you want your low level code taking care of technology-related things (graphics, sound, networking, etc.) and the high level scripting language taking care of content (story, quests, events, etc.).
So Lua then helps you to simplify your code by allowing you to code the parts about story and other things in a higher level language, I understand that. What I am still wondering is how can changing a your lua code cause a change in your game automatically without recompiling?

Also I've seen Falco's vid on Lua but how are you guys able to code a program in two different languages? I think I remember Falco said you have to have a wrapper in C++ for every anytime you want to use Lua. I just wanted a little more clarification on how to blend C++ with Lua.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by MadPumpkin »

What happens is that the LUA script is loaded and parsed FROM the file. So every time that you run a lua script, it will reload the file (if you don't have it purposefully set up otherwise) and redo all of the commands and scripting you have in the file. So if you wanted (don't do it unless for testing reasons) to make a lua script reload on key press of 'H' (seriously) then you could edit your code and then while the program is still running hit the letter 'H' and it would completely reload and run your code immediately. Usually lua is pretty fast (at least where slow down isn't noticeable) so it would usually work pretty quickly as well.

EDIT: and for your second question. You code in C/++ with a scripting language by using what's called wrappers. Basically C/++ loads your script as text files, and parses it's text as commands using an interface which is lua wrappers. So pretty much, it's like loading any other saved file into C/++ but instead it does something (function) based on the strings from the file (command).
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by xx6heartless6xx »

MadPumpkin wrote:What happens is that the LUA script is loaded and parsed FROM the file. So every time that you run a lua script, it will reload the file (if you don't have it purposefully set up otherwise) and redo all of the commands and scripting you have in the file. So if you wanted (don't do it unless for testing reasons) to make a lua script reload on key press of 'H' (seriously) then you could edit your code and then while the program is still running hit the letter 'H' and it would completely reload and run your code immediately. Usually lua is pretty fast (at least where slow down isn't noticeable) so it would usually work pretty quickly as well.

EDIT: and for your second question. You code in C/++ with a scripting language by using what's called wrappers. Basically C/++ loads your script as text files, and parses it's text as commands using an interface which is lua wrappers. So pretty much, it's like loading any other saved file into C/++ but instead it does something (function) based on the strings from the file (command).
To use your press the 'H' example, if 'H' is pressed then would I call the Lua function directly or would I just load the text file the script is on? Would I use an something like an fstream in C++ to read the file and then execute the script? Can you clarify and maybe give an example?
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by MadPumpkin »

Honestly it's been far too long since I've used lua or any scripting language with anything, I've been using straight C++ for a long time now. But I just googled "Hello World LUA C++" and got this, looks like a hit.

http://csl.sublevel3.org/lua/
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by dandymcgee »

xx6heartless6xx wrote: To use your press the 'H' example, if 'H' is pressed then would I call the Lua function directly or would I just load the text file the script is on? Would I use an something like an fstream in C++ to read the file and then execute the script? Can you clarify and maybe give an example?
You can call functions directly or call an entire script. There is something like lua_executescript("filename.lua"), although I don't remember what it's actually called. You could also read the file yourself and execute the commands, but that would defeat the purpose of using lua in the first place.

Just google some of these questions, there are a ton of answers out there that aren't so hard to find. If you have any specific problems you can't find anything on, come back and ask.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: What are Benefits of Adding a Scripting Language to a Game?

Post by Falco Girgis »

MadPumpkin wrote:So every time that you run a lua script, it will reload the file (if you don't have it purposefully set up otherwise) and redo all of the commands and scripting you have in the file.
Uh, not really. Sure, if all of your logic is global, and you're only using luaL_dofile(), but that's a REALLY terrible way to handle it. Then you truly are making a blocking IO call, reading a file, parsing through it, and executing it every time.

You're supposed to have initialization logic global (or in an init function), then have your operations split into a series of functions that you are directly invoking whenever you need to. After you call luaL_dofile() once, the script has already been "compiled" by the interpreter and is stored in the state, ready to go.

Sure, you CAN reload the file at each execution and get away with it on a modern PC, but there is no way in fuck you could do that frame-by-frame in real-time on any kind of console or older machine (I've tried it).
Post Reply