Difference between revisions of "StringToVector"

From Dragon Age Toolset Wiki
Jump to: navigation, search
m (1 revision: Importing auto-generated function articles)
(Added Remarks and and alternative function)
Line 1: Line 1:
{{Generated}}
 
 
{{dafunction
 
{{dafunction
|name=StringToVector
+
|name         = StringToVector
|brief=Converts a string to a vector.
+
|brief       = Converts a string to a vector.
|param1type=string
+
|param1type   = string
|param1name=sString
+
|param1name   = sString
|param1desc=The string to convert
+
|param1desc   = The string to convert
|returntype=vector
+
|returntype   = vector
|returndesc=Returns the vector contained in the string. Returns an empty vector on error.
+
|returndesc   = Returns the vector contained in the string. Returns an empty vector on error.
|sourcefile=script.ldf
+
|sourcefile   = script.ldf
|sourcemodule=
+
|sourcemodule =
 
}}
 
}}
  
Line 16: Line 15:
 
Returns a new vector from the string sString. The format must be "x y z" where x, y and z are floating point numbers.
 
Returns a new vector from the string sString. The format must be "x y z" where x, y and z are floating point numbers.
  
<!-- == Remarks == -->
+
== Remarks ==
 
<!-- This section contains additional comments, observations and known issues. -->
 
<!-- This section contains additional comments, observations and known issues. -->
 +
In testing this function always returned an empty vector.
  
 +
If you require the ability to convert a string to a vector you can use the following <code>Wiki_StringToVector</code> function:
 +
<dascript>
 +
/**
 +
* Attempts to convert a string into a vector. The string must contain all three elements: x, y and
 +
* z. The elements can be separated by a space and/or a comma. The elements can have the "f" suffix.
 +
* If the string does not adhere to the supported formats it will result in a malformed vector.
 +
*
 +
* @brief Converts a string to a vector.
 +
* @param sVector - The string to convert.
 +
* @returns The vector contained in the string.
 +
**/
 +
vector Wiki_StringToVector(string sVector);
 +
vector Wiki_StringToVector(string sVector)
 +
{   
 +
    // find the first delimiter: try comma then space
 +
    int nIndex1 = FindSubString(sVector, ",", 0);
 +
    if(nIndex1 == -1) { nIndex1 = FindSubString(sVector, " ", 0); }
 +
 +
    // extract the first element and consume the delimiter
 +
    float fX = StringToFloat(SubString(sVector, 0, nIndex1)); 
 +
    nIndex1++;
 +
 +
    // find the second delimiter: try comma then space
 +
    int nIndex2 = FindSubString(sVector, ",", nIndex1);
 +
    if(nIndex2 == -1) { nIndex2 = FindSubString(sVector, " ", nIndex1); }
 +
 +
    // extract the second element and consume the delimiter
 +
    float fY = StringToFloat(SubString(sVector, nIndex1, nIndex2 - nIndex1));
 +
    nIndex2++;
 +
 +
    // extract the final element
 +
    float fZ = StringToFloat(SubString(sVector, nIndex2, GetStringLength(sVector) - nIndex2));
 +
 +
    // construct and return the vector
 +
    return Vector(fX, fY, fZ);
 +
}
 +
 +
void main()
 +
{
 +
    // Output: Script    1.100000 2.200000 3.300000
 +
    vector vTest1 = Wiki_StringToVector("1.1 2.2 3.3");
 +
    PrintToLog(VectorToString(vTest1));
 +
 +
    // Output: Script    2.200000 3.300000 4.400000
 +
    vector vTest2 = Wiki_StringToVector("2.2,3.3,4.4");
 +
    PrintToLog(VectorToString(vTest2));
 +
 +
    // Output: Script    3.300000 4.400000 5.500000
 +
    vector vTest3 = Wiki_StringToVector("3.3, 4.4, 5.5");
 +
    PrintToLog(VectorToString(vTest3));
 +
 +
    // Output: Script    4.400000 5.500000 6.600000
 +
    vector vTest4 = Wiki_StringToVector("4.4f 5.5f 6.6f");
 +
    PrintToLog(VectorToString(vTest4));
 +
}
 +
</dascript>
 
<!-- == Examples == -->
 
<!-- == Examples == -->
 
<!-- This section contains examples transcluded from the snippet library. -->
 
<!-- This section contains examples transcluded from the snippet library. -->
 
 
== See also ==
 
== See also ==
 
<!-- This section contains links to articles, functions or constant groups. -->
 
<!-- This section contains links to articles, functions or constant groups. -->
 
[[VectorToString]], [[StringToInt]], [[StringToFloat]]
 
[[VectorToString]], [[StringToInt]], [[StringToFloat]]
[[Category: Conversion functions]]
+
[[Category: Conversion functions]][[Category: Broken functions]]

Revision as of 00:33, 30 January 2013

Converts a string to a vector.

vector StringToVector(
string sString
);
Parameters:
sString
The string to convert
Returns:

Returns the vector contained in the string. Returns an empty vector on error.

Source:

script.ldf

Description

Returns a new vector from the string sString. The format must be "x y z" where x, y and z are floating point numbers.

Remarks

In testing this function always returned an empty vector.

If you require the ability to convert a string to a vector you can use the following Wiki_StringToVector function:

/** 
* Attempts to convert a string into a vector. The string must contain all three elements: x, y and
* z. The elements can be separated by a space and/or a comma. The elements can have the "f" suffix.
* If the string does not adhere to the supported formats it will result in a malformed vector.
*
* @brief Converts a string to a vector.
* @param sVector - The string to convert.
* @returns The vector contained in the string.
**/
vector Wiki_StringToVector(string sVector);
vector Wiki_StringToVector(string sVector)
{    
    // find the first delimiter: try comma then space
    int nIndex1 = FindSubString(sVector, ",", 0);
    if(nIndex1 == -1) { nIndex1 = FindSubString(sVector, " ", 0); }
 
    // extract the first element and consume the delimiter
    float fX = StringToFloat(SubString(sVector, 0, nIndex1));  
    nIndex1++;
 
    // find the second delimiter: try comma then space
    int nIndex2 = FindSubString(sVector, ",", nIndex1);
    if(nIndex2 == -1) { nIndex2 = FindSubString(sVector, " ", nIndex1); }
 
    // extract the second element and consume the delimiter
    float fY = StringToFloat(SubString(sVector, nIndex1, nIndex2 - nIndex1));
    nIndex2++;
 
    // extract the final element
    float fZ = StringToFloat(SubString(sVector, nIndex2, GetStringLength(sVector) - nIndex2));
 
    // construct and return the vector
    return Vector(fX, fY, fZ);
}
 
void main()
{
    // Output: Script    1.100000 2.200000 3.300000
    vector vTest1 = Wiki_StringToVector("1.1 2.2 3.3");
    PrintToLog(VectorToString(vTest1));
 
    // Output: Script    2.200000 3.300000 4.400000
    vector vTest2 = Wiki_StringToVector("2.2,3.3,4.4");
    PrintToLog(VectorToString(vTest2));
 
    // Output: Script    3.300000 4.400000 5.500000
    vector vTest3 = Wiki_StringToVector("3.3, 4.4, 5.5");
    PrintToLog(VectorToString(vTest3));
 
    // Output: Script    4.400000 5.500000 6.600000
    vector vTest4 = Wiki_StringToVector("4.4f 5.5f 6.6f");
    PrintToLog(VectorToString(vTest4));
}

See also

VectorToString, StringToInt, StringToFloat