Jump to content

Simple Osbot 2 Script Skeleton


Jack

Recommended Posts


import java.awt.*;

import javax.imageio.ImageIO;

import org.osbot.rs07.api.ui.Message;

import org.osbot.rs07.script.Script;

import org.osbot.rs07.script.ScriptManifest;

import java.io.IOException;

import java.net.URL;

@ScriptManifest(author = "Jack", name = "", version = 0.1, info = "", logo = "")

public class Core extends Script {

long startTime = 0;

private final Image paintImage = getImage("");

int mouseX,mouseY;

BasicStroke mouseStroke = new BasicStroke(1);

public void onStart() {

startTime = System.currentTimeMillis();

getBot().setHumanInputEnabled(false);

}

public int onLoop()throws InterruptedException{

return random(300, 400);

}

public void onPaint(Graphics2D g){

g.setStroke(mouseStroke);

g.setColor(Color.WHITE);

mouseX = mouse.getPosition().x;

mouseY = mouse.getPosition().y;

g.drawLine(mouseX-6, mouseY-6, mouseX+6, mouseY+6);

g.drawLine(mouseX-6, mouseY+6, mouseX+6, mouseY-6);

}

public void onMessage(Message message) {

if(message!=null){

}

}

private Image getImage(String url) {

try {

return ImageIO.read(new URL(url));

} catch(IOException e) {

return null;

}

}

public void onExit() {

}

}

Edited by Divinity
  • Like 2
Link to comment
Share on other sites

  1. Why throw onLoop in a try/catch block if all you're doing is printing an exception is thrown (no help for debugging what so ever)

Create BasicStroke as class variable so there aren't 40+ of them being created every second and killing the GC

Create an int variable to hold mouse x/y coords so you're not retrieving 8 of the same Point object (same reason as above)

  • Like 1
Link to comment
Share on other sites

 

  1. Why throw onLoop in a try/catch block if all you're doing is printing an exception is thrown (no help for debugging what so ever)
  2. Create BasicStroke as class variable so there aren't 40+ of them being created every second and killing the GC
  3. Create an int variable to hold mouse x/y coords so you're not retrieving 8 of the same Point object (same reason as above)

 

1. the scripter would put whatever that want here?

2/3. adding it now

Link to comment
Share on other sites

Updated your post with what he meant. Now you look normal happy.png

At least add the rest of the access modifiers if you added one of them ph34r.png

 

Also, in my opinion instantiating variables looks better if done from within the constructor instead of right up top. That's opinionated though :p

Edited by Swizzbeat
Link to comment
Share on other sites

 

  1. Why throw onLoop in a try/catch block if all you're doing is printing an exception is thrown (no help for debugging what so ever)
  2. Create BasicStroke as class variable so there aren't 40+ of them being created every second and killing the GC
  3. Create an int variable to hold mouse x/y coords so you're not retrieving 8 of the same Point object (same reason as above)

 

 

About 2 and 3 point.

Actually java JVM is very optimized for huge amount of short lived objects, for such small ones this is psh, no matter.

You can make tests if you dont belive.

Thing changes if you code in c++/c

  • Like 2
Link to comment
Share on other sites

About 2 and 3 point.

Actually java JVM is very optimized for huge amount of short lived objects, for such small ones this is psh, no matter.

You can make tests if you dont belive.

Thing changes if you code in c++/c

Just so I'm clear for what happens on the stack as well, does anything pushed onto the stack from a method immediately get popped off once those variables go out of scope (ie. exiting a method)?

 

So in that case, it wouldn't matter how many primitive data types you create in a method since they will be popped off and not there lingering in the heap.

Edited by Swizzbeat
Link to comment
Share on other sites

Just so I'm clear for what happens on the stack as well, does anything pushed onto the stack from a method immediately get popped off once those variables go out of scope (ie. exiting a method)?

 

So in that case, it wouldn't matter how many primitive data types you create in a method since they will be popped off and not there lingering in the heap.

 

They will be removed shortly, cuz you remove reference of your object each time you invoke method.

While developing java you shouldn't take care of such things because u have jvm developed a way that will help you free unused references. Actualy in some (if not most) cases java memory allocation is even faster than c++ because java jvm doesn't have sheduled deallocation, it frees when it wants, when it needs to.

 

In short answer:

His

methodInvoke -> create objects on stack -> no more use -> end of their life -> free.

Yours

LocalVar -> create object on heap -> use use use -> end of use on end of program -> free

Difference? None for scripter/overall programmer/

 

Ofc we are talking about damn few data bytes lol. Don't you guys have more serious problems ;o

 

Btw this skeleton, try to write api, this is kinda not herpfull ;P

When i mean api, add some utilities show uses of methods etc, skeleton isn't much different than v1

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

They will be removed shortly, cuz you remove reference of your object each time you invoke method.

While developing java you shouldn't take care of such things because u have jvm developed a way that will help you free unused references. Actualy in some (if not most) cases java memory allocation is even faster than c++ because java jvm doesn't have sheduled deallocation, it frees when it wants, when it needs to.

 

In short answer:

His

methodInvoke -> create objects on stack -> no more use -> end of their life -> free.

Yours

LocalVar -> create object on heap -> use use use -> end of use on end of program -> free

Difference? None for scripter/overall programmer/

 

Ofc we are talking about damn few data bytes lol. Don't you guys have more serious problems ;o

 

Btw this skeleton, try to write api, this is kinda not herpfull ;P

When i mean api, add some utilities show uses of methods etc, skeleton isn't much different than v1

I just wanted to see if anyone caught anything I missed for osbot 2. I should be updating when I can add more methods.

Link to comment
Share on other sites

They will be removed shortly, cuz you remove reference of your object each time you invoke method.

While developing java you shouldn't take care of such things because u have jvm developed a way that will help you free unused references. Actualy in some (if not most) cases java memory allocation is even faster than c++ because java jvm doesn't have sheduled deallocation, it frees when it wants, when it needs to.

 

In short answer:

His

methodInvoke -> create objects on stack -> no more use -> end of their life -> free.

Yours

LocalVar -> create object on heap -> use use use -> end of use on end of program -> free

Difference? None for scripter/overall programmer/

 

Ofc we are talking about damn few data bytes lol. Don't you guys have more serious problems ;o

 

Btw this skeleton, try to write api, this is kinda not herpfull ;P

When i mean api, add some utilities show uses of methods etc, skeleton isn't much different than v1

Stop being such a stack trace

  • Like 3
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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