Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactor C++ code to use a scripting language?

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.

like image 788
Justin Ardini Avatar asked Jun 13 '10 19:06

Justin Ardini


People also ask

What is refactoring in C?

Refactoring or Code Refactoring is defined as systematic process of improving existing computer code, without adding new functionality or changing external behaviour of the code.

What is script refactoring?

In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior.

How do you refactor code or code?

Clicking on the Code Action lightbulb or using the Quick Fix command Ctrl+. will display Quick Fixes and refactorings. If you'd just like to see refactorings without Quick Fixes, you can use the Refactor command (Ctrl+Shift+R).


1 Answers

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.

like image 195
GManNickG Avatar answered Oct 06 '22 05:10

GManNickG