Solved Returning value in ASync task

Discussion in 'Plugin Development' started by HeyAwesomePeople, Jun 25, 2014.

Thread Status:
Not open for further replies.
  1. Hello! I am trying to loop through teams in an ASync task to avoid any interruption with the other players on the server. But I am having some trouble. It doesn't seem to be setting the team variable. This is current code:

    Code:java
    1. private BasicTeams team;
    2.  
    3. public BasicTeams getTeam(final Player p) {
    4. team = null;
    5. Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
    6. public void run() {
    7. for (BasicTeams t : plugin.teams) {
    8. for (Player player : t.getMembers()) {
    9. if (p == player) {
    10. team = t;
    11. }
    12. }
    13. }
    14. }
    15. });
    16.  
    17. return this.team;
    18. }


    Right now, this just returns null no matter what. Why is that?
    I HAVE run debug messages and it does get to "team = t;". So whats wrong here?

    Thanks,
    HeyAwesomePeople

    fireblast709 AoH_Ruthless Maybe you could help me out here too xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    adam753

    The function is returning the value before it gets set in the task. Just remove the whole runTaskAsynchronously part.
     
  3. Offline

    mythbusterma

    HeyAwesomePeople Honestly, it's not worth the trouble of running it asynchronously here. This operation is not very expensive at all, and shouldn't take any noticeable amount of time.
     
  4. Offline

    unrealdesign

    By the way your problem is that you returned in a non-async way but you are doing the editing of "team" async. So what is happening is:
    1. You run the method
    2. It runs the loops async
    3. method returns 'team'
    4. async loop finishes

    As you can see, it doesn't finish the calculations in time so it returns null. The only reason you ever need to do async is when you are doing calculations that don't require returns in your method.
     
  5. unrealdesign No, what happens is

    1. Task is scheduled
    2. Method returns team
    3. Loop starts
    4. Loop finishes
     
  6. Offline

    fireblast709

    HeyAwesomePeople why not use a Map to map the players to a BasicTeam in the first place?
     
  7. fireblast709 I was going to do that a while back but I ended up not doing it for some reason. I see no reason to not use it now, so I will just do that. Thanks
     
  8. Offline

    unrealdesign


    Exactly what I said. By "runs loops async" I meant it is running the background. Sorry, I guess I explained it bad.
     
  9. unrealdesign The way you said it simply implies that it starts the loop before the value is returned, but it doesn't. The value is returned and then the loops starts, regardless of whether it was async or not :p
     
Thread Status:
Not open for further replies.

Share This Page