user3211415
user3211415

Reputation: 59

How exactly does a programming language interact with a scripting language?

Ok lets say you designed a game in c++ and used lua as well how exactly does the c++ portion of that game interact with the lua code. My main question that I have the most trouble understanding are:

  1. is the lua code converted into c++ objects when compiled?

  2. Does the lua code compile as the c++ code requests it?

  3. Is the lua code dependent on the c++ code, as in it using functions from the c++ code?

  4. Is the memory of the lua code integrated with the memory from the c++ code or is it a completely different proccess.

Upvotes: 0

Views: 317

Answers (2)

Chris
Chris

Reputation: 2763

For the simple answers to the 4 questions: (1) No (2) Yes (3) Likely (4) Integrated.

A little more discussion though may help.

Lua is an interpreter written in C, which will be embedded into your app. Lua has a lua_State structure which manages the entire script execution state, contains all global variables, local variables, etc. Lua passes this structure around. You can write C/C++ code which manipulates this lua_State structure allowing you to add global variables which will be visible to the script... You can also query the lua_State structure to read variables which were set by the script. On top of this, you can also create C/C++ code which will execute a Lua script function, and you can also write C/C++ code which will export a C function to Lua script which can be called from script directly. When this happens, the lua_State object is passed to the C function so you can work with the script engine data reading it and modifying it as needed.

Generally a game written in C/C++ will implement an engine which handles all the rendering and other time critical items parts. Having got a engine, you could create C/C++ code to create the relevant objects to create and control your game. However you could also export the engine API to Lua and then create Lua scripts to call the engine API so that the game is controlled in script.

Most of the time this is not desired. You want the game engine to maintain control of how it executes, but you want to allow the engine to be extended in some way within specific guidelines. To do this, most engines will add Lua support using a type of 'callback' mechanism, where the game engine is designed to issue calls to certain Lua script functions at certain points in time. E.G. there may be a callback for some type of collision between two objects to allow the user to decide what to do. The Lua script can then change the object states as it deems fit, spawning new objects for explosion effects for example, as needed.

Some engines will create and destroy new Lua instances (new lua_State object) for each call meaning that you cannot create global Lua variables and preserve them between calls, others will use the same instances so you can create your own global Lua variables and do with them what you want to. This is up to you the engine writer.

Adding scripting to a game means you just need to think about how you want users to be able to extend your engine, and create API's which allow them to do this and then decide how you want to implement this.

Upvotes: 3

Oliver
Oliver

Reputation: 29473

  1. is the lua code converted into c++ objects when compiled?

    No, it is converted into instructions that the interpreter then executes.

  2. Does the lua code compile as the c++ code requests it?

    Yes

  3. Is the lua code dependent on the c++ code, as in it using functions from the c++ code?

    It can but it doesn't have to. You decide what c/c++ variables and functions to make available from Lua.

  4. Is the memory of the lua code integrated with the memory from the c++ code or is it a completely different proccess.

    Integrated, same process.

Upvotes: 0

Related Questions