Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does a game have to have built-in functions to accept and run lua scripts, or can I design Events and Hooks using Lua on any game I please, akin to the days where C code could be used to hook into the WinAPI using dlls?

The reason I ask is, I am trying to create a background application that will perform events and hooks on a particular game that does not currently support lua in-game.

Brief examples:

Events: - An action executed by the PLAYER is detected. For instance, hitting the Q key will normally make my character use an ability, but with my Lua script running in the background, will cause a sound to play on my computer (or something).

Hooks: - An action within the GAME is detected. For instance, the game spawns an enemy every minute. When an enemy spawns, the script will detect this and perform an action, for instance playing a sound locally on the computer.

I would like to do both, but I know for games like Garry's Mod, the game already has built-in support for running lua scripts.

Is there a way to do either events OR hooks using lua similarly to how C/C++ can connect to a game using WinAPI dlls?

share|improve this question
It might be possible, but I doubt it'd be very easy, and would require hacking the EXE of the game. – thedaian Jan 20 '12 at 16:05

Lua is not magic. Lua cannot magically associate itself with any application and interfere in any function call.

You can't even do that from C. Yes, with Win32 DLL injection, you can interpose a DLL within a DLL boundary. But if there is no DLL boundary to inject your code... you can't do anything. And I'm guessing most games don't expose the "spawn entity" function across a DLL boundary.

In general, most games put the main game code either in a DLL by itself or within the executable itself. The graphics system, sound system, etc, may be DLLs. But even if the game code is a DLL, internal game processes (create entity, manage entity, etc, stuff you want to hook to) would all be internal within that DLL. And therefore not injectable.

Oh, you can do this (not with Lua directly). But you would have to decompile the application, reverse-engineer the flow of functions and such, and manually edit the compiled binaries to add your external hook functions.

share|improve this answer

I did a similar thing with Just Cause 2 using the WinAPI, I used cheat engine to find out where the players position was stored in memory and networked that to my iphone to display a 'GPS'.

You can see the code here:

I don't know about accessing memory from Lua but cheatengine seems to use Lua, it's worth taking a look:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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