I tried but I get stuck.
Once the function where the .find was executed is done (stack is cleaned) my program crashes.
This is what I currently have:
static void* ArrayFind(CScriptArray* array, asIScriptFunction* callback)
{
asIScriptEngine* engine = AngelScriptRuntime::Instance().GetEngine();
asUINT size = array->GetSize();
int type = array->GetElementTypeId();
asIScriptContext* context = asGetActiveContext();
void* result = nullptr;
for(asUINT i = 0; i < size; i++)
{
auto item = array->At(i);
context->PushState();
int r = context->Prepare(callback);
CHECK_AS_RETURN("Find callback prepare", r, NULL);
r = context->SetArgAddress(0, item);
CHECK_AS_RETURN("Find callback set item", r, NULL);
r = context->SetArgDWord(1, i);
CHECK_AS_RETURN("Find callback set index", r, NULL);
r = context->Execute();
CHECK_AS_RETURN("Find callback execute", r, NULL);
bool callResult = context->GetReturnByte();
context->Unprepare();
context->PopState();
if(callResult)
{
result = item;
break;
}
}
return result;
}
engine->RegisterFuncdef("bool array<T>::findCallback(const T&in if_handle_then_const item, const uint index)");
engine->RegisterObjectMethod("array<T>", "T& find(const findCallback&in callback) const", asFunctionPtr(ArrayFind), asCALL_CDECL_OBJFIRST);
I tried messing around with it but didn't fix it.