Post new topic    
Liquid Metal Slime
Send private message
Check Tag scripting help 
 PostSun Jan 06, 2013 2:09 am
Send private message Reply with quote
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?
Red Slime
Send private message
 
 PostSun Jan 06, 2013 2:32 am
Send private message Reply with quote
You could do something like this:

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



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


After that, if at_least_one_on was false, you would know not to do the while-loop, since all the tags were off.
Liquid Metal Slime
Send private message
 
 PostMon Jan 07, 2013 12:34 am
Send private message Reply with quote
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


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?
Red Slime
Send private message
 
 PostMon Jan 07, 2013 1:49 am
Send private message Reply with quote
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:

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


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.
Liquid Metal Slime
Send private message
 
 PostMon Jan 07, 2013 3:12 am
Send private message Reply with quote
Thanks for all this. It makes sense as I read through the code, but I never would have figured this out on my own.

You just helped add a challenging new mode to my game. Thanks!
Red Slime
Send private message
 
 PostMon Jan 07, 2013 3:36 am
Send private message Reply with quote
No problem, glad I could help Smile
Display posts from previous: