views:

216

answers:

2

Background:

I have been working on a platformer game written in C++ for a few months. The game is currently written entirely in C++, though I am intrigued by the possibility of using Lua for enemy AI and possibly some other logic. However, the project was designed without Lua in mind, and I have already written working C++ code for much of the AI. I am hoping Lua can improve the extensibility of the game, but don't know if it would make sense to convert existing C++ code into Lua.

The question:

When, if ever, is it appropriate to take fully functional C++ code and refactor it into a scripting language like Lua?

The question is intentionally a bit vague, so feel free give answers that are not relevant to the given background.

+5  A: 

Scripting languages are useful for things that might change frequently or be extended, and can afford the trade from speed.

It wouldn't make sense to use a scripting language in your core libraries, because those are relatively static (all they do is process stuff over and over) and need to be quick. But for things like AI, it's a perfect idea. You may tweak the AI without recompiling, and allow future changes quite nicely. Once you ship, you can pre-compile the scripting language and call it good.

It's also best for extensibility. Provide a Lua interface to your game and anybody can write plugins using a simple language, without the need for compiling. The more fleshed out your Lua interface, the more expressive and powerful those plugins can be.

If you've already got everything working, unless you intend on trying to improve it or allow extensions I don't really see a reason to strip it out; you're done. It would be something to keep in mind for your next game engine.

That said, if you're not completely done and this is a hobby/practice kind of thing, I would recommend you do. It will be your introduction into adding scripting capabilities to the game engine. When you get to making larger and more complex engines you won't need to worry about something new.

GMan
So if I'm confident that some portion of the AI code is never going to change, do you recommend I leave that code in C++ or port that to Lua with everything else for consistency?Thanks for the helpful answer.
Justin Ardini
@Justin: It depends on your goals. If it's definitely done and you do eventually need to be finished soon, leave it and move on to other things. But if this is a learning thing, port it to get the practice.
GMan
You can use a hybrid approach. Some portions of the AI that may be usable by alternative AI and is speed critical could be left in C. The Lua script will only be the orchestrator, more or less.
Lie Ryan
+2  A: 

When, if ever, is it appropriate to take fully functional C++ code and refactor it into a scripting language like Lua?

Rarely. Here's when I've done it:

  • I wanted to change the design or add functionality in ways that would require me to revisit the C++ code anyway.

  • I found parts of the C++ code that I kept changing over and over.

  • I believed that by migrating from C++ to Lua that I could make the code five or ten times smaller.

The first two bullets are things anyone can do. The third requires some experience.

Norman Ramsey