Hey
I tried to implement a short message queue where I can add messages via a button (1 click = 1 msg to add). These messages are stored in smsQueue of type Queue.
In Start() I will call my forever looping coroutine displayShortMessageLoop.
/** add sms to the list of sms to be displayed */
public void addShortMessage(ShortMessage sms){
smsQueue.Enqueue(sms);
}
/** while true loop that displays a short message from smsQueue that disappears after a few seconds */
private IEnumerator displayShortMessageLoop(){
ShortMessage sms;
while(true){
if(smsQueue.Count == 0){
yield return new WaitForSeconds(0.1f)
continue; // check again in 0.1s if no message present
} else {
if(lastSMSSpawnTime + 2.9f >= Time.time ) yield return new WaitForSeconds((lastSMSSpawnTime + 2.9f) - Time.time);
Debug.Log ("smsQueue " + smsQueue.Count);
sms = smsQueue.Dequeue();
// do a lot of stuff, set text and image
smsGO.GetComponent<MoveOverTime>().StartCoroutine("begin");
}
}
}
MoveOverTime looks like this
public override void Start () {
base.Start();
originalPos = gameObject.transform.position;
}
public IEnumerator begin() {
foreach(MoveOverTimeContainer cont in moveDirections){
yield return new WaitForSeconds(cont.waitAtStart);
StartCoroutine(moveWithinTime(cont.target, cont.timeToTarget));
yield return new WaitForSeconds(cont.timeToTarget); // wait the time until processing the next cont
}
yield return null;
}
/** move to target position within timetoMove seconds */
public IEnumerator moveWithinTime(Vector3 target, float timeToMove){
Vector3 currentPos = gameObject.transform.position;
target += originalPos;
float t = 0f;
while(t < 1) {
t += Time.deltaTime / timeToMove;
gameObject.transform.position = Vector3.Lerp(currentPos, target, t);
yield return null;
}
}
MoveOverTimeContainer:
/** initial waiting time */
public float waitAtStart = 0f;
/** move to this point */
public Vector3 target;
/** within this timeframe */
public float timeToTarget = 2f;
Now, what I want to achieve is that no matter when the message is added, the following should happen:
If the message has not fully reached its 1st target position (because that is what happens during begin()), wait until that happens. If the message is fully on the screen, make sure it is displayed for at least the waiting delay of the second cont object (2s). From that point on, it is allowed to override the movement so that a message that has being added between second 0 and second timeToMove+2s will stay at the first target position so that the message field stays where it is while the text is being changed. I know this is confusing to I tried to visualize this:
I have problems understanding the scope of a coroutine I think. The moving UI object with his MoveOverTime will always be the same, only the text and image are changed in a message. I canât really make this work because of that and need some clarification
The MoveOverTimeContainers:
1: wait 0s, move to left within 0.9s
2: wait 2s, move to 0/0 (original) within 0.6s
When I click the button 4 times rapidly, I want to scroll it to the left within 0.9s, show msg1 for 2s, then msg2 for 2s, then msg3 for 2s, then msg4 for 2s, then go back to original pos. If I add the message while it is already moving back, interrupt that and move to position1 and continue normally.
Any help appreciated ![]()
