1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2024-12-28 00:15:06 +00:00
OpenMW/apps/openmw/mwlua
Mehdi Yousfi-Monod 767d7bfecd fixed indent
2024-09-01 15:36:53 +02:00
..
types FEAT: Add essential and respawning flags for both NPC and Creature types 2024-07-28 17:14:09 -05:00
animationbindings.cpp use string_view 2024-08-12 21:39:29 +02:00
animationbindings.hpp Rewrite addvfx and spawnvfx to take a model path instead of a static. Mode vfx from core to world. Add event handlers for AddVfx and SpawnVfx. 2024-08-10 14:56:35 +02:00
birthsignbindings.cpp
birthsignbindings.hpp
camerabindings.cpp
camerabindings.hpp
cellbindings.cpp Expose cell ID, Add actor travel destinations to types.Actor.record.servicesOffered 2024-06-19 21:38:27 +00:00
cellbindings.hpp
classbindings.cpp
classbindings.hpp
context.hpp Context helpers for caching packages, split up core bindings into multiple caches, remove getRealFrameDuration in global context 2024-07-24 09:28:50 +02:00
corebindings.cpp Rewrite addvfx and spawnvfx to take a model path instead of a static. Mode vfx from core to world. Add event handlers for AddVfx and SpawnVfx. 2024-08-10 14:56:35 +02:00
corebindings.hpp Context helpers for caching packages, split up core bindings into multiple caches, remove getRealFrameDuration in global context 2024-07-24 09:28:50 +02:00
debugbindings.cpp
debugbindings.hpp
dialoguebindings.cpp Move Lua index helpers to components and make owner.factiooRank match other ranks 2024-06-18 21:32:16 +02:00
dialoguebindings.hpp
engineevents.cpp Revert "Merge branch 'were_engineers_not_salt_miners' into 'master'" 2024-06-25 20:58:31 +00:00
engineevents.hpp Revert "Merge branch 'were_engineers_not_salt_miners' into 'master'" 2024-06-25 20:58:31 +00:00
factionbindings.cpp Move Lua index helpers to components and make owner.factiooRank match other ranks 2024-06-18 21:32:16 +02:00
factionbindings.hpp
globalscripts.hpp Revert "Merge branch 'were_engineers_not_salt_miners' into 'master'" 2024-06-25 20:58:31 +00:00
idcollectionbindings.hpp
inputbindings.cpp Fix memory safety of actions/triggers iteration 2024-08-08 00:09:08 +02:00
inputbindings.hpp
inputprocessor.hpp
itemdata.cpp move enchant charge and soul to itemdata 2024-07-05 23:33:55 +08:00
itemdata.hpp
localscripts.cpp lua - add isFleeing() to lua AI interface 2024-07-31 20:29:33 +02:00
localscripts.hpp
luabindings.cpp Context helpers for caching packages, split up core bindings into multiple caches, remove getRealFrameDuration in global context 2024-07-24 09:28:50 +02:00
luabindings.hpp
luaevents.cpp
luaevents.hpp
luamanagerimp.cpp Merge branch 'anim-api-cleanup' into 'master' 2024-07-28 09:17:05 +00:00
luamanagerimp.hpp Add a showInDialogue parameter to ui.showMessage and use it for level up messages 2024-06-27 17:16:49 +02:00
magicbindings.cpp fixed indent 2024-09-01 15:36:53 +02:00
magicbindings.hpp
markupbindings.cpp
markupbindings.hpp
menuscripts.cpp Move Lua index helpers to components and make owner.factiooRank match other ranks 2024-06-18 21:32:16 +02:00
menuscripts.hpp
mwscriptbindings.cpp Revert "Merge branch 'were_engineers_not_salt_miners' into 'master'" 2024-06-25 20:58:31 +00:00
mwscriptbindings.hpp
nearbybindings.cpp
nearbybindings.hpp
object.hpp Revert "Merge branch 'were_engineers_not_salt_miners' into 'master'" 2024-06-25 20:58:31 +00:00
objectbindings.cpp Move Lua index helpers to components and make owner.factiooRank match other ranks 2024-06-18 21:32:16 +02:00
objectbindings.hpp
objectlists.cpp
objectlists.hpp
objectvariant.hpp
playerscripts.hpp
postprocessingbindings.cpp Move Lua index helpers to components and make owner.factiooRank match other ranks 2024-06-18 21:32:16 +02:00
postprocessingbindings.hpp
racebindings.cpp
racebindings.hpp
README.md
recordstore.hpp Move Lua index helpers to components and make owner.factiooRank match other ranks 2024-06-18 21:32:16 +02:00
soundbindings.cpp
soundbindings.hpp
stats.cpp
stats.hpp
uibindings.cpp Use new Context helpers for UI bindings 2024-07-24 09:28:51 +02:00
uibindings.hpp
userdataserializer.cpp
userdataserializer.hpp
vfsbindings.cpp
vfsbindings.hpp
worker.cpp
worker.hpp
worldbindings.cpp Rewrite addvfx and spawnvfx to take a model path instead of a static. Mode vfx from core to world. Add event handlers for AddVfx and SpawnVfx. 2024-08-10 14:56:35 +02:00
worldbindings.hpp

MWLua

This folder contains the C++ implementation of the Lua scripting system.

For user-facing documentation, see OpenMW Lua scripting. The documentation is generated from /docs/source/reference/lua-scripting. You can find instructions for generating the documentation at the root of the docs folder.

The Lua API reference is generated from the specifications in /files/lua_api. They are written in the Lua Development Tool Documentation Language, and also enable autocompletion for (LDT) users. Please update them to reflect any changes you make.

MWLua::LuaManager

The Lua manager is the central interface through which information flows from the engine to the scripts and back.

Lua is executed in a separate thread by default. This thread executes update() in parallel with rendering logic (specifically with OSG Cull traversal). Because of this, Lua must not synchronously mutate anything that can directly or indirectly affect the scene graph. Instead such changes are queued to mActionQueue. They are then processed by synchronizedUpdate(), which is executed by the main thread. The Lua thread is paused while other updates of the game state take place, which means that state that doesn't affect the scene graph can be mutated immediately. There is no easy way to characterize which things affect the graph, you'll need to inspect the code.

Bindings

The bulk of the code in this folder consists of bindings that expose C++ data to Lua.

As explained in the scripting overview, there are Global and Local scripts, and they have different capabilities. A Local script has read-only access to objects other the one it is attached to. The bindings use the types MWLua::GObject, MWLua::LObject, MWLua::SelfObject to enforce this behaviour.

  • MWLua::GObject is used in global scripts
  • MWLua::LObject is used in local scripts (readonly),
  • MWLua::SelfObject is the object the local script is attached to.
  • MWLua::Object is the common base of all 3.

Functions that don't change objects are usually available in both local and global scripts so they accept MWLua::Object. Some (for example setEquipment in actor.cpp) should work only on self and because of this have argument of type SelfObject. There are also cases where a function is available in both local and global scripts, but has different effects in different cases. For example see the binding actor["inventory"] in 'MWLua::addActorBindings` in actor.cpp:

actor["inventory"] = sol::overload([](const LObject& o) { return Inventory<LObject>{ o }; },
    [](const GObject& o) { return Inventory<GObject>{ o }; });

The difference is that Inventory<LObject> is readonly and Inventory<GObject> is mutable. The read-only bindings are defined for both, but some functions are exclusive for Inventory<GObject>.

Mutations that affect the scene graph

Because of the threading issues mentioned under MWLua::LuaManager, bindings that mutate things that affect the scene graph must be implemented by queuing an action with LuaManager::addAction.

Here is an example that illustrates action queuing, along with the differences between GObject and LObject:

// We can always read the value because OSG Cull doesn't modify `RefData`.
auto isEnabled = [](const Object& o) { return o.ptr().getRefData().isEnabled(); };

// Changing the value must be queued because `World::enable`/`World::disable` aside of
// changing `RefData` also adds/removes the object to the scene graph.
auto setEnabled = [context](const Object& object, bool enable) {
    // It is important that the lambda state stores `object` and not the result of
    // `object.ptr()` because when delayed will be executed the old Ptr can potentially
    // be already invalidated.
    context.mLuaManager->addAction([object, enable] {
        if (enable)
            MWBase::Environment::get().getWorld()->enable(object.ptr());
        else
            MWBase::Environment::get().getWorld()->disable(object.ptr());
    });
};

// Local scripts can only view the value (because in multiplayer local scripts
// will be client-side and we want to avoid synchronization issues).
LObjectMetatable["enabled"] = sol::readonly_property(isEnabled);

// Global scripts can both read and modify the value.
GObjectMetatable["enabled"] = sol::property(isEnabled, setEnabled);

Please note that queueing means changes scripts make won't be visible to other scripts before the next frame. If you want to avoid that, you can implement a cache in the bindings. The first write will create the cache and queue the value to be synchronized from the cache to the engine in the next synchronization. Later writes will update the cache. Reads will read the cache if it exists. See LocalScripts::SelfObject::mStatsCache for an example.