Print function in tutorial
Newbee question. I tried to use the Print function outside the the float function. It is not working. Why even after it is registered as a global function?
float calc(float a, float b)
{
// Print the value that we received
Print("Received: " + a + ", " + b + "\n");
// Print the current system time
Print("System has been running for " + GetSystemTime()/1000.0 + " seconds\n");
// Do the calculation and return the value to the application
return a * b;
}
Print("Hello");//added
Will somebody help us newbees with a simple c++ example as how to create and register a barebone program; without error checking.
I am familiar with lua and learned the basics from sites like this.
http://csl.sublevel3.org/lua/
Will somebody post something similar to this with Angelscript? Thank you.
there is more if you look into the zipped archive of the lib.
if you go through /sdk/tests/test_feature/source/ you can find a bunch of examples
on the main features of the library. looking into /sdk/samples/ you can find some more
powerful examples on how to build more complex programs with that...
if you go through /sdk/tests/test_feature/source/ you can find a bunch of examples
on the main features of the library. looking into /sdk/samples/ you can find some more
powerful examples on how to build more complex programs with that...
Is that code you posted an AngelScript Script or C++? No, it doesn't matter - that's illegal in both! Statements have to be inside a function, they can't just be floating around loose in the file. If you were looking at the errors you were getting from angelscript, it would have told you that!
Thanks for the replies. Even if I wrap it inside a main function it won't work.
Error message "Expected identifier."
Is there a main function built in? If there is not, how can I create one?
Sorry, I have gone through the samples before posting. They are not barebone. I still can't figure out which functions are needed to create, register, compile and run the script. It seems straightforward once we know the proper name and order of functions.
I ripped the sample code, it compiles but can't run the script.
How the calc function is registered in the script? There is no seperate register function for it like there is for Print.
I was looking for a barebone example, with bare necessary functions, to load a file and run the script with a simple registered function in a console program.
I have gone through the documentation, very well done, but not even one full simple, complete example is there.
Error message "Expected identifier."
Is there a main function built in? If there is not, how can I create one?
Sorry, I have gone through the samples before posting. They are not barebone. I still can't figure out which functions are needed to create, register, compile and run the script. It seems straightforward once we know the proper name and order of functions.
I ripped the sample code, it compiles but can't run the script.
How the calc function is registered in the script? There is no seperate register function for it like there is for Print.
I was looking for a barebone example, with bare necessary functions, to load a file and run the script with a simple registered function in a console program.
I have gone through the documentation, very well done, but not even one full simple, complete example is there.
Ok. Got this far from the sample code. But can't figure out how to bind a function for use from script. Tried to create a Hello world function. But it is not being found by the script.
Please help.
//
// Unit test author: Fredrik Ehnbom
//
// Description:
//
// Tests calling a script-function from c.
// Based on the sample found on angelscripts
// homepage.
//
#include "utils.h"
#include <stdio.h>
#include <conio.h>
#include <iostream.h> // cout
#define TESTNAME "TestExecuteScript"
static asIScriptEngine *engine;
static int LoadScript(const char *filename);
static int CompileScript();
static bool ExecuteScript();
bool TestExecuteScript();
void Print();//tried to bind this function inside ExecuteScript().Not working.
main(){
int r;
TestExecuteScript();
getch();
return 0;
}
//===========================================================
bool TestExecuteScript()
{
bool ret = false;
engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
if( LoadScript("TestExecuteScript.as") < 0 )
{
engine->Release();
return false;
}
CompileScript();
ret = ExecuteScript();
engine->Release();
engine = NULL;
return ret;
}
//LoadScript()==================================================================
static int LoadScript(const char *filename)
{
// Read the entire script file
FILE *f = fopen(filename, "rb");
if( f == 0 )
{
printf("%s: Failed to open the script file.\n", TESTNAME);
return -1;
}
fseek(f, 0, SEEK_END);
int len = ftell(f);
fseek(f, 0, SEEK_SET);
// On Win32 it is possible to do the following instead
// int len = _filelength(_fileno(f));
std::string code;
code.resize(len);
size_t c = fread(&code[0], len, 1, f);
fclose(f);
if( c == 0 )
{
printf("%s: Failed to load script file.\n", TESTNAME);
return -1;
}
// Give the code to the script engine
int r = engine->AddScriptSection(0, filename, code.c_str(), len, 0);
if( r < 0 )
{
printf("%s: An error occured while adding the script section.\n", TESTNAME);
return r;
}
// At this point the engine has copied the code to an
// internal buffer so we are free to release the memory we
// allocated.
// We can also add other script sections if we would like.
// All script sections will be compiled together as if they
// where one large script.
return 0;
}
//CompileScript()==============================================================
static int CompileScript()
{
// Create an output stream that will receive information about the build
COutStream out;
engine->SetMessageCallback(asMETHOD(COutStream,Callback), &out, asCALL_THISCALL);
int r = engine->Build(0);
if( r < 0 )
{
printf("%s: Failed to compile script\n", TESTNAME);
return -1;
}
// If we wish to build again, the script sections has to be added again.
// Now we will verify the interface of the script functions we wish to call
const char *buffer = 0;
buffer = engine->GetFunctionDeclaration(engine->GetFunctionIDByName(0, "main"));
if( buffer == 0 )
{
printf("%s: Failed to retrieve declaration of function 'main'\n", TESTNAME);
return -1;
}
engine->ClearMessageCallback();
return 0;
}
//ecuteScript()===============================================================
static bool ExecuteScript()
{
// Create a context in which the script will be executed.
// Several contexts may exist in parallel, holding the execution
// of various scripts in the same engine. At the moment contexts are not
// thread safe though so you should make sure that only one executes
// at a time. An execution can be suspended to allow another
// context to execute.
asIScriptContext *ctx = engine->CreateContext();
if( ctx == 0 )
{
printf("%s: Failed to create a context\n", TESTNAME);
return true;
}
// Prepare the context for execution
// When a context has finished executing the context can be reused by calling
// PrepareContext on it again. If the same stack size is used as the last time
// there will not be any new allocation thus saving some time.
int r = ctx->Prepare(engine->GetFunctionIDByName(0, "main"));
if( r < 0 )
{
printf("%s: Failed to prepare context\n", TESTNAME);
return true;
}
//tried to register Print()============================================
r=engine->RegisterGlobalFunction("void Print()", asFUNCTION(Print),
asCALL_CDECL); assert( r >= 0 );
// If the script function takes any parameters we need to
// copy them to the context's stack by using SetArguments()
// Execute script
r = ctx->Execute();
if( r < 0 )
{
printf("%s: Unexpected error during script execution\n", TESTNAME);
return true;
}
if( r == asEXECUTION_FINISHED )
{
// If the script function is returning any
// data we can get it with GetReturnValue()
float retVal = ctx->GetReturnFloat();
cout<<retVal;//added to print our return value
if (retVal == 7.826446f)
r = 0;
else
printf("%s: Script didn't return the correct value. Returned: %f, expected: %f\n", TESTNAME, retVal, 7.826446f);
}
else if( r == asEXECUTION_SUSPENDED )
{
printf("%s: Execution was suspended.\n", TESTNAME);
// In this case we can call Execute again to continue
// execution where it last stopped.
int funcID = ctx->GetCurrentFunction();
printf("func : %s\n", engine->GetFunctionName(funcID));
printf("line : %d\n", ctx->GetCurrentLineNumber());
}
else if( r == asEXECUTION_ABORTED )
{
printf("%s: Execution was aborted.\n", TESTNAME);
}
else if( r == asEXECUTION_EXCEPTION )
{
printf("%s: An exception occured during execution\n", TESTNAME);
// Print exception description
int funcID = ctx->GetExceptionFunction();
printf("func : %s\n", engine->GetFunctionName(funcID));
printf("line : %d\n", ctx->GetExceptionLineNumber());
printf("desc : %s\n", ctx->GetExceptionString());
}
// Don't forget to release the context when you are finished with it
ctx->Release();
return false;
}
//Print()===============================================================
void Print()
{
cout<<"Hello Angelic World";
}
Script File
------------
float main()
{
Print();//I tried to use it. Error: "No matching signatures to Print()"
float ret = 1;
float ret2 = 0;
float ret3 = 1;
int i2 = 0;
for (int i = 0; i < 2; i++) {
ret = ret * (ret+1);
ret2 += float(i);
ret3 /= 1.1f;
i2++;
i2++;
}
int8 a = 1, b = 2, c = 3, d = 4, e = 5, f = 6;
a = b + c + d + e + f;
return ret + ret2 + ret3;
}
Please help.
//
// Unit test author: Fredrik Ehnbom
//
// Description:
//
// Tests calling a script-function from c.
// Based on the sample found on angelscripts
// homepage.
//
#include "utils.h"
#include <stdio.h>
#include <conio.h>
#include <iostream.h> // cout
#define TESTNAME "TestExecuteScript"
static asIScriptEngine *engine;
static int LoadScript(const char *filename);
static int CompileScript();
static bool ExecuteScript();
bool TestExecuteScript();
void Print();//tried to bind this function inside ExecuteScript().Not working.
main(){
int r;
TestExecuteScript();
getch();
return 0;
}
//===========================================================
bool TestExecuteScript()
{
bool ret = false;
engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
if( LoadScript("TestExecuteScript.as") < 0 )
{
engine->Release();
return false;
}
CompileScript();
ret = ExecuteScript();
engine->Release();
engine = NULL;
return ret;
}
//LoadScript()==================================================================
static int LoadScript(const char *filename)
{
// Read the entire script file
FILE *f = fopen(filename, "rb");
if( f == 0 )
{
printf("%s: Failed to open the script file.\n", TESTNAME);
return -1;
}
fseek(f, 0, SEEK_END);
int len = ftell(f);
fseek(f, 0, SEEK_SET);
// On Win32 it is possible to do the following instead
// int len = _filelength(_fileno(f));
std::string code;
code.resize(len);
size_t c = fread(&code[0], len, 1, f);
fclose(f);
if( c == 0 )
{
printf("%s: Failed to load script file.\n", TESTNAME);
return -1;
}
// Give the code to the script engine
int r = engine->AddScriptSection(0, filename, code.c_str(), len, 0);
if( r < 0 )
{
printf("%s: An error occured while adding the script section.\n", TESTNAME);
return r;
}
// At this point the engine has copied the code to an
// internal buffer so we are free to release the memory we
// allocated.
// We can also add other script sections if we would like.
// All script sections will be compiled together as if they
// where one large script.
return 0;
}
//CompileScript()==============================================================
static int CompileScript()
{
// Create an output stream that will receive information about the build
COutStream out;
engine->SetMessageCallback(asMETHOD(COutStream,Callback), &out, asCALL_THISCALL);
int r = engine->Build(0);
if( r < 0 )
{
printf("%s: Failed to compile script\n", TESTNAME);
return -1;
}
// If we wish to build again, the script sections has to be added again.
// Now we will verify the interface of the script functions we wish to call
const char *buffer = 0;
buffer = engine->GetFunctionDeclaration(engine->GetFunctionIDByName(0, "main"));
if( buffer == 0 )
{
printf("%s: Failed to retrieve declaration of function 'main'\n", TESTNAME);
return -1;
}
engine->ClearMessageCallback();
return 0;
}
//ecuteScript()===============================================================
static bool ExecuteScript()
{
// Create a context in which the script will be executed.
// Several contexts may exist in parallel, holding the execution
// of various scripts in the same engine. At the moment contexts are not
// thread safe though so you should make sure that only one executes
// at a time. An execution can be suspended to allow another
// context to execute.
asIScriptContext *ctx = engine->CreateContext();
if( ctx == 0 )
{
printf("%s: Failed to create a context\n", TESTNAME);
return true;
}
// Prepare the context for execution
// When a context has finished executing the context can be reused by calling
// PrepareContext on it again. If the same stack size is used as the last time
// there will not be any new allocation thus saving some time.
int r = ctx->Prepare(engine->GetFunctionIDByName(0, "main"));
if( r < 0 )
{
printf("%s: Failed to prepare context\n", TESTNAME);
return true;
}
//tried to register Print()============================================
r=engine->RegisterGlobalFunction("void Print()", asFUNCTION(Print),
asCALL_CDECL); assert( r >= 0 );
// If the script function takes any parameters we need to
// copy them to the context's stack by using SetArguments()
// Execute script
r = ctx->Execute();
if( r < 0 )
{
printf("%s: Unexpected error during script execution\n", TESTNAME);
return true;
}
if( r == asEXECUTION_FINISHED )
{
// If the script function is returning any
// data we can get it with GetReturnValue()
float retVal = ctx->GetReturnFloat();
cout<<retVal;//added to print our return value
if (retVal == 7.826446f)
r = 0;
else
printf("%s: Script didn't return the correct value. Returned: %f, expected: %f\n", TESTNAME, retVal, 7.826446f);
}
else if( r == asEXECUTION_SUSPENDED )
{
printf("%s: Execution was suspended.\n", TESTNAME);
// In this case we can call Execute again to continue
// execution where it last stopped.
int funcID = ctx->GetCurrentFunction();
printf("func : %s\n", engine->GetFunctionName(funcID));
printf("line : %d\n", ctx->GetCurrentLineNumber());
}
else if( r == asEXECUTION_ABORTED )
{
printf("%s: Execution was aborted.\n", TESTNAME);
}
else if( r == asEXECUTION_EXCEPTION )
{
printf("%s: An exception occured during execution\n", TESTNAME);
// Print exception description
int funcID = ctx->GetExceptionFunction();
printf("func : %s\n", engine->GetFunctionName(funcID));
printf("line : %d\n", ctx->GetExceptionLineNumber());
printf("desc : %s\n", ctx->GetExceptionString());
}
// Don't forget to release the context when you are finished with it
ctx->Release();
return false;
}
//Print()===============================================================
void Print()
{
cout<<"Hello Angelic World";
}
Script File
------------
float main()
{
Print();//I tried to use it. Error: "No matching signatures to Print()"
float ret = 1;
float ret2 = 0;
float ret3 = 1;
int i2 = 0;
for (int i = 0; i < 2; i++) {
ret = ret * (ret+1);
ret2 += float(i);
ret3 /= 1.1f;
i2++;
i2++;
}
int8 a = 1, b = 2, c = 3, d = 4, e = 5, f = 6;
a = b + c + d + e + f;
return ret + ret2 + ret3;
}
You have to register the functions before compiling the script. Add the code right above your CompileScript() call.
PS: use the [source][/source] tags
PS: use the [source][/source] tags
Thank you. It worked.
Sorry about the code block. I tried to use
Still, I think it will be very useful, to have some simple tutorials in wiki or elsewhere.
Thanks again to everybody.
Sorry about the code block. I tried to use
. It didn't work. So removed it. Still, I think it will be very useful, to have some simple tutorials in wiki or elsewhere.
Thanks again to everybody.
I think the problem is that we already consider the samples simple.
But I think I'll write a 'hello world' tutorial and post it up here. It is about time we had actual tutorials instead of just samples.
Oh geez. I never realized a simple hello world was actually so hard. Things the tutorial would have to cover:
-Creating script engines
-Registering a string type
-Registering a global function
-Compiling a script
-Creating a context
-Preparing and executing a context
But I think I'll write a 'hello world' tutorial and post it up here. It is about time we had actual tutorials instead of just samples.
Oh geez. I never realized a simple hello world was actually so hard. Things the tutorial would have to cover:
-Creating script engines
-Registering a string type
-Registering a global function
-Compiling a script
-Creating a context
-Preparing and executing a context
Great!!Please do it. The beginners of the community will be very thankful.
Hi baard,
Sorry about being so late in answering your post. I've been extremely busy.
Anyway, I'm glad to see that some of the more experienced users were able to help out in my absence. Thanks for that guys.
I'd welcome help to improve the resources for the community, i.e. tutorials, samples, demos, etc. I can't do it all by myself.
But, I promise that I'll work on a simple get started tutorial for inclusion in the SDK. I recognize that it is necessary for the new comers to have something like this. And it will also be useful for those that are evaluating the possibility of using AngelScript in their own projects.
Regards,
Andreas
Sorry about being so late in answering your post. I've been extremely busy.
Anyway, I'm glad to see that some of the more experienced users were able to help out in my absence. Thanks for that guys.
I'd welcome help to improve the resources for the community, i.e. tutorials, samples, demos, etc. I can't do it all by myself.
But, I promise that I'll work on a simple get started tutorial for inclusion in the SDK. I recognize that it is necessary for the new comers to have something like this. And it will also be useful for those that are evaluating the possibility of using AngelScript in their own projects.
Regards,
Andreas
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement