Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Java question: MethodProvider and getLocalPlayer()


    dragontitties

    Recommended Posts

    Maybe I'm not understanding Java correct...

    Looking through the API for DreamBot I see that the method "getLocalPlayer()" belongs to the class MethodProvider

    Everything I've ever learned about Java tells me that an instance method requires you to instantiate the object before you can call methods belonging to it.. Can someone please explain why

     

    	Player player = getLocalPlayer();

    Works just fine? No errors when running. Shouldn't it have to be this? which also doesn't give an error which I would expect

     

    	MethodProvider mp = new MethodProvider();
    	Player player = mp.getLocalPlayer();

     

    Thanks

    Link to comment
    Share on other sites

    5 hours ago, dragontitties said:

    Can someone please explain why

    
    
    
    	Player player = getLocalPlayer();

    Works just fine?

    This is because your main class extends AbstractScript, which derives from MethodProvider. Therefore your main script class is an instance of MethodProvider and you have access to its instance methods.

    kuva.png

    If you were to access the localplayer from another class, you wouldn't want to create a new instance of MethodProvider, though. With the static api there's plenty of ways for you to access the localplayer, such as Client.getLocalPlayer() or Players.localPlayer().

    Link to comment
    Share on other sites

    7 hours ago, Hashtag said:

    This is because your main class extends AbstractScript, which derives from MethodProvider. Therefore your main script class is an instance of MethodProvider and you have access to its instance methods.

    kuva.png

    If you were to access the localplayer from another class, you wouldn't want to create a new instance of MethodProvider, though. With the static api there's plenty of ways for you to access the localplayer, such as Client.getLocalPlayer() or Players.localPlayer().

    Very cool, I want to get into creating some scripts as well in the future 

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.