Jump to content
  • Advertisement

robiwan

Member
  • Content Count

    7
  • Joined

  • Last visited

Community Reputation

107 Neutral

About robiwan

  • Rank
    Newbie
  1. robiwan

    Access violation in AS engine

    Okidoki, was searching for something like that
  2. robiwan

    Access violation in AS engine

    Haha! Fixed. Problem was related to push_back on std::vector, which probably reallocated the entries on second push_back. If I just do outputs.reserve(N) then it works like a charm.
  3. robiwan

    Access violation in AS engine

    Ok, so I've successfully setup AS in my project so I can get hold of input arrays, and create output arrays. Or rather, and this is the strange part, I can only create one output array. As soon as I create another one, regardless of whether I use that array, I get an access violation in as_context.cpp (line 2675, AS2.24.0). My script code looks like: [source lang="cpp"]bool process() { const array@ in0 = getInput(0); uint len0 = in0.length(); array@ out0 = createOutput(0, len0); array@ out1 = createOutput(1, len0); print("Hello World! Length = " + len0 + "\n"); for (uint i = 0; i < len0; ++i) { print(in0 + "\n"); out0 = -in0; out1 = in0; } return true; } [/source] The C++ array class is as follows: [source lang="cpp"] class DoubleArray { double *m_buffer; unsigned int m_length; public: DoubleArray(double* p, unsigned length) : m_buffer(p), m_length(length) {;} DoubleArray(const DoubleArray& other) : m_buffer(other.m_buffer), m_length(other.m_length) {;} void AddRef() const { } void Release() const { } unsigned int length() const { return m_length; } double at(unsigned int index) const { if( index >= m_length ) { // Raise an exception to the script asIScriptContext *ctx = asGetActiveContext(); if( ctx ) ctx->SetException("Out of bounds"); return 0; } // Return by value as the array shouldn't be modified by the script return m_buffer[index]; } double* at(unsigned int index) { if( index >= m_length ) { // Raise an exception to the script asIScriptContext *ctx = asGetActiveContext(); if( ctx ) ctx->SetException("Out of bounds"); return 0; } return m_buffer + index; } static void Register(asIScriptEngine* engine) { int r = engine->RegisterObjectType("array", 0, asOBJ_REF); assert( r >= 0 ); // The memory management methods r = engine->RegisterObjectBehaviour("array", asBEHAVE_ADDREF, "void f()", asMETHOD(DoubleArray,AddRef), asCALL_THISCALL); assert( r >= 0 ); r = engine->RegisterObjectBehaviour("array", asBEHAVE_RELEASE, "void f()", asMETHOD(DoubleArray,Release), asCALL_THISCALL); assert( r >= 0 ); r = engine->RegisterObjectMethod("array", "double &opIndex(uint)", asMETHODPR(DoubleArray, at, (asUINT), double*), asCALL_THISCALL); assert( r >= 0 ); r = engine->RegisterObjectMethod("array", "double opIndex(uint) const", asMETHODPR(DoubleArray, at, (asUINT) const, double), asCALL_THISCALL); assert( r >= 0 ); r = engine->RegisterObjectMethod("array", "uint length() const", asMETHODPR(DoubleArray, length, () const, asUINT), asCALL_THISCALL); assert( r >= 0 ); } }; [/source] And the createOutput function simple creates new DoubleArray(new double[length],length) (and stores it internally in a context object, that's why AddRef/Release aren't implemented). The registering function is: [source lang="cpp"]engine->RegisterGlobalFunction("array@ createOutput(uint,uint)", asFUNCTION(SContextFunctions::create_output), asCALL_CDECL); static DoubleArray* create_output(unsigned index, unsigned length) { asIScriptContext* ctx = asGetActiveContext(); SContextFunctions* pCtx = (SContextFunctions*)ctx->GetUserData(); if (index > pCtx->outputs.size()) { ctx->SetException("Outputs must be created in order 0..N"); return NULL; } if (index < pCtx->outputs.size()) { ctx->SetException("Output already created"); return NULL; } if (index >= 8) { ctx->SetException("Maximum 8 outputs supported"); return NULL; } pCtx->outputs.push_back(DoubleArray(new double[length], length)); return &(pCtx->outputs.back()); } [/source] The outputs is a std::vector<DoubleArray>. It is probably something in my code I haven't spotted yet, but I'm pretty confident there's no memory overwrites etc. Ideas?
  4. robiwan

    Exposing arrays

    Ok, I was thinking along those lines aswell, thanx. It might be the only type of array supported, but I need to be able to create arrays as well, so I'll tinker around with the array add-on, see if I can combine some ideas with the one you gave above for a lightweight wrapper.
  5. robiwan

    Exposing arrays

    See the reply to my original post for one example of how to. Maybe even cleaner would be to have the C++ arrays registered as: double[][] input; but the getInput(arg) would suffice well.
  6. robiwan

    Exposing arrays

    Well actually I might have several C++ arrays (const double* that is) that I'd like to expose as properties to the script as: [source lang="cpp"]double[] in0 = getInput(0); uint in0Length = in0.length(); double[] in1 = getInput(1); uint in1Length = in1.length(); [/source] Then I'd also like to be able to create arrays as: [source lang="cpp"]double[] out0 = createOutput(0); out0.resize(512); double[] out1 = createOutput(1); out1.resize(512); [/source]
  7. robiwan

    Exposing arrays

    Hi, just starting to delve into AngelScript and I have a question: I need to expose several arrays in my C++ code to the scripting engine (with type and length), which is the best way to do that ? TIA /R Btw. Is there a good search feature here on gamedev (that does not use google) ?
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!