For a class group project, I am required to incorporate the work of a different group into my project that covers networking. The basics of the other group’s requirements were to incorporate LUA into their code so that a user can program certain objects to follow those commands at runtime. (for example, they type “trans(0,30,2)” into a text box at runtime, and the object associated with this text box would move to the position (0,30,2) )
To do this, they are using coroutines to run the LUA code and translate it into C#.
Problem is, that the movement executed in a coroutine does not update over the server if the one who programs it to move is a client. (i.e. if the host moves an object, everyone sees. If a client moves an object, only that client sees).
I have been struggling to fix this for 4.5 weeks now, and have tried everything that I can think of to make it work, but it just doesn’t.
Today I came up with the possibility that if I call a command function within the coroutine and translate in the command function, rather than in the coroutine function, that I might have some luck. But in order to return to that spot in the coroutine, I am pretty sure that I need to use a yield function, but I am not sure how to call the command function using yield.
Could anyone help me figure out what to do? Or give me some more ideas on what else to try?
I am ready to say that it just can’t be done, so I would really appreciate some feedback.
To make my explanation (hopefully) make more sense, here is a snippet of code from a test I am creating to try to get Unity coroutines, in general, to work correctly. The basic idea of this code is that if a player on the server clicks the Tab button, the object should move a given amount of times between a certain amount of predefined points, after waiting for a given amount of time. (or this is my belief on how it works-- the code is a modified (LUA-less) version of what was given to me from the other group).
Where they interpolate the points to move the object, I want to call the command function.
public void Translate(List<Vector3> list, float duration, int loopCount)
{
list = new List<Vector3>();
Vector3 vec1 = new Vector3(1.0f, 0.25f, 1.0f);
Vector3 vec2 = new Vector3(13.0f, 0.25f, -5.0f);
Vector3 vec3 = new Vector3(-11.0f, 0.25f, 3.0f);
list.Add(vec1);
list.Add(vec2);
list.Add(vec3);
coroutine = StartCoroutine(MoveCoroutine(list, duration, loopCount));
}
public IEnumerator MoveCoroutine(List<Vector3> list, float duration, int loopCount)
{
while (loopCount > 0 || loopCount < 0)
{
loopCount= " + loopCount);
foreach (Vector3 point in list)
{
translateTime = 0;
init = LUAObjPrefab.transform.position;
goal = point;
translateDuration = duration;
while (translateTime < duration)
{
float dTime = Time.deltaTime;
translateTime += dTime;
translateDuration -= dTime;
//onTranslateAction(dTime);
yield return new WaitForEndOfFrame();
float lerpValue = Mathf.Clamp01(translateTime / duration);
// ******WHERE I WANT TO YIELD!!!!!
//yield return new CmdServerTrans(init, goal, lerpValue);
CmdServerTrans(init, goal, lerpValue)
//what the other group had
//LUAObjPrefab.transform.position = Vector3.Lerp(init, goal, lerpValue);
}
}
if (loopCount > 0)
{
--loopCount;
}
}
gameObject.GetComponent<Rigidbody>().isKinematic = true;
move = false;
yield return 0;
}
[Command]
void CmdServerTrans(Vector3 start, Vector3 end, float lerpTime)
{
LUAObjPrefab.transform.position = Vector3.Lerp(start, end, lerpTime);
}