StringToVector
From Dragon Age Toolset Wiki
Revision as of 12:34, 30 January 2013 by Sunjammer (Talk | contribs) (Tweaked function's description to avoid confusion)
Converts a string to a vector.
- 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 comma and/or a space. 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)); }