Jump to content

Cases vs private voids


Kramnik

Recommended Posts

Hi, so trying to create more advanced scripts and first thing that I need is to switch to more task based system instead of just using if statements as I did before. What first came to my mind is to use cases as I saw in couple other scripts. So to learn I made up this quick pseudoscript

If prayer left is not 0 -> prayer is off -> turn on the prayer, else pray at altar.

Spoiler

public int onLoop() throws InterruptedException {

        int pray = getSkills().getDynamic(Skill.PRAYER);
        log(pray);

        switch (pray){

            case 0:
                log("No prayer left");
                RS2Object altar = getObjects().closest("Altar");
                sleep(500);
                altar.interact("Pray-at");
                log("Praying at altar");
                sleep(1000);
                break;


            case 1:
                log("Full prayer, turning on pray");
                sleep(500);
                if(getPrayer().isActivated(PrayerButton.THICK_SKIN))
                {
                    log("Nevermind, prayer is on");
            }
            else{

                RS2Widget prayer = getWidgets().get(548, 53);
                if (prayer != null) prayer.interact();
                sleep(500);
                RS2Widget skin = getWidgets().get(541, 5);
                if (skin != null) skin.interact();
                sleep(1000);
                RS2Widget inv = getWidgets().get(548, 51);
                if (inv != null) inv.interact();
                sleep(700);
                break;

            }


        }

        return 500;
    }

And it does it's job. Then I checked Explvs 101 scripting tutorial and saw that it takes complete different approach, so wrote my script like he does:

Spoiler

@Override
public final int onLoop() throws InterruptedException {

    int pray = getSkills().getDynamic(Skill.PRAYER);

    if (pray == 1) {
        prayOn();
    } else {
        prayAltar();
    }
    return random(150, 200);
}

private void prayOn() throws InterruptedException {
    log("Full prayer, turning on pray");
    sleep(500);
    if(getPrayer().isActivated(PrayerButton.THICK_SKIN))
    {
        log("Nevermind, prayer is on");
    }
    else{

        RS2Widget prayer = getWidgets().get(548, 53);
        if (prayer != null) prayer.interact();
        sleep(500);
        RS2Widget skin = getWidgets().get(541, 5);
        if (skin != null) skin.interact();
        sleep(1000);
        RS2Widget inv = getWidgets().get(548, 51);
        if (inv != null) inv.interact();
        sleep(700);


    }
}

private void prayAltar() throws InterruptedException {
    log("No prayer left");
    RS2Object altar = getObjects().closest("Altar");
    sleep(500);
    altar.interact("Pray-at");
    log("Praying at altar");
    sleep(1000);
}

So they both do what they suppose to do, that's good. What I am curious about why some people use cases and some the other system. What are the pros and cons of each? 

The cases approach is very unfamiliar to me. How do you make more advanced tasks from it? In this case I just checked whether the prayer level is 0 or 1 (since its fresh acc it has only two options), guess this would be non sense if account had higher prayer level, but maybe then cases approach would be not the smart choice? 

In general what I currently like to learn is to make more extended script with task system in it. Lets say, go chop trees until level 20 woodcutting, then do quest, then go train combat and cases approach seem like a way to go for this kind of script :) 

Link to comment
Share on other sites

28 minutes ago, Malcolm said:

As ironic as this is you can write Tutorial Island without using cases but just checking the config with if statements also.

You can achieve the same thing. It's really just personal preference and readability imo.

You can write it either way. As the the link in Charlotte's post says and I quote "Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing." Which is why I said I and not should be written. I organized it that way by config number being config 660 or 670 so I can go straight to that section for bug testing. I don't know about you but I def prefer using the switch in the case. If it's something small like 4 things then I just use if statements.

Link to comment
Share on other sites

37 minutes ago, Malcolm said:

As ironic as this is you can write Tutorial Island without using cases but just checking the config with if statements also.

You can achieve the same thing. It's really just personal preference and readability imo.

By saying "config" you are refering to this part? :) From which the script then uses private voids and etc.

Spoiler

if (pray == 1) {

prayOn();

} else {

prayAltar();

} return random(150, 200); }

 

Link to comment
Share on other sites

16 minutes ago, Kramnik said:

By saying "config" you are refering to this part? :) From which the script then uses private voids and etc.

  Hide contents

if (pray == 1) {

prayOn();

} else {

prayAltar();

} return random(150, 200); }

 

No he isn't. Check #13 on Explv's scripting 101 where he explains what Configs are and using them. Quoting from Charlotte's post again "switch works with the byteshortchar, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer" Sorry I am lazy to manually change the black text. So basically in your case, your pray level will return as an int. So configs can return as int as well. So we tell the switch to grab that int value and which ever case equals that int, is the section of code we run for that specific Prayer level. Sorry if I explained this poorly and confused you.

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...