I want to write a script that sets a random tag to off. If the tag is already off, then another random tag is selected and turned off instead. It would be structured something like this:
1) VARIABLE = random tag 11-13.
2) Check tag VARIABLE.
3) If VARIABLE = off, then goto 1.
4) Set tag VARIABLE off.
Unfortunately, I don't know how to make this work within the Hamsterspeak syntax. Can someone offer me some guidance?
You could do something like this:
Note that this would be an infinite loop if none of the tags were on. In order to prevent that, you could have a for-loop at the top that just checked that at least one of the tags from start_tag to end_tag was set to true, e.g.:
After that, if at_least_one_on was false, you would know not to do the while-loop, since all the tags were off.
Code:
variable(tag_not_set, tag)
tag_not_set := true
while(tag_not_set)do, begin
#start_tag and end_tag are either args if this is a function call or just hard-coded
tag := random(start_tag, end_tag)
if(check tag(tag))then, begin
set tag(tag, false)
tag_not_set := false
end
end
variable(tag_not_set, tag)
tag_not_set := true
while(tag_not_set)do, begin
#start_tag and end_tag are either args if this is a function call or just hard-coded
tag := random(start_tag, end_tag)
if(check tag(tag))then, begin
set tag(tag, false)
tag_not_set := false
end
end
Note that this would be an infinite loop if none of the tags were on. In order to prevent that, you could have a for-loop at the top that just checked that at least one of the tags from start_tag to end_tag was set to true, e.g.:
Code:
variable(i, at_least_one_on)
at_least_one_on := false
for(i, start_tag, end_tag)do, begin
if(check tag(i))then, begin
at_least_one_on := true
break
end
end
variable(i, at_least_one_on)
at_least_one_on := false
for(i, start_tag, end_tag)do, begin
if(check tag(i))then, begin
at_least_one_on := true
break
end
end
After that, if at_least_one_on was false, you would know not to do the while-loop, since all the tags were off.
Code:
plotscript, pickupitem1, begin
variable(tag_not_set, tag)
tag_not_set := true
variable(i, at_least_one_on)
at_least_one_on := false
while(tag_not_set)do, begin
for(i, 11, 13)do, begin
if(check tag(i))then, begin
at_least_one_on := true
break
end
end
tag := random(11,13)
if(check tag(tag))then, begin
set tag(tag, false)
tag_not_set := false
end
end
end
plotscript, pickupitem1, begin
variable(tag_not_set, tag)
tag_not_set := true
variable(i, at_least_one_on)
at_least_one_on := false
while(tag_not_set)do, begin
for(i, 11, 13)do, begin
if(check tag(i))then, begin
at_least_one_on := true
break
end
end
tag := random(11,13)
if(check tag(tag))then, begin
set tag(tag, false)
tag_not_set := false
end
end
end
I can't seem to get this work. The second half of the script works, but the first half doesn't. The first half is supposed to stop the the while loop from running infinitely if tags 11, 12, and 13 are all off. What am I misunderstanding?
Sorry, I should have wrote it all as one code-block. That's what I get for writing it as it came to me rather than all at once...
Try this:
Basically, you just have to do the for-loop check to see that at least one of the tags is on before starting the while-loop at all. If the "at_least_one_on" boolean is still false by the time you get to the while-loop, just skip it entirely.
Note that nothing will happen if they are all off. I put a comment in where you should add an else-block if you want something specific to happen in the case of all tags being off.
Try this:
Code:
plotscript, pickupitem1, begin
variable(tag_not_set, tag, i, at_least_one_on)
at_least_one_on := false
for(i, 11, 13)do, begin
if(check tag(i))then, begin
at_least_one_on := true
break
end
end
if(at_least_one_on)then, begin
tag_not_set := true
while(tag_not_set)do, begin
tag := random(11,13)
if(check tag(tag))then, begin
set tag(tag, false)
tag_not_set := false
end
end
end #, else yadda yadda yadda
end
plotscript, pickupitem1, begin
variable(tag_not_set, tag, i, at_least_one_on)
at_least_one_on := false
for(i, 11, 13)do, begin
if(check tag(i))then, begin
at_least_one_on := true
break
end
end
if(at_least_one_on)then, begin
tag_not_set := true
while(tag_not_set)do, begin
tag := random(11,13)
if(check tag(tag))then, begin
set tag(tag, false)
tag_not_set := false
end
end
end #, else yadda yadda yadda
end
Basically, you just have to do the for-loop check to see that at least one of the tags is on before starting the while-loop at all. If the "at_least_one_on" boolean is still false by the time you get to the while-loop, just skip it entirely.
Note that nothing will happen if they are all off. I put a comment in where you should add an else-block if you want something specific to happen in the case of all tags being off.



