Set a variable depending on if a bot is online or not

Grabuge

Member
I'm trying to make a script check which clanbot is online and ask them for a buff if I can't cast it. If I put only one BotName and BotID it works fine but I get "Unknown variable 'BotID' (test.ash, line 24)" if I try to make it switch if the bots aren't online. How do I set the variable to switch from one bot to the other?

Code:
import <zlib.ash>;

//Set to true if you want the script to use the bot even if you have the skill yourself.
boolean AbuseBot = true;

//Set the number of turns you want to ask the bots.
int BotTurns = 400;

if (is_online(000000)){
	string BotName = "Clanbot1";
	int BotID = 000000;
}
else if (is_online(1111111)){
	string BotName = "Clanbot2";
	int BotID = 1111111;
}

if ( have_effect($effect[Fat Leon's Phat Loot Lyric]) < 1 ){
	if ( AbuseBot == False){
		if (have_skill($skill[Fat Leon's Phat Loot Lyric])){
			use_skill($skill[Fat Leon's Phat Loot Lyric]);
		}
	}
	else if (is_online(BotID)){
		print("Asking "+BotName+" to cast Fat Leon's Phat Loot Lyric on you","green");
		string url = visit_url("sendmessage.php?pwd=&action=send&towho="+BotID+"&message="+ BotTurns +" phat&savecopy=on");
		print("Done... we're are waiting on "+BotName+" to buff you", "green");
		if(have_effect($effect[Fat Leon's Phat Loot Lyric]) < 1)
		repeat {wait(5); refresh_status();}
		until (have_effect($effect[Fat Leon's Phat Loot Lyric]) > 1);
	}
	else if ( AbuseBot == True && have_skill($skill[Fat Leon's Phat Loot Lyric])){
			use_skill($skill[Fat Leon's Phat Loot Lyric]);
	}
	else {
		abort ("You don't know how to cast Fat Leon's Phat Loot Lyric and both bots seem offline");
	}
}

if ( have_effect($effect[Polka of Plenty]) < 1 ){
	if ( AbuseBot == False){
		if (have_skill($skill[The Polka of Plenty])){
			use_skill($skill[The Polka of Plenty]);
		}
	}
	else if (is_online(BotID)){
		print("Asking "+BotName+" to cast Polka of Plenty on you","green");
		string url = visit_url("sendmessage.php?pwd=&action=send&towho="+BotID+"&message="+ BotTurns +" polka&savecopy=on");
		print("Done... we're are waiting on "+BotName+" to buff you", "green");
		if(have_effect($effect[Polka of Plenty]) < 1)
		repeat {wait(5); refresh_status();}
		until (have_effect($effect[Polka of Plenty]) > 1);
	}
	else if ( AbuseBot == True && have_skill($skill[The Polka of Plenty])){
			use_skill($skill[The Polka of Plenty]);
	}
	else {
		abort ("You don't know how to cast Polka of Plenty and both bots seem offline");
	}
 

Veracity

Developer
Staff member
Change:

Code:
if (is_online(000000)){
	string BotName = "Clanbot1";
	int BotID = 000000;
}
else if (is_online(1111111)){
	string BotName = "Clanbot2";
	int BotID = 1111111;
}
to:

Code:
string BotName;
int BotID;

if (is_online(000000)){
	BotName = "Clanbot1";
	BotID = 000000;
}
else if (is_online(1111111)){
	BotName = "Clanbot2";
	BotID = 1111111;
}
When you declare (and initialize) variables inside {} like you did, they are defined ONLY inside the {}.
 

Theraze

Active member
Create the BotID and BotName values even if offline. For example, throw an "else" after. Changing this:
Code:
if (is_online(000000)){
	string BotName = "Clanbot1";
	int BotID = 000000;
}
else if (is_online(1111111)){
	string BotName = "Clanbot2";
	int BotID = 1111111;
}
to add to the end.
Code:
if (is_online(000000)){
	string BotName = "Clanbot1";
	int BotID = 000000;
}
else if (is_online(1111111)){
	string BotName = "Clanbot2";
	int BotID = 1111111;
}
else {
	string BotName = "Offline";
	int BotID = -1;
}
 

Veracity

Developer
Staff member
Create the BotID and BotName values even if offline. For example, throw an "else" after. Changing this:
Code:
if (is_online(000000)){
	string BotName = "Clanbot1";
	int BotID = 000000;
}
else if (is_online(1111111)){
	string BotName = "Clanbot2";
	int BotID = 1111111;
}
to add to the end.
Code:
if (is_online(000000)){
	string BotName = "Clanbot1";
	int BotID = 000000;
}
else if (is_online(1111111)){
	string BotName = "Clanbot2";
	int BotID = 1111111;
}
else {
	string BotName = "Offline";
	int BotID = -1;
}
Theraze, that will not fix his compile error. I gave him the correct answer.

Using my suggestion, if he does not initialize the variables and none of the bots are online, BotName will be "" and BotID will be 0. Whether or not that is inferior to initializing them to "Offline" and -1, say, is a matter of taste; he might be better off simply having

Code:
else {
	abort( "None of the clanbots are currently logged in." );
}
 
Last edited:

Theraze

Active member
Theraze, that will not fix his compile error. I gave him the correct answer.

I was thinking it was a run error rather than a compile error, and so tried to keep it there. I did consider that it wasn't the right way, but... Eh, when people want to do inefficient code, I don't necessarily try to overly confuse them.

If memory serves, sloppy code like this used to work. But... ninja-ed, and with the 'proper' way to do it as well. :) Thanks for it.
 

Bale

Minion
If memory serves, sloppy code like this used to work.

Scope has always been controlled like this in ASH since I first began to script. I know this because I once had this problem in one of my very first scripts. I simply liked the elegance of defining my variable at the same time that I defined its initial value. That stopped once I understood was also defining their scope.
 

digitrev

Member
This is actually my biggest problem when dealing with Python. I learned how to code in C++, and scope is very strict and well-defined. In Python, half the time I'm not sure what the scope is.
 
Top