Kramnik Posted October 2, 2019 Share Posted October 2, 2019 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 Quote Link to comment Share on other sites More sharing options...
Charlotte Posted October 3, 2019 Share Posted October 3, 2019 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html 1 Quote Link to comment Share on other sites More sharing options...
Gunman Posted October 3, 2019 Share Posted October 3, 2019 What Charlotte posted is correct; But a script example where it I would use a switch is when grabbing the config(281) value on tutorial island to find which stage of the tutorial island the player is at; And based off that value I will tell it to run the section of code for that stage. Quote Link to comment Share on other sites More sharing options...
Gunman Posted October 3, 2019 Share Posted October 3, 2019 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. Quote Link to comment Share on other sites More sharing options...
Kramnik Posted October 3, 2019 Author Share Posted October 3, 2019 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); } Quote Link to comment Share on other sites More sharing options...
Gunman Posted October 3, 2019 Share Posted October 3, 2019 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 "A switch works with the byte, short, char, 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. Quote Link to comment Share on other sites More sharing options...
Charlotte Posted October 3, 2019 Share Posted October 3, 2019 You can use either if it’s efficient and comfortable. Anyway everything is all about preference. 2 Quote Link to comment Share on other sites More sharing options...
RSAccountsFarm Posted October 3, 2019 Share Posted October 3, 2019 if you got big script, using enums and cases makes debugging lot easier.for example if u got 3000 lines with just void methods its like navigating through fat manual without tables of content Quote Link to comment Share on other sites More sharing options...
Kramnik Posted October 4, 2019 Author Share Posted October 4, 2019 12 hours ago, free said: if you got big script, using enums and cases makes debugging lot easier.for example if u got 3000 lines with just void methods its like navigating through fat manual without tables of content You mean it would just suck scrolling though the code searching what you need? Quote Link to comment Share on other sites More sharing options...
RSAccountsFarm Posted October 4, 2019 Share Posted October 4, 2019 (edited) 1 hour ago, Kramnik said: You mean it would just suck scrolling though the code searching what you need? yes Edited October 4, 2019 by free Quote Link to comment Share on other sites More sharing options...