a few questions...

Started by
5 comments, last by WitchLord 19 years, 4 months ago
Hi Firstly congratulations on writing a nice bit of software. I'd implemented Lua but switched to AngelScript as it seems to suit my needs and it's a bit clearer on how to get things working. The majority of my code is written in straight C. So far everything has gone smoothly but I have a few questions. * In AddScriptSection what's the relationship between module and section? What exactly is a section? I have several scripts (.as files) for my frontend - would I use the unique filename as the module name? If so what do I put for the section? * I have a C structure type called Screen and I want the scripts to use this but by reference only. So I have a Screen_SetPosition( Screen *scr, U32 x, U32 y ). How do I handle something like this? * How do I pass variables to script functions. I can't find an example for the life of me. I have a script like this... void Initialise( int a ) { Script_Printf(a); } The Printf function works fine (it's not a proper print, just wanted to debug it), so if I pass it Script_Printf(20) it prints 20 but the passed in variable is always comes out as 0. I call ctx->SetArguments( 0, (asDWORD*) &val, 1 ); before Preparing and executing the script. I've tracked this down to something to do with the argument size but I don't know what else I need to do. * Is there a problem with having several scripts with the same function name? That's all the questions I have (for the moment :) ). As a suggestion would it be possible on having a tutorial about getting a basic system up and running? What I mean is, it's straightforward to get a script in there and running but how do you work with 20 or 30 scripts? One for each enemy ai? at the same time? Even if it was something as simple as having two script files that print a message over a period of time. eg. // script 1 void Update( void ) { while (true) { Print( "hello world", 10, 10 ); Wait(1000); Print( "I've waited a second", 10, 10); Wait(500); } } // script 2 void Update( void ) { while (true) { Print( "I'm waiting for the application before carrying on", 400, 10 ); Wait(); Print( "Ok. Now I'll wait a bit before going back to the start, 400, 10 ); Wait(500); } } It would help me loads as getting a script to run isn't a problem but multiple scripts together is another matter entirely. I'm a bit scared that I'll do something stupid and have bugs creep in later on. Thanks and keep up the good work! This is definately more application friendly than lua. I found lua to be too much about the lua language and not enough on embedding it in real world situations (ie. games).
Advertisement
First of all I would like to thank you for choosing AngelScript. If you find that there is something missing in the library please let me know.

I recognize that AngelScript is still missing a lot of things, tutorials and examples are one such thing. I will try to write some, but I have a lot of features that I want to implement in AngelScript and that is my priority right now.

* The module name is kind of like a namespace. You could for example have two different modules with the exact same function signatures. The application can then choose in which module to call the functions by specifying the module name.

The script section name is only used for compiler messages, so that the user is able to identify in what part of the script the error is. If you are compiling files, then a good section name is the file name.

* You have several options for this. You could let the script store the pointer to the screen object directly, like "Screen *screen;". You could also register a special handle object, that would allow the script engine to do automatic reference counting on the screen object (if you're using this). Another option would be to register the screen object as a global property.

The fact that your screen functions take a pointer to the screen object, allows you to register the screen functions in several ways:

RegisterGlobalFunction("void Screen_SetPosition(Screen &, uint, uint)", asFUNCTION(...), asCALL_CDECL);
RegisterGlobalFunction("void Screen_SetPosition(Screen *, uint, uint)", asFUNCTION(...), asCALL_CDECL);

You may even register it so that it appears like an object method to the script.

RegisterObjectMethod("Screen", "void SetPosition(uint, uint)", asFUNCTION, asCALL_CDECL_OBJFIRST);

* I believe the problem is that you are doing it in the wrong order.

ctx->Prepare(...);
ctx->SetArguments(0, (asDWORD*)&val, 1);
ctx->Execute();

Prepare() sets the context up for executing the function, and also defines how many parameters that are expected, etc.

* No, there is no problem with this. If the two functions have different parameter types they can even be in the same script module (function overloading). If they have the same parameter types you need to compile them into two different modules (namespace).

To have 20-30 scripts that all have the same script function signatures, you would compile the scripts into different modules, where the module name ideally reflect the AI type.

---

I agree with you. In my opinion Lua was written in order to accomodate new language constructions such as the metatables, which I believe is a good thing, but not useful for everyone. With AngelScript I'm not trying to invent new language constructs, I'm simply trying to write a scripting library that is as easy as possible to use and embed in C++ applications.

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

Hi

Thanks for the quick reply!

Regarding the module\section name thing. That makes sense - I was going with the module name as the filename but I see how it works now. Thanks.

And you were spot on with the Prepare thing, just had to move a function call and it all worked fine!

Thanks again!

Scott
Quote:Original post by WitchLord
* The module name is kind of like a namespace. You could for example have two different modules with the exact same function signatures. The application can then choose in which module to call the functions by specifying the module name.


Sorry to hijack the thread with something unrelated, but are there any plans in version 2.0 to allow namespaces declared in the script, then perhaps we wouldn't need to worry about the whole module thingy?

Quote:
The fact that your screen functions take a pointer to the screen object, allows you to register the screen functions in several ways:

RegisterGlobalFunction("void Screen_SetPosition(Screen &, uint, uint)", asFUNCTION(...), asCALL_CDECL);
RegisterGlobalFunction("void Screen_SetPosition(Screen *, uint, uint)", asFUNCTION(...), asCALL_CDECL);

You may even register it so that it appears like an object method to the script.

RegisterObjectMethod("Screen", "void SetPosition(uint, uint)", asFUNCTION, asCALL_CDECL_OBJFIRST);


In version 2.0 you're going to remove pointers and pass objects by references. How will this work exactly? If you pass them by reference by default and in the function I modify the variable, it shouldn't be altered when the function returns, but because you're passing them by reference, it will be modified. Perhaps the solution is to pass them as a constant reference, unless a special modifier is used (such as function(object & modifyable);)


Like I said, I know this isn't helping the original posters' questions, but I was just reading your reply and thought of those questions.

Thanks in advance.
I don't think he minds, after all he already got the answers he wanted. And the topic title still suits your post. :)

I don't have any immediate plans to add namespaces in the scripts, but it would certainly be possible. Maybe in a future version of 2.x.x.

An object will be passed by reference to functions, but it will be transparent to the script writer. This means that if the script writer writes a function that takes an object parameter by value, the script engine will make a copy of the object before passing it to the function. Passing the parameters as const refernence would be faster, but would also require changes to the language that I don't want to do. Anyway, if the script writer wants the extra performance he could himself declare the function to take the arguments as const ref.

The fact that all objects are stored by reference instead of directly on the stack, allows me to have better control over the objects. For example the exception handler can identify the object type dynamically, which makes it much easier to write. It will make it much easier to write context serialization, which is something that has been asked for. The interface between application and script functions should also become easier to work with. It will also allow the application to register functions that take references to objects it doesn't know anything about beforehand, for example if the script writes a class that inherits from an application object, it can send the derived object to the application, and the application can dynamically determine the script object's properties and methods.

There are some drawbacks with this, as it puts restrictions on the application functions that can be registered, but I believe that in the end it will be for the better. I will try my best to minimize the restrictions, hence less need for wrapper functions.

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

I'm just wondering how these references fit in with the host code. If you register a function with AS2, do you have to use references there, or are pointers also allowed (I don't know how C(++) deals with references internally).
Internally references and pointers are the same thing to C/C++, it's just the allowed operations that are different.

AngelScript will hold the references in a handle object (kind of like a smart pointer), so it is not a direct reference like in C++. However I plan to have AS2 automatically convert these handle objects to direct pointers/references to the true object, so the application still can register normal functions that take pointers to objects. It will be possible to access the handle object as well, for example if the application wish to query properties on script declared classes.

Obviously this will cause a performance hit when it comes to handling objects, but I think the greater flexibility will compensate for it.

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