Refactor C++ code to use a scripting language?
Background:
I have been working on a platfo开发者_StackOverflow中文版rmer 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.
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.
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.
精彩评论