|
| CBotProgram () |
| Constructor. More...
|
|
| CBotProgram (CBotVar *thisVar) |
| Constructor. More...
|
|
| ~CBotProgram () |
| Destructor. More...
|
|
bool | Compile (const std::string &program, std::vector< std::string > &externFunctions, void *pUser=nullptr) |
| Compile compiles the program given as string. More...
|
|
CBotError | GetError () |
| Returns the last error. More...
|
|
bool | GetError (CBotError &code, int &start, int &end) |
| Returns the last error. More...
|
|
bool | GetError (CBotError &code, int &start, int &end, CBotProgram *&pProg) |
| Returns the last error. More...
|
|
bool | Start (const std::string &name) |
| Starts the program using given function as an entry point. The function must be declared as "extern". More...
|
|
bool | Run (void *pUser=nullptr, int timer=-1) |
| Executes the program. More...
|
|
bool | GetRunPos (std::string &functionName, int &start, int &end) |
| Gives the current position in the executing program. More...
|
|
CBotVar * | GetStackVars (std::string &functionName, int level) |
| Provides the pointer to the variables on the execution stack. More...
|
|
void | Stop () |
| Stops execution of the program. More...
|
|
bool | SaveState (FILE *pf) |
| Save the current execution status into a file. More...
|
|
bool | RestoreState (FILE *pf) |
| Restore the execution state from a file. More...
|
|
bool | GetPosition (const std::string &name, int &start, int &stop, CBotGet modestart=GetPosExtern, CBotGet modestop=GetPosBloc) |
| GetPosition Gives the position of a routine in the original text the user can select the item to find from the beginning to the end see the above modes in CBotGet. More...
|
|
const std::list< CBotFunction * > & | GetFunctions () |
| Returns the list of all user-defined functions in this program as instances of CBotFunction. More...
|
|
bool | ClassExists (std::string name) |
| Check if class with that name was created in this program. More...
|
|
Class that manages a CBot program. This is the main entry point into the CBot engine.
Engine initialization / destruction
To initialize the CBot engine, call CBotProgram::Init(). This function should be only called once.
Afterwards, you can start defining custom functions, constants and classes. See:
Next, compile and run programs.
After you are finished, free the memory used by the CBot engine by calling CBotProgram::Free().
Example usage
std::vector<std::string> externFunctions;
bool ok = program->Compile(code.c_str(), externFunctions, nullptr);
if (!ok)
{
int cursor1, cursor2;
program->GetError(error, cursor1, cursor2);
}
program->Start(externFunctions[0]);
while (!program->Run());
static void Free()
Frees the static memory areas.
Definition: CBotProgram.cpp:414
CBotProgram()
Constructor.
Definition: CBotProgram.cpp:40
static bool AddFunction(const std::string &name, bool rExec(CBotVar *pVar, CBotVar *pResult, int &Exception, void *pUser), CBotTypResult rCompile(CBotVar *&pVar, void *pUser))
Add a function that can be called from CBot.
Definition: CBotProgram.cpp:321
static void Init()
Initializes the module, should be done once (and only once) at the beginning.
Definition: CBotProgram.cpp:392
CBotError
This enum contains possible CBot error values. Values in range 5000-5999 are compile errors,...
Definition: CBotEnums.h:191
bool CBot::CBotProgram::AddFunction |
( |
const std::string & |
name, |
|
|
bool |
rExecCBotVar *pVar, CBotVar *pResult, int &Exception, void *pUser, |
|
|
CBotTypResult |
rCompileCBotVar *&pVar, void *pUser |
|
) |
| |
|
static |
Add a function that can be called from CBot.
To define an external function, proceed as follows:
- Define a function for compilation
This function should take a list of function arguments (types only, no values!) and a user-defined void* pointer (can be passed in Compile()) as parameters, and return CBotTypResult.
Usually, name of this function is prefixed with "c".
The function should iterate through the provided parameter list and verify that they match.
If they don't, then return CBotTypResult with an appropariate error code (see CBotError).
If they do, return CBotTypResult with the function's return type
CBotTypResult cMessage(CBotVar* &var, void* user)
{
var = var->GetNext();
}
@ CBotErrOverParam
too many parameters
Definition: CBotEnums.h:221
@ CBotErrBadString
expected string
Definition: CBotEnums.h:234
@ CBotErrLowParam
not enough parameters
Definition: CBotEnums.h:223
@ CBotTypString
string
Definition: CBotEnums.h:46
@ CBotTypFloat
float
Definition: CBotEnums.h:43
- Define a function for execution
This function should take:
- a list of parameters
- pointer to a result variable (a variable of type given at compilation time will be provided)
- pointer to an exception variable
- user-defined pointer (can be passed in Run())
This function returns true if execution of this function is finished, or false to suspend the program and call this function again on next Run() cycle.
Usually, execution functions are prefixed with "r".
bool rMessage(CBotVar* var, CBotVar* result, int& exception, void* user)
{
std::string message = var->GetValString();
std::cout << message << std::endl;
return true;
}
- Call AddFunction() to register the function in the CBot engine
For more sophisticated examples, see the Colobot source code, mainly the src/script/scriptfunc.cpp file
- Parameters
-
name | Name of the function |
rExec | Execution function |
rCompile | Compilation function |
- Returns
- true