🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Retrieving an array of strings from a dictionary

Started by
5 comments, last by WitchLord 9 years, 10 months ago

I have a dictionary element of type array<string>:


dictionary d = { {'arr', array<string>(1, "something")} };

I now want to retrieve this entry into a local variable:


//array<string> arr = cast<array<string>>(d['arr']);	// works

//array<string>@ arr = null;
//bool found = d.get('arr', @arr);			// works (found == true)

//array<string> arr;
//bool found = d.get('arr', arr);			// doesn't work (found == false)

array<string> arr;
bool found = d.get('arr', @arr);			// doesn't work (found == true, but arr variable is empty)

Could you help me understand why the 3rd and 4th options don't work?

On an unrelated note, if I can directly instantiate an array of ints like so:


array<int> arr = {,3,4,}

Why does this not work?


array<string> arr = {"a", "b", "c"}; // "Argument cannot be assigned. Output will be discarded" compiler warning
Advertisement

It looks like this might be caused by a couple of bugs in the add-ons.

I'll investigate it.

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


Why does this not work?

array arr = {"a", "b", "c"}; // "Argument cannot be assigned. Output will be discarded" compiler warning

Are you using the default add-ons scriptarray.cpp and scriptstdstring.cpp? Or custom versions?

The following code works as expected:

{
  engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
  engine->SetMessageCallback(asMETHOD(COutStream, Callback), &out, asCALL_THISCALL);
 
  RegisterStdString(engine);
  RegisterScriptArray(engine, false);
 
  asIScriptModule *mod = engine->GetModule("test", asGM_ALWAYS_CREATE);
  mod->AddScriptSection("test",
    "array<string> arr = {'a', 'b', 'c'}; \n");
  r = mod->Build();
  if( r < 0 )
    TEST_FAILED;
 
  engine->Release();
}

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


Are you using the default add-ons scriptarray.cpp and scriptstdstring.cpp? Or custom versions?

Yes, default - no modifications to string or array (or anything, for that matter, besides a local patch to support 'handle of handle' syntax for backwards compatibility). rev 1996 on 32-bit linux.

Could it be an edge-case with single-characters? I'll try more situations soon.


//array arr = cast>(d['arr']); // works

//array@ arr = null;
//bool found = d.get('arr', @arr); // works (found == true)

//array arr;
//bool found = d.get('arr', arr); // doesn't work (found == false)

array arr;
bool found = d.get('arr', @arr); // doesn't work (found == true, but arr variable is empty)
Could you help me understand why the 3rd and 4th options don't work?

The 3rd option didn't work because the dictionary didn't have support for doing a value assignment with the stored handle (fixed in revision 1997). Also, I'd like to point out that the returned boolean from the get() method is not really indicating if the entry was found, but rather if the value could be retrieved, i.e. if the stored entry is compatible with the requested type. (I've updated the example in the documentation to better reflect this too).

The 4th option doesn't work because the local variable arr is not a handle and cannot be reassigned to refer to the contained handle in the dictionary. This is indicated by the compiler with the warning "Argument cannot be assigned. Output will be discarded". The get() method still returns true though, because it has no idea that the calling function will discard the output after the call. (the warning should probably have been an error instead, I'll look into that)

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

Regarding not being able to create an array of strings in-place via initialization lists, I think I was getting confused with the following situation:


//array<string> arr = {"abc", "def"};			// OK
//array<string> arr = {"a", "b", "c"};			// OK
array<string> arr = {"something"};			// OK

//dictionary d = { {'arr', array<string>(1, "something")} };	// OK
//dictionary d = { {'arr', "something"} };			// OK
//dictionary d = { {'arr', {"something"}} };			// Initialization lists cannot be used with '?'
								// Reference is read-only
dictionary d = { {'arr', {"abc", "def"}} };			// same as above

Is there any way to insert an array of strings as a dictionary element via initialization lists?

Thank you.

By using anonymous objects like this:

dictionary d = { {'arr', array<string> = {'abc', 'def'}} };

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

This topic is closed to new replies.

Advertisement