Jump to content

A Beginners Guide to Writing OSBot Scripts (where to get started!) by Apaec


Apaec

Recommended Posts

7 hours ago, Apaec said:

Just had a quick glance, i'm not sure why you're extending Script in your second class? Only one class can extend Script, otherwise the script executor won't know the entry point to your code :)

Hopefully fixing this will prevent the crashing issue which you are experiencing. You should then be able to debug further issues yourself, if there are any :)

As a side note, when developing code it is important to do it in small parts. Add one bit at a time instead of writing the whole thing and only then testing it. That will make sure you don't end up in this situation in the future :)

-Apa

Brilliant! I never would have figured that out myself... Just one class in a script can extend Script? I do wonder how people would just know this... Probably a principle for when you extend anything then?

But that means I won't be able to use methods such as myPlayer().isAnimating() and the like from within my second class. Guess I've got to call all of that from inside my first class. Thanks! And yes, bad habit, it's good practice to keep checking my code frequently.

Link to comment
Share on other sites

8 hours ago, Glaciation96 said:

Brilliant! I never would have figured that out myself... Just one class in a script can extend Script? I do wonder how people would just know this... Probably a principle for when you extend anything then?

But that means I won't be able to use methods such as myPlayer().isAnimating() and the like from within my second class. Guess I've got to call all of that from inside my first class. Thanks! And yes, bad habit, it's good practice to keep checking my code frequently.

That's why you've got handy methods like exchangeContext(). Basically you can extend MethodProvider from your second class then when you initialise it from your main class run secondClass.exchangeContext(getBot()); or alternatively pass the bot instance (getBot()) to your second class from the main class and run the exchangeContext(bot) method in the second class' constructor. 

Edited by jca
  • Like 1
Link to comment
Share on other sites

2 hours ago, jca said:

That's why you've got handy methods like exchangeContext(). Basically you can extend MethodProvider from your second class then when you initialise it from your main class run exchangeContext(SecondClass); or alternatively pass the script instance (this) to your second class from the main class and run the exchangeContext method in the second class' constructor. 

Cheers man! very helpful. I'll definitely look into that. Trying to successfully achieve this check through multithreading has been a rocky road lol, but really wanna understand/learn this.

 

EDIT: Damn, I'm not getting any results for exchangeContext(). Is there another term for this method I can search up instead? 

Edited by Glaciation96
Link to comment
Share on other sites

4 hours ago, Glaciation96 said:

Cheers man! very helpful. I'll definitely look into that. Trying to successfully achieve this check through multithreading has been a rocky road lol, but really wanna understand/learn this.

 

EDIT: Damn, I'm not getting any results for exchangeContext(). Is there another term for this method I can search up instead? 

Check the API docs for the ref to exchangeContext() or do a quick Google search, there’s a lot of info around. 

I’ve updated my original answer to demonstrate the call - I wrote it too quickly before and made a mistake, sorry! The code should work now. 

Edited by jca
  • Like 1
Link to comment
Share on other sites

10 hours ago, jca said:

Check the API docs for the ref to exchangeContext() or do a quick Google search, there’s a lot of info around. 

I’ve updated my original answer to demonstrate the call - I wrote it too quickly before and made a mistake, sorry! The code should work now. 

Sorry, I initially thought exchangeContext() was a java method not associated with botting! I managed to research into and understand how to implement it as you've suggested. But exchangeContext() has been deprecated, so no Javadoc could be found. Is there a new or updated approach? I was hoping suppressing the warnings would be a magical way out lol. 

Thanks!

EDIT: Oh wow, even with the line going straight through, the variables still works. Just looks ugly. Thanks for the help, exchangeContext() is a life saver?

Edited by Glaciation96
Link to comment
Share on other sites

1 hour ago, Glaciation96 said:

Sorry, I initially thought exchangeContext() was a java method not associated with botting! I managed to research into and understand how to implement it as you've suggested. But exchangeContext() has been deprecated, so no Javadoc could be found. Is there a new or updated approach? I was hoping suppressing the warnings would be a magical way out lol. 

Thanks!

EDIT: Also, I've seen from Explv that it's marked for internal use only, but still fine to use? How do I make it so it's "fine to use"? There is a horizontal line going straight through 'exchangeContext(getBot)'

It’s fine, a horizontal line through the method is a notification to you that it is deprecated rather than an error that will stop it compiling. So you can use it as normal, as far as I’m aware it is the best practice. 

  • Like 1
Link to comment
Share on other sites

2 hours ago, Glaciation96 said:

Sorry, I initially thought exchangeContext() was a java method not associated with botting! I managed to research into and understand how to implement it as you've suggested. But exchangeContext() has been deprecated, so no Javadoc could be found. Is there a new or updated approach? I was hoping suppressing the warnings would be a magical way out lol. 

Thanks!

EDIT: Oh wow, even with the line going straight through, the variables still works. Just looks ugly. Thanks for the help, exchangeContext() is a life saver?

I'd personally avoid using deprecated methods as they may be removed altogether in future builds. There are other ways to achieve what you're looking for without exchangeContext, for example you can pass a MethodProvider or Bot instance to the new class via the constructor :)

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

In the Setting up part, I have two issues. First when setting up a class, when i name my project and hit finish a window automatically pops up prompting me to make a module name. Then after that I have no clue how to open the class window.

Second, when i get to the build path config and try to hit add external jar the button is not clickable. I do not understand why the button is not clickable.

 

Thank you for you help.

Anthony

Link to comment
Share on other sites

8 hours ago, Toomanytimes said:

In the Setting up part, I have two issues. First when setting up a class, when i name my project and hit finish a window automatically pops up prompting me to make a module name. Then after that I have no clue how to open the class window.

Second, when i get to the build path config and try to hit add external jar the button is not clickable. I do not understand why the button is not clickable.

 

Thank you for you help.

Anthony

Hey :)

I don't think that the interfaces have changed from what i've specified in the guide. Are you sure you followed the steps outlined in the screenshots ?

If so, maybe you could send me a couple of screenshots and I can make suggestions for what to try

Apa

Link to comment
Share on other sites

  • 2 weeks later...

Hey there! Hope you're all doing well, but unfortunately, I am back.

I'll make it quick this time around:) 

I'm trying to get into using Lambdas, but I'm a little confused.

Is there an import which includes the Sleep method that enables the use of lambdas? I have import org.osbot.rs07.utility.ConditionalSleep; but clearly that isn't it. I just see some people using lambdas in their script, but I'm not seeing the functional interface which is required? Maybe they're not pasting the entirety of their script, but would I have to create my own interface? Just wondering if there was already an osbot import/extension for that, other than the built-in java ones.

Cheers!

 

Link to comment
Share on other sites

14 hours ago, Glaciation96 said:

Hey there! Hope you're all doing well, but unfortunately, I am back.

I'll make it quick this time around:) 

I'm trying to get into using Lambdas, but I'm a little confused.

Is there an import which includes the Sleep method that enables the use of lambdas? I have import org.osbot.rs07.utility.ConditionalSleep; but clearly that isn't it. I just see some people using lambdas in their script, but I'm not seeing the functional interface which is required? Maybe they're not pasting the entirety of their script, but would I have to create my own interface? Just wondering if there was already an osbot import/extension for that, other than the built-in java ones.

Cheers!

Lambdas are a part of Java 8 so you won't be able to use them unless your script is built accordingly. Maybe this is where the issue is?

Lambdas are unrelated to OSBot, so it has nothing to do with api imports :)

Apa

Link to comment
Share on other sites

On 12/8/2018 at 2:09 PM, Apaec said:

Lambdas are a part of Java 8 so you won't be able to use them unless your script is built accordingly. Maybe this is where the issue is?

Lambdas are unrelated to OSBot, so it has nothing to do with api imports :)

Apa

Oh, I'm probably misunderstanding what imports are and how they work then lol. It's probably something to do with instantiating a class with/or an interface which enables you to use them then... The Sleep class might have it I guess, just gotta dig through the libraries. Thanks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...