0
\$\begingroup\$

I am wondering how I could manage to execute programs made by players in my game via some sort of in-game computer. I have been trying to figure it out but I have no idea how it is even possible.

Examples of other games that do this are:

I just need a basic explanation of how I could do it so I can start making my own games with in-game coding as a mechanic.

\$\endgroup\$
  • 6
    \$\begingroup\$ gameprogrammingpatterns.com/bytecode.html should be helpful \$\endgroup\$ – Ryan Aug 17 '16 at 5:06
  • \$\begingroup\$ computercraft's JAR file has a version of LuaJ integrated in it \$\endgroup\$ – UDXS Aug 17 '16 at 6:27
4
\$\begingroup\$

There's two basic approaches:

  • you host a scripting language like Lua in your game, and you have your game's "computer" be "programmed" in Lua. Simulating this computer is simply running the user's Lua scripts after setting up a Lua environment that contains the variables and other state you want your computer to represent.

  • you do the same thing as above, except instead of using Lua you invent your own language or abstraction around a language (such as by placing blocks with certain functionality in the world which interact with blocks nearby them, as in Minecraft).

Really they're basically the same thing, it comes down to how much you want the experience of programming the virtual computer to be like programming a real one or like interacting with your world. And also down to how much effort you want to put into it.

In both cases you need to design what a "computer" in your game looks like. Usually they're quite simple, things that have a small amount of memory and some very basic IO functions. In your game you set aside a block of memory in a variable and you call that your "computer"'s memory. Similarly you write a bunch of functions to manipulate that memory -- to get a value from it, to add a value to it, to print it to the screen, et cetera -- and you hook them up in some fashion to the rest of your gameplay via whatever interface is appropriate for your game design.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Or one can use the language that provides a free compiler and ships it with the framework (if any). For example, C#. You can write programs from within the game, compile it using provided compiler and then launch them (probably from a separate thread). \$\endgroup\$ – Sergey.quixoticaxis.Ivanov Sep 14 '16 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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