I do not really know how I would start to program this, the reason why I want to use a string system rather than a list system is because a list system takes up too much ram on my pc, lagging the overall game. (I have 16 gb ram)
This is how I would like it too work: say I have a string labeled "1\n", "2\n", "3\n", "4\n", "5\n", "6\n" then if the index of the string I wanted to get was = 0. I would get a vector3 = 1, 2, 3 and if the index was 1 it would be 4, 5,6.
If you know any other ways of going about this problem, it would be highly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Container : MonoBehaviour
{
public string Bchanged;
void GetBlockbyIndex(int index)
{
//Get what block it is based on its index
/*Example:
* index = 0;
* Bchanged = "1\n", "2\n", "3\n", "4\n", "5\n", "6\n";
* Vector3 answer = new vector3(1, 2, 3);
*/
Vector3 anwser = Bchanged[index];
}
void AddBlock(Vector3 block)
{
//Add block to string list
}
}
CodePudding user response:
Encoding the data in a string will use more ram than the Vector3. A Char is 16 bits. 16 * (3 for single digit 2 delimiters) = 80 bits. A Vector3 is 3 floats = 96 bits, and can store more than 0-9.
Lists add a little overhead above an array, but gives you the ability to dynamically add space.
If you know how many items will be added use the initial capacity parameter:
List<Vector3> Blocks = new List<Vector3>(100);
This will prevent the resizing reallocation of the underlying array.
You can save one third of the space if your values are all positive integers and the range of X,Y,Z = (0,0,0) - (2047,2047,1023)
You can store the Vector3 in a packed unsigned integer (32bits).
List<UInt32> Blocks = new List<UInt32>(); // insert max count if known
const int SCALE = 1;
// for Grid alignment(16 makes the stored values 1, 2,... 2047 -> 16, 32,... 32752)
Vector3 GetBlockbyIndex(int index)
{
//Get what block it is based on its index
return new Vector3(Blocks[index] & 0x7FF,
(Blocks[index] & 0x3FFFFF) >> 11,
Blocks[index] >>22) * SCALE;
}
void AddBlock(Vector3 block)
{
//Add block to list
// Watch the input range, (2048,0,0) becomes (0,1,0)
block /= SCALE;
Blocks.Add((uint)block.X | (uint)block.Y << 11 | (uint)block.Z << 22);
}