Storing movement (arrows) then do it (start).
Hello guys,
I hope everyone is well, Can somebody please help me with some C# pattern, I searched through all the internet and I just can't find something, something right.
First of all, I'm new to Unity.. Just messing around and still learning but I'm in love with it from the first sight. I'm mad because I didn't 'discovered' it earlier... Meh.
To the point:
I have a Canvas / Panel with 5 buttons : Left, Right, Forward, Back and Start. I also have a class with all the 5 methods: moveLeft(), moveForward() .. you get the point. The thing is, when the user will mess around with the buttons: L,B,L,F,L,R,R,L for example and then he/she will trigger the 'Start' button, my cube should move exactly in the order captured. (Left, then back, then left, then forward etc)
I've tried with delegate list, with some arrays , encapsulations, command pattern, I just can't find it out :(.
Can you please show me 'za uei' ? I would appreciate any suggestions.
P.S.: I'm trying to make a 'movement planning puzzle game'.
Best regards,
Sergiu
Hello again guys. Great day!
I just don't know how to Edit my question, don't blame me :).
I tried both of the answers yesterday and @bakir-omarov 's one was the best !
Thank you very much,
Have a wonderful day ahead.
Answer by bakir-omarov · Jun 12, 2018 at 03:45 PM
But if you want "HARDCORE BABY OUU YEAAH!!!" (in your case it is not efficient) . Use delegate void . Example:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class delete : MonoBehaviour
{
delegate void SavedMoves(); // delegate VOID
List<SavedMoves> savedMoves = new List<SavedMoves>();
public bool levelStarted;
private float moveTime = 1f;
private void Start()
{
savedMoves.Clear();
}
public void SaveLeft() // on click button will call this
{
if(!levelStarted)
savedMoves.Add(MoveLeft);
}
public void SaveRight() // on click button will call this
{
if (!levelStarted)
savedMoves.Add(MoveRight);
}
public void SaveBack() // on click button will call this
{
if (!levelStarted)
savedMoves.Add(MoveBack);
}
public void StartGame() // on click button will call this
{
if (!levelStarted && savedMoves.Count > 0)
{
levelStarted = true;
StartCoroutine(PlayLevel());
}
else
return;
}
private IEnumerator PlayLevel()
{
if (savedMoves.Count > 0) {
for (int i = 0; i < savedMoves.Count; i++)
{
savedMoves[i](); // call your voids like this
yield return new WaitForSeconds(moveTime); // time between each move
}
savedMoves.Clear();
levelStarted = false;
}
}
//confirmed working
void MoveLeft()
{
Debug.Log("i am moving left!");
}
void MoveRight()
{
Debug.Log("i am moving right!");
}
void MoveBack()
{
Debug.Log("i am moving back!");
}
}
Wow, you're energy is awesome. Thank you! I did exactly 85% the same but without the bool check. This is so easy. I will come back with feedback tomorrow on how things worked out. $$anonymous$$y instinct is telling me that your answer is more suitable for my question. Thanks mate !
Answer by tormentoarmagedoom · Jun 12, 2018 at 03:30 PM
Good day.
Is the movement continous or Discrete? (i mean map is tiled or is movement*time)
I think is very simple, you only need to create a list for all the orders, for examples with strings, and use the add function to add all clicks.
So at the endo of "user planning phase" you will have a list with all the movements in the order the user said.
You only need now to read each element 1 by 1 and execute it
foreach (string Order in OrderList)
{
switch (Order)
{
case left:
// Move left
break;
case "right":
//Move right
break;
....
}
}
Something like this should work with no problems..
Bye!
Hello Sir, thank you for the good feedback. The movement is movement*Time.deltaTime as my cube will move on the 3D space to avoid some another cubes. (If the user is planning the movement well).
I'm currently at work, but I will try it and tomorrow I will be back with feedback!
Your answer
Follow this Question
Related Questions
How to move the player only 1 tile per buttonPress? 2 Answers
Magnetic pieces being attached to each other and moving together ??? 0 Answers
Collecting puzzle pieces in my platformer game 0 Answers
how can i move an object with constant movement ? 1 Answer
One of meshes keeps following the camera in scene mode for some reason? 1 Answer