• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Squared'D

C++ STL-Style Array That's Compatible with AngelScript

8 posts in this topic

I've written a wrapper for the CScriptArray add-on that makes it so you can use it almost exactly like std::vector. I wrote it so I could more easily share arrays with the C++ program and AngelScript. I've written a blog post for it. You can find the CScriptArraySTL class here:

 

http://squaredprogramming.blogspot.kr/2014/01/c-stl-style-array-thats-compatible-with.html

or

http://www.gamedev.net/blog/1670/entry-2259127-c-stl-style-array-thats-compatible-with-angelscript/

 

Let me know if you have any problems or if it's lacking any important functionality. I hope it's useful to others.

0

Share this post


Link to post
Share on other sites

Interesting. I'll take a closer look at this later on. Perhaps I can incorporate part of into the CScriptArray add-on directly (with your permission of course).

0

Share this post


Link to post
Share on other sites

You're allowing C++ and script to easily share data through a 'global' (actually a global C++ variable, or at least lives long enough to cover the execution of a given script coroutine) instance of e.g. a string array?

 

Am I correct in guessing that this wouldn't work if you were running simultaneous coroutines via threads?

0

Share this post


Link to post
Share on other sites

Interesting. I'll take a closer look at this later on. Perhaps I can incorporate part of into the CScriptArray add-on directly (with your permission of course).

 

I hope it's useful. The code is completely open so if you'd like to incorporate it that'll be ok. I'm glad that I'm able to contribute.

0

Share this post


Link to post
Share on other sites

You're allowing C++ and script to easily share data through a 'global' (actually a global C++ variable, or at least lives long enough to cover the execution of a given script coroutine) instance of e.g. a string array?

 

Am I correct in guessing that this wouldn't work if you were running simultaneous coroutines via threads?

 

The main purpose for writing the CScriptArraySTL class was so I could manipulate a global array in C++ and AngelScript directly without copying data or manually casting void pointers. Underneath, this uses the CScriptArray class that's in the AngelScript add_on folder so it can do everything that CScriptArray can do. CScriptArraySTL creates a CScriptArray object and that object is what can be registered. If you register it as a global property, the object should exist as long as the script is running, but becuase CScriptArray is a reference type object, you could also return a handle to it from a function. If you do this, the reference counted CScriptArray would persist until the reference count reaches zero.

 

Right now it can't using an existing array, but that's something I'm thinking about adding. Then you'll be able to use it to manipulate arrays created inside a script.

 

This example can be found here: http://www.angelcode.com/angelscript/sdk/docs/manual/doc_addon_array.html

// Registered with AngelScript as 'array<string> @CreateArrayOfString()'
CScriptArray *CreateArrayOfStrings()
{
  // If called from the script, there will always be an active
  // context, which can be used to obtain a pointer to the engine.
  asIScriptContext *ctx = asGetActiveContext();
  if( ctx )
  {
    asIScriptEngine* engine = ctx->GetEngine();

    // The script array needs to know its type to properly handle the elements.
    // Note that the object type should be cached to avoid performance issues
    // if the function is called frequently.
    asIObjectType* t = engine->GetObjectTypeById(engine->GetTypeIdByDecl("array<string>"));

    // Create an array with the initial size of 3 elements
    CScriptArray* arr = new CScriptArray(3, t);
    for( asUINT i = 0; i < arr->GetSize(); i++ )
    {
      // Set the value of each element
      string val("test");
      arr->SetValue(i, &val);
    }

    // The ref count for the returned handle was already set in the array's constructor
    return arr;
  }
  return 0;
}

I can re-write it using the CScriptArraySTL class so I can write it like std::vector

// Registered with AngelScript as 'array<string> @CreateArrayOfString()'
CScriptArray *CreateArrayOfStrings()
{
  // If called from the script, there will always be an active
  // context, which can be used to obtain a pointer to the engine.
  asIScriptContext *ctx = asGetActiveContext();
  if( ctx )
  {
    asIScriptEngine* engine = ctx->GetEngine();

    CScriptArraySTL <string> arr;

    // The script array needs to know its type to properly handle the elements.
    // Note that the object type should be cached to avoid performance issues
    // if the function is called frequently.
    // create the array with an initial size of 3
    arr.InitArray(engine, "array<string>", 3);

    // use iterators to fill it
    for(CScriptArraySTL <string>::iterator it = arr.begin(); it < arr.end(); ++it)
    {
       *it = "test";
    }

    // The ref count for the returned handle was already set in the array's constructor
    // added parameters to GetRef() 2014-01-06
    return arr.GetRef(false /*don't increment the reference count*/, true /*erase data from this object when done*/);
  }
  return 0;
}

Note: Thanks for this question. Because of this, I changed GetRef() to take two optional parameters.

0

Share this post


Link to post
Share on other sites

Thanks for this - I look forward to studying the code more closely as I get started with AngelScript myself.

 

While we're on this topic.. does the primary difficulty with std::vector and script interoperability lie with the templated nature of the C++ container, which is resolved at C++-compile-time... so existing attempts to bind std::vector are themselves templated and require upfront binding for every type you want to expose... which would mean that you could expose (to script) std::vectors of all the primitive types and your registered application types... but when dealing with a vector in script, it would never be able to hold script-created types (such as script-class instances)?

0

Share this post


Link to post
Share on other sites

@iraxef

 

This is exactly the reason why registering the std::vector can't be used for the dynamic script array. Since the C++ template is resolved at compile time there is no way for the script engine to create instances for each subtype used by the script dynamically. The application would have to register all possible types at start-up, which isn't viable, since the number of combinations can be infinite. 

 

It is possible to use and register the std::vector for specific types, e.g. if you have situations where the script needs to manipulate a std::vector owned by the application, then you could register the std::vector of that particular type with AngelScript at start-up and then allow the script to interact with it directly. Remember that the std::vector won't treat incorrect usage in a friendly manner (i.e. out-of-bounds checks, etc), so if you want to get a friendly script exception if the script tries to access elements that don't exist you'll need to wrap these methods of the std::vector and add this check on your own.

0

Share this post


Link to post
Share on other sites

If you're binding the add-on which creates the 'ref' script type.. could you bind an std::vector which contains CScriptHandle (or CScriptHandle*) and then you'd have a script-side std::vector type that can store any object handle?

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0