Jump to content

Calling a different script from within one?


PineappleSausage

Recommended Posts

Hi, I am new to this API and bot scripting in general (not to coding though) and I'm unsure of how to accomplish using multiple scripts. I'm trying to use something like a "main" Script as an overarching entry point, which can call other Scripts in sequential order. This way, I could implement modular Scripts.

 

So in my "main" script I have this:

 

@[member='Override']
public int onLoop() throws InterruptedException {
    Script tinCopperMiner = new TinCopperMiner(); // TinCopperMiner is a local script
    Bot bot = this.getBot();
    bot.getScriptExecutor().prepare(tinCopperMiner);
    bot.getScriptExecutor().start(tinCopperMiner);
    // call more local scripts as needed
    this.stop();

    return 0;
}

 The inner, modular Scripts would be responsible for calling stop(). This doesn't seem to work though (null pointer exception), probably am misusing the API...

Edited by PineappleSausage
Link to comment
Share on other sites

Edited as i misread the post. ~

 

AFAIK It is possible to switch which script you're running on a OSBot client through the API. 

 

However it's not possible to start another OSBot client from within a osbot script (I tried this in the past. It's against the rules).

 

A way you could accomplish this would be to write a socket that executes scripts and closes scripts depending on messages it receives from the client. This would require a reasonable bit of coding prowess and creativity, but as you've stated that you're not new to coding perhaps this idea might be relevant to you.

 

Edit 12637883533:

 

Creating a script manager that used modular coding would be fucking awesome though.  

Edited by PlagueDoctor
Link to comment
Share on other sites

Hm okay... Well I thought of a fairly easy way to still create modular scripts. Just need to separate out the implementation.

 

TinCopperMiner:

public class TinCopperMiner extends Script {
    private TinCopperMinerImpl tinCopperMinerImpl;

    @[member='Override']
    public void onStart() {
        TinCopperMinerImpl tinCopperMiner = new TinCopperMinerImpl(this);
    }

    @[member='Override']
    public int onLoop() throws InterruptedException {
        TinCopperMinerImpl.execute();
        this.stop();
        return 0;
    }

    @[member='Override']
    public void onExit() {}
}

 

TinCopperMinerImpl (the actual implementation):

public class TinCopperMinerImpl {
    Script parentScript;

    public TinCopperMinerImpl(Script script) {
        // still have access to the Script's API via this constructor arg
        parentScript = script;
    }

    // psuedo-code-y
    public void start() {
        setup();
        while (some condition is true) {
            loop();
        }
        exit();
    }

    // some methods similar to what you'd override in the Script class
    private void setup()
    private int loop()
    private void exit()
}

The "main" entry Script I talked about would then call out to individual implementations not Scripts:

@[member='Override']
public int onLoop() throws InterruptedException {
    TinCopperMinerImpl tinCopperMinerImpl = new TinCopperMinerImpl(this);
    tinCopperMinerImpl.execute();
    // call more executes of other Script implementations as needed
    this.stop();
    return 0;
}

Pretty hack-y and I'm not 100% sure this will work. But if it does work, it would allow me to use TinCopperMiner as a Script on its own, or use its implementation in a different script.

 

Is there some other, better way to create modular code?  Or maybe a tool which lets you run Scripts back to back (which is basically all I want to be able to do)? I'm just trying to keep code modular for readability and efficiency purposes. I stumbled upon a tutorial island script here on this site which had a switch statement in its onLoop with several hundred cases... definitely trying to avoid that.

Edited by PineappleSausage
Link to comment
Share on other sites

When I started writing my scripts, I made them modular similar to the post above, for example my tutorial island, blue Dragon, blast furnace,.. scripts all require either a main script class, or a 'proxy' class that allows me to use them separately or together in a script.

The proxy class just makes sure all the onMessage, onPaint and onResponseCode gets forwarded to the correct module :)

Edited by Abuse
Link to comment
Share on other sites

Hi, I am new to this API and bot scripting in general (not to coding though) and I'm unsure of how to accomplish using multiple scripts. I'm trying to use something like a "main" Script as an overarching entry point, which can call other Scripts in sequential order. This way, I could implement modular Scripts.

 

So in my "main" script I have this:

 

@[member='Override']
public int onLoop() throws InterruptedException {
    Script tinCopperMiner = new TinCopperMiner(); // TinCopperMiner is a local script
    Bot bot = this.getBot();
    bot.getScriptExecutor().prepare(tinCopperMiner);
    bot.getScriptExecutor().start(tinCopperMiner);
    // call more local scripts as needed
    this.stop();

    return 0;
}

 The inner, modular Scripts would be responsible for calling stop(). This doesn't seem to work though (null pointer exception), probably am misusing the API...

 

If you just want to execute a script after another has stopped you can use my manager:

 

http://osbot.org/forum/topic/100554-explvs-osbot-manager/

 

 

But if you need something more complex, you could try initialising a script like so (I have not tested this):

// Initialise the script
Script someOtherScript = new SomeScript();
someOtherScript.exchangeContext(getBot());

// Call onStart
someOtherScript.onStart();

// In onLoop of your wrapper script call
return someOtherScript.onLoop();

You would probably also need to override the stop() method of the script you are running, so that it doesn't stop the ScriptExecutor and does something else instead.

Edited by Explv
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...