1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

AsyncTasks And Multiple Functions

Discussion in 'Development' started by CupidonSauce173, Feb 17, 2020.

  1. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    Hello, I had a little question.
    So basically I have some heavy blocks of code that I want to run in AsyncTasks but I don't want to have a lot of AsyncTask classes, I would prefer to store them all in the same task class and call them from the plugin. I was thinking about asking for an $OPTION variable to use the AsyncTask and then looking if $OPTION === "blabla" to then execute a function in the task but I was looking for a more proper way to do it, if there is. I know AsyncTasks are not magical and won't remove 100% of the lags but it would at least reduce them. Or if there is better ways to do what I want, it would be welcomed lol.
     
  2. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    If I were you I would make a task file to do like 10 blocks per tick. It would be like $mode === 1 first 10 blocks, $mode++, $mode === 2 another 10 blocks etc.
     
    CupidonSauce173 likes this.
  3. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    But it is not about blocks, it's just to execute blocks of codes that takes some time to process which freeze the server during a really small amount of time. This is why I need to find a way to only call one function in the task class depending on the player action.

    By "blocks of codes" I mean like 10 lines of code :)
     
  4. Gianluxx

    Gianluxx Slime

    Messages:
    94
    GitHub:
    Gianluxx
    You can pass any variable to the task when you start it.. so you can pass an $option
     
    CupidonSauce173 likes this.
  5. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    Yes I know, this is what I explained in my first message, but I actually want something more proper than that if possible because I would need to have a long if and elseif thing to look what the $option is to calm the function in the task :)
     
  6. Gianluxx

    Gianluxx Slime

    Messages:
    94
    GitHub:
    Gianluxx
    more proper than a if or a switch? nah
     
    CupidonSauce173 likes this.
  7. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    I didn't think of a switch :thinking: that would be nice, better than the elseif thing. Ok I think I'll do that :) any other suggestions anyone ?
     
  8. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    If those blocks of code have absolutely nothing to do with each other, the proper way would actually be putting them in separate classes.
     
    CupidonSauce173 likes this.
  9. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    Yes I know...but that would mean that I need like at least 15 task classes and each of them would be called only by one thing, which is kind of useless
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.