# Squirrel Class Reference

## Public Member Functions

bool | LoadScript (const char *script)
| Load a script.
void | AddMethod (const char *method_name, SQFUNCTION proc, uint nparam=0, const char *params=NULL, void *userdata=NULL, int size=0)
| Adds a function to the stack.
void | AddConst (const char *var_name, int value)
| Adds a const to the stack.
void | AddConst (const char *var_name, bool value)
| Adds a const to the stack.
void | AddClassBegin (const char *class_name)
| Adds a class to the global scope.
void | AddClassBegin (const char *class_name, const char *parent_class)
| Adds a class to the global scope, extending 'parent_class'.
void | AddClassEnd ()
| Finishes adding a class to the global scope.
bool | Resume (int suspend=-1)
| Resume a VM when it was suspended via a throw.
void | ResumeError ()
| Resume the VM with an error so it prints a stack trace.
void | CollectGarbage ()
| Tell the VM to do a garbage collection run.
void | InsertResult (bool result)
void | InsertResult (int result)
bool | CallMethod (HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend=-1)
| Call a method of an instance, in various flavors.
bool | CallMethod (HSQOBJECT instance, const char *method_name, int suspend=-1)
bool | CallStringMethodStrdup (HSQOBJECT instance, const char *method_name, const char **res, int suspend=-1)
bool | CallIntegerMethod (HSQOBJECT instance, const char *method_name, int *res, int suspend=-1)
bool | CallBoolMethod (HSQOBJECT instance, const char *method_name, bool *res, int suspend=-1)
bool | MethodExists (HSQOBJECT instance, const char *method_name)
| Check if a method exists in an instance.
bool | CreateClassInstance (const char *class_name, void *real_instance, HSQOBJECT *instance)
| Exactly the same as CreateClassInstanceVM, only callable without instance of Squirrel.
void | SetGlobalPointer (void *ptr)
| Sets a pointer in the VM that is reachable from where ever you are in SQ.
void | SetPrintFunction (SQPrintFunc *func)
| Set a custom print function, so you can handle outputs from SQ yourself.
void | ThrowError (const char *error)
| Throw a Squirrel error that will be nicely displayed to the user.
void | ReleaseObject (HSQOBJECT *ptr)
| Release a SQ object.
bool | IsSuspended ()
| Did the squirrel code suspend or return normally.
bool | HasScriptCrashed ()
| Find out if the squirrel script made an error before.
void | ResetCrashed ()
| Reset the crashed status.
void | CrashOccurred ()
| Set the AI status to crashed.
bool | CanSuspend ()
| Are we allowed to suspend the squirrel script at this moment?

## Static Public Member Functions

static bool | LoadScript (HSQUIRRELVM vm, const char *script, bool in_root=true)
static SQRESULT | LoadFile (HSQUIRRELVM vm, const char *filename, SQBool printerror)
| Load a file to a given VM.
static bool | CreateClassInstanceVM (HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook)
| Creates a class instance.
static bool | GetRealInstance (HSQUIRRELVM vm, SQUserPointer *ptr)
| Get the real-instance pointer.
static bool | GetInstance (HSQUIRRELVM vm, HSQOBJECT *ptr, int pos=1)
| Get the Squirrel-instance pointer.
static const char * | ObjectToString (HSQOBJECT *ptr)
| Convert a Squirrel-object to a string.
static int | ObjectToInteger (HSQOBJECT *ptr)
| Convert a Squirrel-object to an integer.
static bool | ObjectToBool (HSQOBJECT *ptr)
| Convert a Squirrel-object to a bool.
static void * | GetGlobalPointer (HSQUIRRELVM vm)
| Get the pointer as set by SetGlobalPointer.
static void | DecreaseOps (HSQUIRRELVM vm, int amount)
| Tell the VM to remove `amount` ops from the number of ops till suspend.

## Static Protected Member Functions

static void | CompileError (HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
| The CompileError handler.
static void | RunError (HSQUIRRELVM vm, const SQChar *error)
| The RunError handler.
static void | PrintFunc (HSQUIRRELVM vm, const SQChar *s,...)
| If a user runs 'print' inside a script, this function gets the params.
static void | ErrorPrintFunc (HSQUIRRELVM vm, const SQChar *s,...)
| If an error has to be print, this function is called.

## Private Types

typedef void( | SQPrintFunc )(bool error_msg, const SQChar *message)

## Private Member Functions

HSQUIRRELVM | GetVM ()
| Get the squirrel VM.

## Static Private Member Functions

static SQInteger | _RunError (HSQUIRRELVM vm)
| The internal RunError handler.

## Private Attributes

HSQUIRRELVM | vm
| The VirtualMachine instnace for squirrel.
void * | global_pointer
| Can be set by who ever initializes Squirrel.
SQPrintFunc * | print_func
| Points to either NULL, or a custom print handler.
bool | crashed
| True if the squirrel script made an error.

## Friends

class | AIScanner
class | AIInstance
void | squirrel_register_std (Squirrel *engine)

## Detailed Description

Definition at line 15 of file squirrel.hpp.

## Member Function Documentation

SQInteger Squirrel::_RunError | ( | HSQUIRRELVM | vm | ) | `[static, private]`

The internal RunError handler.

It looks up the real error and calls RunError with it.

Definition at line 85 of file squirrel.cpp.

References RunError().

void Squirrel::AddClassBegin | ( | const char * | class_name,
|  | const char * | parent_class |
| ) |  |  |

Adds a class to the global scope, extending 'parent_class'.

Make sure to call AddClassEnd when you are done adding methods.

Definition at line 155 of file squirrel.cpp.

References vm.

void Squirrel::AddClassBegin | ( | const char * | class_name | ) |

Adds a class to the global scope.

Make sure to call AddClassEnd when you are done adding methods.

Definition at line 148 of file squirrel.cpp.

References vm.

void Squirrel::AddClassEnd | ( |  | ) |

Finishes adding a class to the global scope.

If this isn't called, no class is really created.

Definition at line 168 of file squirrel.cpp.

References vm.

void Squirrel::AddConst | ( | const char * | var_name,
|  | bool | value |
| ) |  |  |

Adds a const to the stack.

Depending on the current state this means either a const to a class or to the global space.

Definition at line 141 of file squirrel.cpp.

References vm.

void Squirrel::AddConst | ( | const char * | var_name,
|  | int | value |
| ) |  |  |

Adds a const to the stack.

Depending on the current state this means either a const to a class or to the global space.

Definition at line 134 of file squirrel.cpp.

References vm.

void Squirrel::AddMethod | ( | const char * | method_name,
|  | SQFUNCTION | proc,
|  | uint | nparam = `0`,
|  | const char * | params = `NULL`,
|  | void * | userdata = `NULL`,
|  | int | size = `0` |
| ) |  |  |

Adds a function to the stack.

Depending on the current state this means either a method or a global function.

Definition at line 119 of file squirrel.cpp.

References vm.

Referenced by DefSQClass< CL >::DefSQAdvancedMethod(), DefSQClass< CL >::DefSQMethod(), DefSQClass< CL >::DefSQStaticMethod(), squirrel_register_global_std(), and squirrel_register_std().

bool Squirrel::CallMethod | ( | HSQOBJECT | instance,
|  | const char * | method_name,
|  | HSQOBJECT * | ret,
|  | int | suspend = `-1` |
| ) |  |  |

Call a method of an instance, in various flavors.

Returns:
  False if the script crashed or returned a wrong type.

Definition at line 208 of file squirrel.cpp.

References crashed, IsSuspended(), and vm.

Referenced by AIInstance::GameLoop(), AIInfo::GetSettings(), and AIInstance::Save().

bool Squirrel::CreateClassInstanceVM | ( | HSQUIRRELVM | vm,
|  | const char * | class_name,
|  | void * | real_instance,
|  | HSQOBJECT * | instance,
|  | SQRELEASEHOOK | release_hook |
| ) |  |  | `[static]`

Creates a class instance.

Parameters:

  | vm | The VM to create the class instance for
  | class_name | The name of the class of which we create an instance.
  | real_instance | The instance to the real class, if it represents a real class.
  | instance | Returning value with the pointer to the instance.
  | release_hook | Optional param to give a release hook.

Returns:
  False if creating failed.

Definition at line 266 of file squirrel.cpp.

Referenced by CreateClassInstance().

static bool Squirrel::GetInstance | ( | HSQUIRRELVM | vm,
|  | HSQOBJECT * | ptr,
|  | int | pos = `1` |
| ) |  |  | `[inline, static]`

Get the Squirrel-instance pointer.

Note:
  This will only work just after a function-call from within Squirrel to your C++ function.

Definition at line 173 of file squirrel.hpp.

Referenced by ScriptFileInfo::Constructor(), SQConvert::DefSQAdvancedNonStaticCallback(), and SQConvert::DefSQNonStaticCallback().

static bool Squirrel::GetRealInstance | ( | HSQUIRRELVM | vm,
|  | SQUserPointer * | ptr |
| ) |  |  | `[inline, static]`

Get the real-instance pointer.

Note:
  This will only work just after a function-call from within Squirrel to your C++ function.

Definition at line 166 of file squirrel.hpp.

HSQUIRRELVM Squirrel::GetVM | ( |  | ) | `[inline, private]`

Get the squirrel VM.

Try to avoid using this.

Definition at line 32 of file squirrel.hpp.

References vm.

Referenced by AIInstance::CallLoad(), AIInstance::GameLoop(), AIInstance::Load(), AIInstance::Save(), and squirrel_register_std().

bool Squirrel::IsSuspended | ( |  | ) |

Did the squirrel code suspend or return normally.

Returns:
  True if the function suspended.

Definition at line 517 of file squirrel.cpp.

Referenced by CallMethod(), and AIInstance::GameLoop().

bool Squirrel::LoadScript | ( | const char * | script | ) |

Load a script.

Parameters:

  | script | The full script-name to load.

Returns:
  False if loading failed.

Definition at line 486 of file squirrel.cpp.

Referenced by AIScanner::ImportLibrary(), AIInstance::LoadCompatibilityScripts(), SquirrelStd::require(), and ScriptScanner::ScanDir().

void Squirrel::SetGlobalPointer | ( | void * | ptr | ) | `[inline]`

Sets a pointer in the VM that is reachable from where ever you are in SQ.

Useful to keep track of the main instance.

Definition at line 194 of file squirrel.hpp.

References global_pointer.

Referenced by AIInstance::RegisterAPI().

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

- src/script/squirrel.hpp
- src/script/squirrel.cpp