Advertisement

How to register template specializations for the array type

Started by March 27, 2021 01:56 PM
7 comments, last by LeonMrBonnie 3 years, 8 months ago

Hello,

the AngelScript docs tell the embedder to write template specializations for the array type, so it doesn't have to do these at runtime.

I haven't understood how to properly define template specializations though.

I have tried it like this, but it does not work:

engine->RegisterObjectType("array<int>", 0, asOBJ_REF | asOBJ_GC);
engine->RegisterObjectBehaviour("array<int>", asBEHAVE_FACTORY, "array<int>@ f(int&in)", asFUNCTIONPR(CScriptArray::Create, (asITypeInfo*), CScriptArray*), asCALL_CDECL);
engine->RegisterObjectBehaviour("array<int>", asBEHAVE_FACTORY, "array<int>@ f(int&in, uint length) explicit", asFUNCTIONPR(CScriptArray::Create, (asITypeInfo*, asUINT), CScriptArray*), asCALL_CDECL);
engine->RegisterObjectBehaviour("array<int>", asBEHAVE_FACTORY, "array<int>@ f(int&in, uint length, const T &in value)", asFUNCTIONPR(CScriptArray::Create, (asITypeInfo*, asUINT, void *), CScriptArray*), asCALL_CDECL);

engine->RegisterObjectBehaviour("array<int>", asBEHAVE_LIST_FACTORY, "array<int>@ f(int&in type, int&in list) {repeat int}", asFUNCTIONPR(CScriptArray::Create, (asITypeInfo*, void*), CScriptArray*), asCALL_CDECL);

None

What error do you get?

Did you register the array<class T> template first?

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

Advertisement

WitchLord said:
Did you register the array template first?

Yes, the array template addon is registered before.

WitchLord said:
What error do you get?

[11:49:00][Warning]  (0, 0): Failed in call to function 'RegisterObjectType' with 'array<int>' 
(Code: asINVALID_NAME, -8)
[11:49:00][Warning]  (0, 0): Failed in call to function 'RegisterObjectBehaviour' with 'array<int>' and 'array<int>@ f(int&in)' (Code: asINVALID_TYPE, -12)
[11:49:00][Warning]  (0, 0): Failed in call to function 'RegisterObjectBehaviour' with 'array<int>' and 'array<int>@ f(int&in, uint length) explicit' (Code: asINVALID_TYPE, -12)
[11:49:00][Warning]  (0, 0): Failed in call to function 'RegisterObjectBehaviour' with 'array<int>' and 'array<int>@ f(int&in, uint length, const T &in value)' (Code: asINVALID_TYPE, -12)   
[11:49:00][Warning]  (0, 0): Failed in call to function 'RegisterObjectBehaviour' with 'array<int>' and 'array<int>@ f(int&in type, int&in list) {repeat int}' (Code: asINVALID_TYPE, -12)

None

I'm not sure what it is causing this for you. For me it works:

		engine = asCreateScriptEngine();
		engine->SetMessageCallback(asMETHOD(CBufferedOutStream, Callback), &bout, asCALL_THISCALL);
		bout.buffer = "";

		RegisterScriptArray(engine, false);

		r = engine->RegisterObjectType("array<int>", 0, asOBJ_REF);
		if (r < 0)
			TEST_FAILED;

		if (bout.buffer != "")
		{
			PRINTF("%s", bout.buffer.c_str());
			TEST_FAILED;
		}

		engine->ShutDownAndRelease();

Can you debug into the library to check why it is returning an error for you?

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

In as_scriptengine.cpp on line 1848, I added this:

if( token != ttIdentifier || typeName.GetLength() != tokenLen )
{
    std::cout << "Name is reserved keyword: " << token << std::endl;
    std::cout << typeName.AddressOf() << std::endl;
    std::cout << tokenLen << std::endl;
    return ConfigError(asINVALID_NAME, "RegisterObjectType", name, 0);
}

And this is the output I get when I add engine->RegisterObjectType("array<int>", 0, asOBJ_REF);

Name is reserved keyword: 5
array<int>
5
[21:35:19][Warning]  (0, 0): Failed in call to function 'RegisterObjectType' with 'array<int>' 
(Code: asINVALID_NAME, -8)

None

It only gets to that condition because for some reason it doesn't recognize the already registered array template.

Are you certain you have called RegisterScriptArray before RegisterObjectType("array<int>",…)? Have you by any chance defined a different namespace with SetDefaultNamespace?

Can you show more of your code for registering the application interface with the engine before you get to RegisterObjectType("array<int>",…)?

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

Advertisement

WitchLord said:
Have you by any chance defined a different namespace with SetDefaultNamespace?

Okay, that was the problem, moving it before my code that calls SetDefaultNamespace fixed it. I thought when you set the default namespace that it would also still search in the global namespace.

None

// nvm, the forum lagged

None

This topic is closed to new replies.

Advertisement