Open 3D Engine AzCore API Reference 23.10.0
O3DE is an open-source, fully-featured, high-fidelity, modular 3D engine for building games and simulations, available to every industry.
AZ::ScriptContext Class Reference

#include <ScriptContext.h>

Classes

struct  CustomReaderWriter
 Stack temporary memory. More...
 

Public Types

enum class  ErrorType { Log , Warning , Error }
 
using ErrorHook = AZStd::function< void(ScriptContext *context, ErrorType error, const char *message)>
 
using RequireHook = AZStd::function< int(lua_State *lua, ScriptContext *context, const char *module)>
 
using StackVariableAllocator = AZ::StackVariableAllocator
 

Public Member Functions

 AZ_CLASS_ALLOCATOR (ScriptContext, AZ::SystemAllocator)
 
 ScriptContext (ScriptContextId id=ScriptContextIds::DefaultScriptContextId, IAllocator *allocator=nullptr, lua_State *nativeContext=nullptr)
 
void BindTo (BehaviorContext *behaviorContext)
 Bind LUA context (VM) a specific behaviorContext.
 
BehaviorContextGetBoundContext () const
 
ScriptContextId GetId () const
 
bool Execute (const char *script=nullptr, const char *dbgName=nullptr, size_t dataSize=0)
 execute all script loaded though load function, plus the one you supply as string
 
bool LoadFromStream (IO::GenericStream *stream, const char *debugName, const char *mode, lua_State *thread=nullptr)
 
void GarbageCollect ()
 Perform a full garbage collect step, this can be slow prefer GargabeCollectStep for runtime garbage collect.
 
size_t GetMemoryUsage () const
 Return the amount of memory used, in bytes.
 
void GarbageCollectStep (int numberOfSteps=2)
 
lua_State * NativeContext ()
 
bool ExistsGlobal (const char *name)
 
bool FindGlobal (const char *name, ScriptDataContext &dc)
 
bool FindGlobal (int cachedIndex, ScriptDataContext &dc)
 
template<class T >
bool ReadGlobal (const char *name, T &value)
 
template<class T >
bool ReadGlobal (int cacheIndex, T &value)
 
template<class T >
void AddReplaceGlobal (const char *name, const T &value)
 
void RemoveGlobal (const char *name)
 
template<class T >
void AddReplace (int cacheIndex, const T &value)
 
int CacheGlobal (const char *name)
 
void ReleaseCached (int cacheIndex)
 Release any cached resource (global or local)
 
bool Call (const char *functionName, ScriptDataContext &dc)
 
bool CallCached (int cachedIndex, ScriptDataContext &dc)
 
bool InspectTable (const char *tableName, ScriptDataContext &dc)
 
bool InspectTableCached (int cachedIndex, ScriptDataContext &dc)
 
bool ReadStack (ScriptDataContext &dc)
 
void EnableDebug ()
 Creates debug context (you can obtain vie GetDebugContext()). Depending on the implementation this can require more memory.
 
void DisableDebug ()
 Destroys debug context.
 
ScriptContextDebugGetDebugContext ()
 
void DebugSetOwnerThread (AZStd::thread::id ownerThreadId)
 
bool DebugIsCallingThreadTheOwner () const
 
void SetErrorHook (ErrorHook cb)
 
ErrorHook GetErrorHook () const
 
void Error (ErrorType error, bool doStackTrace, const char *format,...)
 
void SetRequireHook (RequireHook hook)
 
AZ::ScriptPropertyConstructScriptProperty (ScriptDataContext &dc, int valueIndex, const char *name=nullptr, bool restrictToPropertyArrays=false)
 

Static Public Member Functions

static ScriptContextFromNativeContext (lua_State *nativeContext)
 Get Native context.
 

Static Public Attributes

static const int m_maxDbgName = 512
 Max debug name length.
 

Protected Attributes

class ScriptContextImpl * m_impl
 
ScriptContextId m_id
 

Detailed Description

Lua VM context.

Note
move all LUA code to CPP and remove including it by default, most operations happen via ScriptDataContext.

Member Function Documentation

◆ DebugIsCallingThreadTheOwner()

bool AZ::ScriptContext::DebugIsCallingThreadTheOwner ( ) const

Make sure that the Lua EBus handlers are not called from background threads. Use this to make sure the calling thread is the thread that owns the script context.

◆ DebugSetOwnerThread()

void AZ::ScriptContext::DebugSetOwnerThread ( AZStd::thread::id  ownerThreadId)

Make sure that the Lua EBus handlers are not called from background threads. By default the thread that creates the script context is the owner. This method allows to override this default behaviour.

◆ GarbageCollectStep()

void AZ::ScriptContext::GarbageCollectStep ( int  numberOfSteps = 2)

Step the garbage collector. There is no exact number that works in all cases, tune this number for optimal performance in your app.

◆ LoadFromStream()

bool AZ::ScriptContext::LoadFromStream ( IO::GenericStream stream,
const char *  debugName,
const char *  mode,
lua_State *  thread = nullptr 
)

Load a lua buffer from the stream provided. Can be either text or binary blob. If load succeeds (true is returned), a function will be on top of the stack. If load fails (false is returned), a string (error message) will be on top of the stack. If not passing the result directly to lua (i.e. as you would for require), it is the caller's responsibility to pop the result.

Parameters
streamstream to read from.
debugNamethe debug name for the module
threadthe thread to execute the load on (MUST be a child of NativeContext()). Defaults to NativeContext().
modethe type of the stream, "b" for compiled Lua binary, "t" for raw Lua text, "bt" to have Lua check for you
Returns
whether or not the load succeeded.

◆ SetRequireHook()

void AZ::ScriptContext::SetRequireHook ( RequireHook  hook)

Set function to be invoked when require() is called, but module is not cached.


The documentation for this class was generated from the following file: