1

I've been trying to make a mod for Scratch that would use the website's API to pull information about user's. The problem is that I've had to break it into two blocks. (These blocks work by calling functions based on a table with block names and function names, and passes in the arguments.) I've set it up where the first one (which calls primAskAboutMe) loads the information from the API, and then when the information loads, the user can use the second block (which calls primReportAboutMe) to return the value. The problem is that I can't get the second block to return an answer.

So either I'm trying to get information the wrong way, (which is possible because this is the first time I've used Action Script/Flash or whatever,) or I really don't understand what I'm doing.

*Note: Original Scratch Repository. This code would be found in src/primitives/Primitives.as (but isn't because I haven't uploaded this yet to my own repository, because I'm not really sure how).

protected var resultAM:String;

    protected function primAskAboutMe(b:Block):void {
var user:String = interp.arg(b, 0);
    var loader:Loader = new Loader();
    var url:String = 'http://www.api.scratch.mit.edu/users/'+user;
    var urlReq:URLRequest = new URLRequest(url);
    loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onCompleteAboutMe );
    loader.load(urlReq);



}
protected function onCompleteAboutMe(e:Event):void{
    //resultAM = e.target.data;
    resultAM="foo";
    }

protected function primReportAboutMe():String{


return resultAM;


}
BKFighter
  • 33
  • 7
  • Anyone? Does this have a bump feature? Hellooooo? – BKFighter May 29 '16 at 15:04
  • What is your Scratch script? Perhaps the data hasn't yet loaded when you try and access it. Click edit on the post above to add that in, and then reply to my comment. – Scimonster May 30 '16 at 03:37
  • No, there is no bump feature. You get an answer when a **volunteer** feels like dealing with your question. So you _"can't get the second block to return an answer"_, what does that mean exactly? You get a string but it's empty? Or you you mean you cannot run the function at all? Before you do line `return resultAM;` add the line `trace ("Result AM is : " + resultAM);`. What does it say about result? – VC.One May 30 '16 at 04:44
  • My apologies @VC.One I'll get back to you once I do those tests. – BKFighter May 30 '16 at 15:26
  • @VC.One Nothing appeared in the console. It didn't change the value of the variable in Scratch. I've tried many different setups which should allow enough time to load. – BKFighter May 30 '16 at 15:34
  • So your debugger didn't show any text like `Result AM is :` etc? That means maybe the function didn't even run at all? At what point (not shown in your code) are you even running that **primReportAboutMe** function? The code for that should look like `resultAM="foo";` now also add the following line `primReportAboutMe();` (which runs this function). Now do you get a feedback `Result AM is :` etc etc.... – VC.One May 30 '16 at 16:12
  • @VC.One Ten seconds after I used primAskAboutMe is when I ran primReportAboutMe. I also added 'resultAM = "loader sent";' at the en of primAskAboutMe which made no change. – BKFighter May 30 '16 at 17:11
  • I added 5 traces, and the first one, which is now the first line of code in 'primAskAboutMe' would not show in the console. I'm going to check some code that calls it but based on how picky gradle has been when compiling I can't imagine anything is wrong with it. Actually, 'primReportAboutMe' didn't trigger it's trace blocks either... – BKFighter May 30 '16 at 17:42
  • My bad... I should have mentioned you update an existing String variable with the `return` result... something like `myString = primReportAboutMe();`. See the basic test in my Answer. Also consider making your functions as `public` instead of `protected` just incase that status affects anything (it shouldn't, but you never know). You can always change back to protected functions once you're sure everything else works fine... – VC.One May 30 '16 at 17:44

1 Answers1

0

The problem is that I can't get the second block to return an answer.

I think it's because you haven't set a receiver for the returned result. If you make a Report string then update like str_Report = primReportAboutMe(); this will return text of resultAM into str_Report.

Here is a small testable example. Maybe it will help you in some way... Remember since primReportAboutMe returns String data it must be used to update some String variable.

var resultAM:String = "";
var checkResult:String = "";

resultAM="foo";

checkResult = primReportAboutMe(); //# update this String with function returned String
trace ("Check Result is : " + checkResult); //# shows... Check Result is : foo

function primReportAboutMe():String
{
    return resultAM; //# returns a String's current value
}
VC.One
  • 14,790
  • 4
  • 25
  • 57