How to return an array of any

Started by
9 comments, last by WitchLord 3 years, 2 months ago

Hello,

in my current project I have to give the ability for clients to communicate with the server and the other way around, they do this by sending events to each other. When compiling the script I don't know what events will be sent or received, or how many or which arguments they will have.

So I need some way to call a handler function with an array<any> where I store the arguments so in the script the values can be retrieved.

How can I achieve this? I am using the standard array and any add-on.

None

Advertisement

What language ?

AngelScript / C++

None

Take a look at the StringSplit function in scriptstdstring_utils.cpp. It takes a string and returns an array of them. The registration is pretty straight forward once you see it.

How to return an array?

In the following example, the method returns an array of integer type.

  1. import java.util.Arrays;
  2. public class ReturnArrayExample1.
  3. {
  4. public static void main(String args[])
  5. {
  6. int[] a=numbers(); //obtain the array.
  7. for (int i = 0; i < a.length; i++) //for loop to print the array.
  8. System.out.print( a[i]+ " ");@LeonMrBonnie

I am looking for a solution with AngelScript.

None

The manual shows how to create a script array from C++ and return it to the script.

http://www.angelcode.com/angelscript/sdk/docs/manual/doc_addon_array.html#doc_addon_array_4

It also shows how to work with the any type from C++ to set its content.

http://www.angelcode.com/angelscript/sdk/docs/manual/doc_addon_any.html#doc_addon_any_3

I think with these two you should be able to implement your function that return an array of any.

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

Thanks for the response.

I am struggling with storing primitives in my any.

converted.second is the pointer to my primitive value (e.g. int64_t*) and converted.first is the type id of (e.g. 5)

When I call this function, it crashes.

any->Store((void*)converted.second, converted.first);

None

I'm not sure what you're doing wrong since you're not showing me any code, but the following works:

	{
		engine = asCreateScriptEngine();
		engine->SetMessageCallback(asMETHOD(COutStream, Callback), &out, asCALL_THISCALL);
		RegisterScriptAny(engine);
		CScriptAny* any = new CScriptAny(engine);

		asINT64 someValue = 32;
		any->Store(&someValue, asTYPEID_INT64);

		someValue = 0;
		any->Retrieve(&someValue, asTYPEID_INT64);

		if (someValue != 32)
			TEST_FAILED;
		any->Release();

		engine->ShutDownAndRelease();
	}

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

This topic is closed to new replies.

Advertisement