how can i register object methods with a parameter " const char * "

Started by
17 comments, last by WitchLord 3 years, 3 months ago

hi.

//in my c++ code
class classA
{
void SetName(const char *name);
}

register object methods
engine->RegisterObjectMethod("classA", "void SetName(const string &in)", asMETHOD(classA, SetName), asCALL_THISCALL);

it 's fail. how can i do?

Advertisement

Since const char * is not a safe type I recommend you wrap this class method to take a string object instead.

class classA
{
void SetName(const char *name);
}
// wrapper (without modifying the original class)
void classA_SetName(const std::string &name, classA &thisObj)
{
  thisObj.SetName(name.c_str());
}
// Register the wrapper function instead of the real method
engine->RegisterObjectMethod("classA", "void SetName(const string &in)", asFUNCTION(classA_SetName), asCALL_CDECL_OBJLAST);

Of course, if you absolutely want to use the char* directly instead of the string type then you can register the char* as a type with the script engine. Example:

// Register const char* as a type
engine->RegisterObjectType("const_char_ptr", sizeof(const char*), asOBJ_VALUE|asOBJ_POD|asOBJ_APP_PRIMITIVE);

// Register the method using the type
engine->RegisterObjectMethod("classA", "void SetName(const_char_ptr)", asMETHOD(classA,SetName), asCALL_THISCALL);

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

//There seems to be some error.
class classA
{
public:
	void SetName(const wchar_t *s)
	{
		m_str = s;
 //breakpoint here, some error codes in front of my real text
	}
	const wchar_t *GetName()
	{
		return m_str.c_str();
	}
	std::wstring m_str;
};
//as code
classA a;
a.SetName("aaa");
//register 
r = engine->SetEngineProperty(asEP_STRING_ENCODING, 1); assert( r >= 0 );
............
#ifdef _UNICODE
	engine->RegisterObjectType("const_char_ptr", sizeof(const wchar_t *), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_PRIMITIVE);
#else
	engine->RegisterObjectType("const_char_ptr", sizeof(const char *), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_PRIMITIVE);
#endif
...............
	r = engine->RegisterObjectType("classA", 0, asOBJ_REF|asOBJ_NOCOUNT); 
	r = engine->RegisterObjectBehaviour("classA", asBEHAVE_FACTORY, "classA@ f()", asFUNCTION(classA_Ref_Factory), asCALL_CDECL);  assert( r >= 0 );
	
	r = engine->RegisterObjectMethod("classA", "void SetName(const string &in)", asMETHOD(classA, SetName), asCALL_THISCALL);  assert( r >= 0 );

i can't register like this
r = engine->RegisterObjectMethod("classA", "void SetName(const_char_ptr)", asMETHOD(classA, SetName), asCALL_THISCALL); assert( r >= 0 );

error message : No matching signatures to ‘classA:SetName(string)’

i have register mine custom string, not std::string , I'm not sure if my string object caused the problem.
Thank you.

If you want to use both string and const_char_ptr then you need to register conversion behaviours/functions too,

You can do it the same way as in C++. i.e. register the c_str() metod on your string type, and a constructor on the string type that takes a const_char_ptr.

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

I still haven't dealt with it well. Can you give me an example? I want to write scripts just like writing C + + programs. There are many functions involving const char * in my C + + classes, which will be very troublesome and uncontrollable for subsequent maintenance.

Well if that is what you want then just as in C++ you need a way to convert between the string type and const char *.

You can do this with normal functions if you want the script writer to do the conversion explicitly. But if you want the conversion to be done implicitly by the compiler, you need to follow the syntax for type-conversion operator overloads.

http://www.angelcode.com/angelscript/sdk/docs/manual/doc_script_class_ops.html#doc_script_class_conv

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

yes, it's working fine now.

//register const_char_ptr
r = engine->RegisterObjectType("const_char_ptr", sizeof(const char *), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_PRIMITIVE); assert( r >= 0 );

//register string construct with const_char_ptr and conversion
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(const_char_ptr &in)", asFUNCTION(String_Construct2), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "const_char_ptr opImplConv() const", asFUNCTION(StringToLPCTSTR), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "const_char_ptr opConv() const", asFUNCTION(StringToLPCTSTR), asCALL_CDECL_OBJLAST); assert( r >= 0 );

thank you.

It is great!!!
There is a new question.
In general, how to handle pointers and references.
class ClassA
{
void SetMyClass( CMyClass *pClass) {}
CMyClass *GetMyClassPR() {}
CMyClass &GetMyClass {}
}
do something, GetMyClassPR()→Fun();
Do i need to register “CMyClass *” and “CMyClass &” ?

Generally speaking in C++ both pointers and references are the same, it is just a syntax difference, but in memory they are both represented the same way.

So, no you don't need to register both methods. Unless there is an implementation difference between them, e.g. if the function that returns a pointer can return null in some conditions, but the other cannot, then you'll need to decide if you want to expose both to the scripts.

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

ok. thanks. Witchlord.
How to get a pointer from a script and call other functions with the pointer as a parameter.

//c++ code
class classA
{
void funA() {}
}
class classB
{
classA *GetPointerA() { ........}
}
void DrawText(classA *a)
{
}

//register GetPointerA()

r = engine->RegisterObjectMethod("classB", "classA @GetPointerA()", asMETHODPR(classB, GetPointerA, (), classA *), asCALL_THISCALL);

//register DrawText(classA *a)

r = engine->RegisterGlobalFunction("void DrawText(classA @a)", asFUNCTIONPR(DrawText, classA *, void), asCALL_CDECL);

//script code

classB b;

classA @a = b.GetPointerA();

if(a != null)

{

a.funA(); //ok

DrawText(a); //error

DrawText(@a); //error

DrawText(&a); //error

}

I really hope angel scripts can support pointers.

Recently, I'm integrating scripts in my directui.

As mentioned above, when I write a script, it's like writing C++ code.

This topic is closed to new replies.

Advertisement