1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 03:35:27 +00:00
OpenMW/apps/openmw/mwlua/object.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
1.8 KiB
C++
Raw Normal View History

2020-12-18 23:21:10 +01:00
#ifndef MWLUA_OBJECT_H
#define MWLUA_OBJECT_H
2022-05-16 17:04:41 +02:00
#include <map>
2023-07-28 15:23:53 +02:00
#include <stdexcept>
2021-01-29 01:54:54 +01:00
#include <typeindex>
#include <sol/sol.hpp>
#include <components/esm3/cellref.hpp>
2020-12-18 23:21:10 +01:00
#include "../mwworld/ptr.hpp"
namespace MWLua
{
// ObjectId is a unique identifier of a game object.
// It can change only if the order of content files was change.
using ObjectId = ESM::RefNum;
inline ObjectId getId(const MWWorld::Ptr& ptr)
2020-12-18 23:21:10 +01:00
{
return ptr.getCellRef().getRefNum();
}
// Lua scripts can't use MWWorld::Ptr directly, because lifetime of a script can be longer than lifetime of Ptr.
// `GObject` and `LObject` are intended to be passed to Lua as a userdata.
// It automatically updates the underlying Ptr when needed.
2023-03-05 21:47:49 +01:00
class Object : public MWWorld::SafePtr
2020-12-18 23:21:10 +01:00
{
public:
2023-03-05 21:47:49 +01:00
using SafePtr::SafePtr;
const MWWorld::Ptr& ptr() const
{
const MWWorld::Ptr& res = ptrOrEmpty();
2023-03-05 21:47:49 +01:00
if (res.isEmpty())
throw std::runtime_error("Object is not available: " + id().toString());
return res;
}
2020-12-18 23:21:10 +01:00
};
// Used only in local scripts
struct LCell
{
MWWorld::CellStore* mStore;
};
2020-12-18 23:21:10 +01:00
class LObject : public Object
{
using Object::Object;
};
// Used only in global scripts
struct GCell
{
MWWorld::CellStore* mStore;
};
2020-12-18 23:21:10 +01:00
class GObject : public Object
{
using Object::Object;
};
using ObjectIdList = std::shared_ptr<std::vector<ObjectId>>;
template <typename Obj>
struct ObjectList
{
ObjectIdList mIds;
};
using GObjectList = ObjectList<GObject>;
using LObjectList = ObjectList<LObject>;
template <typename Obj>
struct Inventory
{
Obj mObj;
};
2020-12-18 23:21:10 +01:00
}
#endif // MWLUA_OBJECT_H