2

I'm making a programming game in C++ and Qt, but have a unique constraint as far as my choice in engines go; because it's possible that the player may write code that loops infinitely or takes a long time to run, I need to be able to terminate a scripting engine's execution at any time. I asked a question about this on StackOverflow yesterday, and unfortunately there's no way to kill a thread in C++ without potentially causing resource problems (locks not being released, memory leaks, etc.).

I was advised that I need a scripting engine that allows me to terminate scripts at will without jeopardizing the player's session. What options do I have, subject to these requirements?

  • It must be embeddable in a C++ application.
  • It must work on Windows, Mac, Linux, and Android.
  • It doesn't need to be a mainstream language like Python, but it must be teachable to someone entirely new to programming.
    • Haskell, Scheme, Prolog, etc. are right out unless you can convince me that they would be good first languages for an 11-year-old to learn.
  • I need to be able to stop a script's execution at any time.
    • I can't kill the thread itself, so why not kill the scripting engine?
  • I should have full control over the scripting context (e.g. what objects/functions/types are available).
  • Qt integration would be nice, but is optional (I can make my own wrapper).
  • It doesn't need to be very fast -- the player's code will run repeatedly, but no more than once every half-second or so.
  • I must be able to call C++ functions from this scripting engine, and I must be able to call functions written in this scripting language from C++.
2
  • Actually, you might not need the ability to terminate the scripting engine at will. It might well be enough to be able to signal it that you want a long-running script terminated; or you could have it use time-outs internally, like Javascript's setTImeout()
    – einpoklum
    Commented Jul 16, 2016 at 6:52
  • Also remember that you can instruct users to write simple scripts, using a small subset of the scripting language's grammar - and if they try writing something more complex, it's up to them. And a final note - check out what existing FOSS games use (0 A.D., Battle for Wesnoth, Cave Story) - at leasy some of them use a scripting engine, and you can get inspiration regarding how to use it as well.
    – einpoklum
    Commented Jul 16, 2016 at 6:57

1 Answer 1

2

You might want to try Lua.

  • It is embeddable in C or C++

  • It is portable (works on all platforms that have a C compiler)

  • It is a rather easy language

  • You can call into Lua from C++ and the other way round (even nested multiple times)

  • You have full control over which functionality/objects/... are provided to the scripts

  • From my understanding it should be feasible to terminate an executing Lua engine in a multithreaded environment

If you want to stay entirely inside C++, you might be interested in luabind.

3
  • @JesseTG: The Battle for Wesnoth uses Lua extensively.
    – einpoklum
    Commented Jul 16, 2016 at 6:54
  • Many games do: e.g. WoW (for user addons) and Civilization V, just to name 2 AAA titles from the top of my head
    – hoffmale
    Commented Jul 16, 2016 at 8:17
  • If Lua runs under a C++ thread, and you terminate it, you terminate the C++ thread and violate the OP's original objection. Why is this an answer?
    – Ira Baxter
    Commented Aug 30, 2016 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.