Scripting

Started by
6 comments, last by Photonman 18 years, 1 month ago
Ok, I've tinkered with Python before, but I've been hearing a lot about integrating scripting languages with, say, C++, recently, and I'm just not getting it. What tasks would you need a script to do that a program is incapable of doing? Most scripting languages are fairly fully-featured languages capable of writing entire programs on the same scale as any other PL, so what's the big diff? Also, How do you interface the script and the application? Is it like using XML documents to load values, but more complex (funtions and the like)?
______________________________Stranger things have happened...The Following Sentence is True. The Above Sentence is False.
Advertisement
Integrating scripting languages isn't about capabilities; it's about productivity.

Why is writing code in Python more productive than in C++? Same reason C++ is more productive than assembler - because it's more abstract. You can do more work with less code, and the work you do is safer and more reliable. (Think Python strings/C++ std::string versus directly writing bytes into memory.) Embedding languages is typically done for two reasons: to make the original programming team more productive, and/or to let people outside the original team extend the program easily.

Games are some of the biggest users of scripting languages for productivity. There is a sort of unwritten rule in programming that says always write code at the highest possible level of abstraction. Embedded languages let us write more abstract code easily. If you need to program a huge battle scene between thousands of warriors, would you prefer to do it in assembler or Python?

Extensibility is big in the office software realm. If you've ever used macros for something like Word or Excel you'll be familiar with this concept. Basically the idea is to give users a simple and easy-to-learn language (like Python or Basic) so they can add on to the core program without needing the original source code, or knowledge of the original language.


The techniques behind integration depend a lot on the two languages being merged. In Python's case you can actually set up C++ classes to become native Python objects, and have C++ code talk to Python objects as if they were C++ objects. A well-embedded Python system is actually basically the same as being able to interchange C++ and Python code at will (more or less). Other language combinations range from similar levels of power to more separated systems where the two languages can only communicate via a defined set of API functions. Typically the host language (e.g. C++) will get a way to send data to the embedded language, and call functions implemented in that language.

You can read more about how Python embedding works here.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

There was a thread on this a while back here (and indeed there are many others on this topic). As to why you should use a scripting language I fully agree with ApochPiQ. Also consider this (quoting myself from the thread I linked to above).

Quote:If you wanted to you could write an program that converts your data files to header files (i.e. store all the data in predefined constant arrays) and then just include all the header files instead of writing stuff that loads your data from files. Needless to say this is a bad idea, so why do the same for your game logic?


Scripting language can be very helpful in game development, I suggest taking a look at a few.
Yeah a really good reason is that you can expand your game without having to recompile/modify your engine. For big projects a goal should be to be able to add stuff to the game without having to touch the engine at all ;).

Note that if the scripting API looks like the C++ API, you're doing something wrong. Either your C++ API isn't rich enough, or your scripting API is too low level.
enum Bool { True, False, FileNotFound };
So basically, one of the ideas behind it is to be able to write an engine and, through modifying the scripts be able to rewrite functions without recompiling the game? If so, how do the APIs handle interpreting function code from the script lang into the engine lang?
______________________________Stranger things have happened...The Following Sentence is True. The Above Sentence is False.
It's not a matter of rewriting functions so much as building new systems out of the building blocks that the engine code gives you. Really it's analogous to using the Windows API functions to build Windows programs; you're not rewriting Windows, you're using the features it offers to spare yourself a lot of work (drawing stuff on the screen, messing with the mouse, etc.).

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Ah, now that's an analogy I can grasp.
______________________________Stranger things have happened...The Following Sentence is True. The Above Sentence is False.

This topic is closed to new replies.

Advertisement