Wrapping my head around AngelScript

Started by
6 comments, last by thePyro_13 11 years, 10 months ago
I'm been playing with AngelScript for around a week or so, and having some trouble wrapping my head around how it all fits together. I'm hoping someone here can walk me through what I need to do to achieve what I want.

Basically I just want the entities in my project to be controlled by a script class(one class per entity type). The entities are described by xml(lists the script file, and image files used by the entity).

I think I need a single Script Engine for my entire app. Then do I need to build a module for each entity type? The entities are only different on the scripting side, they all use the same generic class on the C++ side.

I've been trying to work this all out from the Game sample included in the sdk, but some of it leaves me confused. I think it's supposed to be one module per entity, yet the example registers its objects to the engine rather than the module. Do I just register all of my classes(the ones I want scripts to be able to use) to the engine for all scripts to access?

Where is IController coming from? I couldn't find it in any of the script files in the game sample project or in the C++ app. Is it a generic part of AngelScript?

I suspect I'll have a lot more questions as I learn more about this library, sorry in advance if all my questions get annoying. :P
Advertisement
The game sample shows how to integrate the script engine similarly to how you want.

Let me try to clarify some of the doubts you have:

- You'll use a single script engine. It is possible to use multiple engines, but there are only disadvantages with that. (At least I can't think of any advantages at this moment)
- The application must register all functions and types that the scripts should be able to access in the application. If you wish to provide different functions and/or types to different script modules, for example if you have scripts controlling the GUI too. Then you can use access masks to control which module should be able to access which function.
- You'll have one module for each entity type, i.e. all zombie type entities will share a single module, and the player type will use another module, etc. The script code and global variables in the module will be shared between the entities (could be useful for collective behaviours).
- Each entity should have it's on script class which is instantiated from the entity type's module. The script class holds the class members with the information that only that specific entity has access to.
- The IController interface is registered by the application with RegisterInterface("IController"); The interface could be used to force the script class to implement specific methods, but in the game sample it is only used to facilitate identifying the script class that is supposed to control the entity. You don't have to use this if you don't want. Your XML file with the entity configuration could for example provide the name of the class directly instead.

I hope this clears up some of your doubts.

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

Thanks for your reply. That's helped me heaps.

I'll reply here again if I have any trouble.
Ok I've been building off the game sample, and have made good headway.

Currently both the level and the entities run from scripts. I've been trying to work out a way to expose a function to the level script which returns a script object handle rather than a handle to a c++ object.

Like this(TestArea.as):

#include "Entity.as" //defines the Entity angelscript class.
class TestArea
{
TestArea(Area @obj)
{
// Keep the owner for later reference
@self = obj;
@skeleton = self.CreateEntity("entites/skeleton/skeleton.xml", 5, 10); //loads the entity data from the xml and creates it.
}

Area @self;
Entity @skeleton;
}


Where 'CreateEntity' creates a scripted entity and places it in the entity manager associated with this TestArea class. But I'd also like to to return the script object associated with the C++ entity so that I can define additional functions and send data without having to go back through C++ land.

All I have in the C++ entity is it's asIObjectType and asIScriptObject. But I'm not sure how to get the script object in a format that I can pass back to my script.
Basically I want the Entity angelscript object, not the entity C++ object.

Is this possible? Any help will be greatly appreciated! :D
What you need is shared classes. Shared classes compiled in two different modules will share their code and typeids, which makes it possible to pass a pointer to that shared class from one module to the other. The drawback with shared classes is that they cannot access any non-shared entities, such as global variables or functions that have not been declared as shared too.

I assume your Entity class is the base class that all script controllers inherits from. Am I right? The Entity should then be declared as shared. The script controllers can still inherit from it without themselves being shared.

The CreateEntity() method should create the script controller instance, and then return the asIScriptObject pointer. As the controller inherits from the Entity class the level script will then be able to use is such.

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

That's right, they all inherit from Entity(except the level scripts).

I've declared it as shared, but I'm having trouble defining the interface.

r = ScriptEngine->RegisterObjectMethod("Area", "asIScriptObject@ CreateEntity(std::string source, int posx, int posy)", asMETHOD(Area, CreateEntity), asCALL_THISCALL); assert( r >= 0 );


This line throws an assert. Do I have to register to Entity type to the Script engine? And do I define the function above as returning asIScriptObject@ or Entity@(The C++ function is now returning asIScriptObject*)?
I'm sorry. I misunderstood your problem.

When registering the application interface, the engine still doesn't know of the Entity script class, so you can't register any functions that take or return that class. There are two ways of getting around this problem.

1. First register an interface from the application, e.g. RegisterInterface("IEntity"). The CreateEntity method can then be registered to return a handle to that interface, i.e. "RegisterObjectMethod("Area", "IEntity @CreateEntity(...)", ...). The Entity script class obviously needs implement the inteface for this to work. As the script that calls CreateEntity will receive an IEntity it needs to cast the handle to the Entity class before accessing its members, unless you registers methods directly with the interface.

2. Use the generic handle add-on, i.e. CScriptHandle, to return a handle that the script can cast to Entity as needed. In this case the CreateEntity() method will be registered to return the generic handle, i.e. "ref @CreateEntity(...)", which the calling script can cast to an Entity class. The implementation of the CreateEntity() method needs to be modified to return a CScriptHandle object by value.


CScriptHandle Area::CreateEntity(...)
{
...

// Create the Entity instance
int typeId = module->GetTypeIdByDecl("Entity");
asIScriptObject *obj = reinterpret_cast<asIScriptObject*>(engine->CreateScriptObject(typeId));

// Prepare the generic handle object for returning the Entity instance
CScriptHandle handle;
handle.opAssign(obj, typeId);

// The generic handle will hold on to its own reference, so we need to release ours
obj->Release();

// Return the generic handle object by value (even though the method is registered as 'ref @')
return handle;
}




I had an idea for an improvement in the library because of this problem you've faced. The application could register the name of the shared class as part of the configuration. This would allow it to register objects taking or returning the type. When the script is then compiled it will fill in the members and properties of the type. This would greatly simplify the solution for your problem. I'm not sure I'll be able to implement this for version 2.24.0, but I'll add this to my to-do list for a later release.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks for your help. I've got it working now with the scripthandle add-on.

Thanks for taking your time to help me. The angelscript library is really cool. :D

This topic is closed to new replies.

Advertisement