- Home /
Yield not working within OnGUI()
I've been trying to get a GUI system to work using yield (which is, or at least should be, much easier than the alternatives), except that none of my code seems to be working. Come to think of it, I can see the problem with this approach (nothing would be displayed continuously without a loop or something to make it stay in place). Nevertheless, the odd part is that not a single line of code is working on any method that I call from OnGUI() that contains a yield statement, including debug code.
EG:
def OnGUI (): Debug.Log("This works") if GUILayout.Button("First button"): Debug.Log("Pushed first button") Test()
def Test (): Debug.Log("But neither this") if GUILayout.Button("Nor this"): yield GUILayout.Label("Nor this") Debug.Log("Nor this works")
This has become.... quite frustrating.
I guess it will be back to the drawing board with xml and parsers if I can't get this scripting solution to work. (It's for a conversation system, btw. I thought, why bother with more arrays and data languages if I can get it to work just using scripts? Much simpler; more powerful... now this).
Answer by PrimeDerektive · Feb 28, 2011 at 02:23 PM
I'm not sure exactly what you're looking for, but I think something like this would work:
private var isTalking : boolean = false;
function OnGUI(){
if(GUI.Button(Rect(10,10,150,30), "Start Conversation") && !isTalking)
StartCoroutine(StartTalking());
if(isTalking)
GUI.Label(Rect(10,50,500,30), "This conversation will be displayed for 5 seconds when you click the Start Conversation button.");
}
function StartTalking(){ isTalking = true; yield WaitForSeconds(5.0); isTalking = false; }
Yes, that would work. It's not the system that I was looking for (build a conversation tree just using scripting - ie plain logic using custom function calls and yield to maintain the conversation's position). Unfortunately that system would be impossible to implement via just scripting, which means that I'll just have to go back to my original plan: build complex editor classes to view, edit, and save conversation scripts using a data language, and load the data at runtime. This is probably 10x more complicated than a scripting workaround, but oh well... those scripts are nearly done anyways.
Answer by Eric5h5 · Feb 28, 2011 at 04:20 AM
You can't use yield in OnGUI any more than you can use it in Update. OnGUI runs every frame and can't be interrupted or delayed.
Yes, I'm well aware of that. But I'm using yield in a separate method (Test(), in the above example), so why isn't that working?
Ok, I can't get my code to run via boo, but I was able to get a bit of code running in javascript using yield within a loop. Not that that was what I had in $$anonymous$$d, but yes, you actually can use yield in a method called by OnGUI - though it might just be due to a javascript bug... . Back to the drawing board.
@Xathos: You need to use StartCoroutine in Boo if you want to start a coroutine. However, it's not what you want here, because the function would still be called every frame from OnGUI, so you'd end up with zillions of instances of it running.
Your answer
Follow this Question
Related Questions
yield OnGUI 1 Answer
Function On GUI 2 Answers
yield WaitForSeconds in OnGUI 4 Answers