print loop?

Hey guys, I sure that the solution to this is fairly simple, but for some reason I'm having trouble getting it to work. I have several scripts with the syntax:
Code:
if ( <boolean expression> )
 <block or statement>
else
print("a line of text");

The problem I'm having is that this just keeps printing over and over. Obviously a simple solution would be to use the abort command, but I still want it to do the rest of the stuff in the script. Is there a way to have it print once without aborting the script?
 

Spiny

Member
Code:
 int i = 1;

if (i == 1)
  print("condition is true");
else
  print("a line of text");

This code works as expected. If you change it so that if (i != 1), then it will print "a line of text" only once. If you have more than one statement in the block, then you need to surround it with curly braces.

If that doesn't help, it may be an issue with the boolean expression you're using or if you're calling this elsewhere, perhaps the elsewhere is looping this over and over.

Hope that helps.
-Spiny
 
That makes sense, and that is what I thought. However I'm not sure still, how to go about fixing this in my scripts. Here's an example:

Code:
void spider_web(){
	if ( item_amount( $item[Spider web] ) < 10 )
		{cli_execute("adventure Sleazy Back Alley");}
	else
	[B]print ("Gourd Hoarder!!", "blue");[/B]
}

void firecracker(){
	if ( item_amount( $item[Knob Goblin firecracker] ) < 10 )
		{cli_execute("adventure Outskirts of The Knob");}
	else {spider_web();}
}

void can_lid(){
	if ( item_amount( $item[razor-sharp can lid] ) < 10 )
		{cli_execute("adventure Manor1: Haunted Pantry");}
	else {firecracker();}
}

void farm_gourd(){
	While( my_adventures() >= 0)	
		{can_lid();}
}

void main()
{farm_gourd();}

Is it repeating the print command over and over because of the while command?
Code:
While( my_adventures() >= 0)
or is something else causing it?
 

Bale

Minion
Once you have 10 can lids, 10 firecrackers AND 10 spider webs it will simply print "Gourd Hoarder" forever. I can't imagine why you would use that, but after it has 10 of each, there is no exit condition. It won't adventure, so my_adventures() will stop decrementing.
 
That's just the first ash script I wrote to see how things worked and test the water a little bit, so to speak. My question is how would I make that script print "Gourd Hoarder!!" one time only without aborting the script?
 

mredge73

Member
create an exit condition
there are several ways do doing that,
1 you could change all of your functions to boolean and have them return false if they want to flag an exit condition.
2 you could create a global variable that's only function is to cause your while to exit
3 etc...

easiest
Code:
//create global exit boolean
boolean IamDone=false;

void spider_web(){
	if ( item_amount( $item[Spider web] ) < 10 )
		{cli_execute("adventure Sleazy Back Alley");}
	else
{
print ("Gourd Hoarder!!", "blue");
IamDone=true;
}

}

void firecracker(){
	if ( item_amount( $item[Knob Goblin firecracker] ) < 10 )
		{cli_execute("adventure Outskirts of The Knob");}
	else {spider_web();}
}

void can_lid(){
	if ( item_amount( $item[razor-sharp can lid] ) < 10 )
		{cli_execute("adventure Manor1: Haunted Pantry");}
	else {firecracker();}
}

void farm_gourd(){
	While( my_adventures() >= 0 &&  !IamDone)	
		{can_lid();}
}

void main()
{farm_gourd();}
 
Last edited:
Excellent! That is very useful information. Thank you. That's what several of my scripts have been missing - exit conditions! Simple enough. Also, I've noticed the exclamation mark at work in a lot of scripts I've downloaded and now here, but I'm not sure I understand it. Does the exclamation mark just mean the opposite value of the boolean it proceeds or...? Sorry about all the n00b questions. Thanks for being patient with me.
 

Bale

Minion
It means "the opposite value of the boolean it precedes." It turns true to false and turns false to true.

It is read as "not". Thinking of it that way will make it easier to understand. So if you have boolean done, then !done is read as "not done".
 

mredge73

Member
quick bit of advice for programming for those new at programming in general.
when taking a programming class for the first time and you get to the chapter of loops early on and it will read like this:

first it will teach you how to create a loop

second it will teach you how to create an exit condition

third-100 it will tell you that infinite loops are BAD, very, very, bad. The rest of the chapter will be about identifying infinite loops and how to prevent them.

If you take this as a college course and you turn in a program that contains an infinite loop you will earn you a big fat 0 no matter how much work you put into it. Just keep this in mind when creating loops.
 
Top