x


Generator function visibility?

I've been using a jpeg encoder to take a screenshot of my game. The function which encodes the screenshot, aptly named EncodeScreenshot(filepath: String) is a generator function that is it contains a yield statement. The same javascript contains another function, getFilepath() which returns the filepath where the screenshot is stored.

From my gui script (written in C sharp), I can call getFilepath, no problem. But when I try to call EncodeScreenshot(filepath), it doesn't appear to call the function. I have Debug.Log statements inside the function, and before and after the function call. The before and after statements print, but not the one inside the function.

On the other hand, if I write a test OnGui into the Screenshot script, the function saves the screenshot to the correct location.

What is happening? Why does one function call work and not the other? My wild guess is that it has something to do with the way generator functions work.

GUI Code abridged

void OnGui(){
     if(submenuVisible = 1){
          getScreenshot();
     }
}

void getScreenshot(){
         string filename = this.GetComponent<Screenshot>().ScreenShotName();       
         string message = "Your screenshot will be saved to: " + filename;
       this.GetComponent<Screenshot>().ScreenshotEncode(filename);
         GUI.BeginGroup(centerRect);
         GUI.Box(new Rect(0, 0, centerRect.width, centerRect.height), message);
         if(GUI.Button(new Rect((int)(centerRect.width/2 - 50), centerRect.height - 40, 100, 30), "OK")){
          Debug.Log("Button true");
          this.GetComponent<Screenshot>().ScreenshotEncode(filename);
                Debug.Log("Picture stored?");
          submenuVisible = -1;
         }
         GUI.EndGroup();

    }

Screenshot Code in full

import System.IO;

private var filename : String;
/**
* Test key
*/
function OnGUI()
{
    if(GUI.Button(new Rect(50, 50, 100, 100), "click")){
       filename = ScreenShotName(); 
       ScreenshotEncode(filename);
    }
}
function Start(){
    filename = "";
}

/**
* Take the screen buffer and spit out a JPG
*/
function ScreenshotEncode(filenameParameter: String)
{
    Debug.Log("Start Encode");
    filename = filenameParameter;
    // wait for graphics to render
    yield WaitForEndOfFrame();

    // create a texture to pass to encoding
    var texture:Texture2D = new Texture2D (Screen.width, Screen.height, TextureFormat.RGB24, false);

    // put buffer into texture
    texture.ReadPixels(Rect(0.0, 0.0, Screen.width, Screen.height), 0.0, 0.0);
    texture.Apply();

    // split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
    yield;

    // create our encoder for this texture
    var encoder:JPGEncoder = new JPGEncoder(texture, 75.0);

    // encoder is threaded; wait for it to finish
    while(!encoder.isDone) {
       yield;
    }

    // save our test image (could also upload to WWW)
    File.WriteAllBytes(filename, encoder.GetBytes());
    Debug.Log(String.Format("Took screenshot to: {0}", filename));

    //return filename;
}

function ScreenShotName() {
    return (Application.persistentDataPath + "/screen_" +
                         System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") +".jpg");
}
more ▼

asked Jan 23 '12 at 02:26 PM

cmauceri gravatar image

cmauceri
121 5 7 8

That line:

if(submenuVisible = 1){
     getScreenshot;
}

is that exactly how it is in your program? If so, I don't think it does what you expect. You are trying to call it as a member, not as a function. You should use

getScreenshot();

instead. Of course, if that was just pseudocode and not actually how it goes in your script, disregard this comment.

Jan 23 '12 at 03:01 PM syclamoth

It wasn't supposed to be pseudocode, but I was a bit sloppy with copy and paste. Will edit.

The GUI code is not all the code I'm using, but if you think the problem might be hiding in another part of the code, I can post it complete.

Jan 23 '12 at 03:14 PM cmauceri

Waait, is ScreenshotEncode meant to be a coroutine?

Jan 23 '12 at 03:22 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

In your getScreenshot C# method, you need to wrap

this.GetComponent<Screenshot>().ScreenshotEncode(filename);

in a StartCoroutine method.

Otherwise, it won't execute properly, because it doesn't know how to handle the yield statements correctly. So-

StartCoroutine(this.GetComponent<Screenshot>().ScreenshotEncode(filename));

Should fix at least some of the problems.

The confusion arises because in JS you don't need to explicity state that your function is a Coroutine- all that stuff gets magically inserted at compile time.

more ▼

answered Jan 23 '12 at 03:23 PM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

Yikes, I just looked at the coroutine page on wikipedia. Is there a reference that might better explain what coroutines are? Your solution solves the problem, but now I'm curious.

Jan 23 '12 at 03:39 PM cmauceri

This is what comes from borrowing code! :P

Jan 23 '12 at 03:40 PM cmauceri
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3669
x56
x38
x16

asked: Jan 23 '12 at 02:26 PM

Seen: 502 times

Last Updated: Jan 23 '12 at 03:40 PM