Sign in to follow this  
iraxef

Retrieving an array of strings from a dictionary

Recommended Posts

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
Edited by iraxef

Share this post


Link to post
Share on other sites


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();
}

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites


//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)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this