- Home /
Other
How do I swap two gameObjects order in a list?
So I have this list filled with "instructions" for tile-based movement, but currently it is generated randomly and you cannot swap the positions of said instructions so the gameObjects just go around and move randomly. I'd like to be able to move those instructions around in order for the player to be able to move those gameObjects in their desired order.
This is how the movement "cards" are given and shuffled:
void DarPeçasNecessarias() //Da exatamente as peças necessárias para o jogador terminar o nivel
{
if (direçao.x > 0)
{
numRight = (int) direçao.x;
for (int i = 0; i < numRight; i++)
{
Peçass.Add("Right");
}
if (direçao.y > 0)
{
numUp = (int) direçao.y;
for (int i = numRight; i < numRight + numUp; i++)
{
Peçass.Add("Up");
}
}
else if (direçao.y < 0)
{
numDwn = (int) - direçao.y;
for (int i = numRight; i < numRight + numDwn; i++)
{
Peçass.Add("Down");
}
}
else { numDwn = 0; numUp = 0; }
}
else if (direçao.x < 0)
{
numLft = (int) - direçao.x;
for (int i = 0; i < numLft; i++)
{
Peçass.Add("Left");
}
if (direçao.y > 0)
{
numUp = (int) direçao.y;
for (int i = numLft; i < numLft + numUp; i++)
{
Peçass.Add("Up");
}
}
else if (direçao.y < 0)
{
numDwn = (int) - direçao.y;
for (int i = numLft; i < numLft + numDwn; i++)
{
Peçass.Add("Down");
}
}
else { numDwn = 0; numUp = 0; }
}
else { numLft = 0; numRight = 0; }
//-----------------
numPeçasNecessarias = numRight + numLft + numUp + numDwn;
DarPeçasExtra();
}
void DarPeçasExtra() //Da o resto das peças
{
for (int i = numPeçasNecessarias; i < NumPeças; i++)
{
int nrg = Random.Range(1,5);
//Debug.Log(nrg);
if (nrg == 1)
{
Peçass.Add("Up");
numUp++;
}
else if (nrg == 2)
{
Peçass.Add("Down");
numDwn++;
}
else if (nrg == 3)
{
Peçass.Add("Left");
numLft++;
}
else
{
Peçass.Add("Right");
numRight++;
}
}
ShuffleArray();
}
void ShuffleArray() //Baralha o array
{
string Temp = "Temp";
for (int i = 0; i < Peçass.Count; i++)
{
int rnd = Random.Range(0, Peçass.Count);
Temp = Peçass[rnd];
Peçass[rnd] = Peçass[i];
Peçass[i] = Temp;
}
//FillImages();
}
I'd like to know where do I go from here, currently there are temporary visual indicators that show the order of the cards and movement, but I'd like to move them around so that it can move freely, eg:
Any help would be greatly appreciated! Thank you!
These visual indicators consist of buttons. The FillImages() method just changes their sprite according to the list instruction
The method DarPeçasNecessarias() basically gives the player the exact amount of cards and instructions to finish the level.
The method DarPeçasExtra() just gives random other "cards" to fill the list and the Hotbar.
Sorry for not specifying it in the post itself!
Answer by Llama_w_2Ls · Feb 01, 2021 at 01:54 PM
To swap items in an array or list, create a temporary value that stores variable a, set a to b, and set b to that temporary value. For example:
void Swap(GameObject a, GameObject b)
{
GameObject temp = a;
a = b;
b = temp;
}
@DiasPTW Is that what you needed? Your question sounds a little complicated.
That is more or less what I want, my real question is basically how do I detect which 2 buttons/gameObjects I clicked and then change their orders in the list, sorry for not explaining it well.
Answer by Wolfzoon · Feb 01, 2021 at 02:27 PM
It's not so clear to me what your question is. You obviously know how to swap List<T>
items, as you do the same in your ShuffleArray
function. Nor do I really get what DarPeçasNecessarias
does. Is it called on Clicking the gameObjects?
I see a bunch of "instructions" in your screenshot, these are each GameObjects with an InstructionScript
attached I'd guess? But do they also know which position they have in the "List", I assume they are executed left-to-right, top-to-bottom?
In that case you would want these gameObjects to swap positions visually, and in the way you keep track of them, e.g.
Vector3 posTemp = objectOne.transform.position;
objectOne.transform.position = objectTwo.transform.position;
objectTwo.transform.position = posTemp;
var scriptOne = objectOne.GetComponent<InstructionScript>();
var scriptTwo = objectTwo.GetComponent<InstructionScript>()
Swap(scriptOne.Number, scriptTwo.Number);
@DiasPTW Do you need something like this?
the DarPeçasNecessarias
is called in the start function, I then added a comment to the Original post kinda explaining what those functions do, sorry for not including it in the post itself.
I don't really want to change the positions itself, I really only want to change the positions in the list because in the Update
function ill update visually. I'm just a little confused because I can't really figure out how I can detect which 2 buttons/gameObjects i clicked and then see their place on the list and then change their positions on said. Sorry for the poor explanation originally