Author Topic: Looping without GoTo  (Read 9283 times)

0 Members and 1 Guest are viewing this topic.

Offline 12TimesOverTopic starter

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Looping without GoTo
« on: October 31, 2008, 11:12:43 AM »
0
Just a quick one here.

In the quest for learning alternative methods for setting up loops within my scripts without the use of GoTo statements (just a personal quest to enhance my creative thinking) I wondered if the following example is syntacticly accurate in it's use of the #RETURN sysvar preloaded with a value. I've done this before with my own user vars but just wondered what other people thought about this as a general practice.

This example uses TM's Gumpwait sub which returns #FALSE if the timeout value is reached before the Gump is found.

Code: [Select]
   set #RESULT #FALSE
   while #RESULT = FALSE
      {
      set #lobjectid %Container
      event macro 17
      gosub GumpWait Generic_Gump Generic_Gump
      finditem %ItemInContainer
      if #findkind <> 1
         set #RESULT #FALSE
      else set #RESULT #TRUE
      }

Thanks for your input!

XII
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Looping without GoTo
« Reply #1 on: October 31, 2008, 11:37:16 AM »
0
Hmm, well I think you are kinda "over-writing" the intention of the return value for GumpWait since you aren't really using it now in this context.  So I think there's some unintended consequences to your usage.

With that said, I have used the #RESULT variable for general purposes many times; sometimes I just don't want to create a variable in a quicky sub, i.e.:

Code: [Select]
;------------------------------------------------------------
sub TM_GetPersistantVariable
  set #RESULT %1 , _ , #CHARID
  set #RESULT * . #RESULT
return #RESULT
;------------------------------------------------------------
sub TM_SetPersistantVariable
  set #RESULT %1 , _ , #CHARID
  set * . #RESULT %2
return

Personally, I get a chuckle out of people who will jump through no end of hoops to rid themselves of goto's and tags.  Oh, *chuckes*
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline 12TimesOverTopic starter

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Re: Looping without GoTo
« Reply #2 on: October 31, 2008, 12:10:54 PM »
0
Hmm, well I think you are kinda "over-writing" the intention of the return value for GumpWait since you aren't really using it now in this context.  So I think there's some unintended consequences to your usage.

With that said, I have used the #RESULT variable for general purposes many times; sometimes I just don't want to create a variable in a quicky sub, i.e.:

<snip>

Personally, I get a chuckle out of people who will jump through no end of hoops to rid themselves of goto's and tags.  Oh, *chuckes*

Hehe, yeah that wasn't really a good example since I probably wouldn't be reopening a bag when a #finditem failed. I was just more concerned with the concept or syntax itself than the logic. Sounds like I'm not breaking any laws then.

As for the goto thing, well I'm just really trying to understand both sides and trying to think about things in more than one way. It really is more of a learning exercise for me than simply an evil goto statement remove activity. Personally I always get a feeling that I'm being inefficient when I pre-load a variable for a loop constraint just for the sake of loading the variable rather than as an activity of some other  logic within the program.

Did that make sense?

XII
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline Cerveza

  • Hacksimus Maximus
  • Scripthack
  • *
  • Posts: 5857
  • Activity:
    0%
  • Reputation Power: 80
  • Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!
  • Gender: Male
  • So... Hows that Hopey-Changey thing working out?
  • Respect: +403
  • Referrals: 11
    • View Profile
Re: Looping without GoTo
« Reply #3 on: October 31, 2008, 12:59:16 PM »
0
GOTO - Bad  >:(

While - Good  :D

Repeat/Until - Good  ;D
XXXXXXXXXX________________________________________] 20%
I've forgotten more about this game then most people will ever know.
Thank you for controlling your children. Their manners reflect your love for them.
Give a man a fish and you feed him for a day. Don't teach a man to fish, and you feed yourself. He's a grown man. Fishing's not that hard.

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Looping without GoTo
« Reply #4 on: October 31, 2008, 01:56:13 PM »
0
Wow, I'm surprised it took you so long Cerveza!  heh
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline 12TimesOverTopic starter

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Re: Looping without GoTo
« Reply #5 on: November 01, 2008, 01:37:22 AM »
0
Wow, I'm surprised it took you so long Cerveza!  heh

LOL I was thinking the same thing!
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline Endless Night

  • Global Moderator
  • *
  • *
  • Posts: 5467
  • Activity:
    0%
  • Reputation Power: 62
  • Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!
  • Respect: +393
  • Referrals: 1
    • View Profile
Re: Looping without GoTo
« Reply #6 on: November 01, 2008, 07:10:54 AM »
0
Well since basic 101 .. way back in the distant pass when commadors and sinclairs walked the earth .. i have never really found a need to use a goto statment.  Takes a slight readjustment of thinking but once you have it i...f your like me you will find gotos a real pain in the backside to logic flow.

Outlaw Josey Wales - "Manwink, A Long Gone Scripty, and Endless are always teasing us with their private sections lol. What there realy saying is scripters rule and users drool."
Briza - "Your a living breathing vortex of usefulness."

Offline Nicar

  • Full Member
  • ***
  • Posts: 198
  • Activity:
    0%
  • Reputation Power: 0
  • Nicar has no influence.
  • Respect: +20
  • Referrals: 0
    • View Profile
Re: Looping without GoTo
« Reply #7 on: November 01, 2008, 09:49:16 AM »
0
Once I get around to my bod sorter or gaman quest heartwood run script, I think in spite, I'm going to do it with nothing but gotos.   no subs, just all gotos. goto here, do this, sysmsg GoT0s Ru73!

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Looping without GoTo
« Reply #8 on: November 01, 2008, 11:22:48 AM »
0
Once I get around to my bod sorter or gaman quest heartwood run script, I think in spite, I'm going to do it with nothing but gotos.   no subs, just all gotos. goto here, do this, sysmsg GoT0s Ru73!

Unfortunately, that's where the GOTO got the bad rap.  Typically it's the first looping construct people learn because of past experience with the Jurassic period of computers as EN alluded to, and then are sooooo overused.  I kinda give EasyUO scripters a pass because to fuel the fires of using gotos, that was the only looping construct available other than the "FOR" statement in the original EasyUO 1.42 and before.  So you didn't have the nice "WHILE" or "REPEAT/UNTIL".  Also there was no "ELSE" with the "IF" statement so every one of your "IF" statements had to be prefaced with a 3rd argument if you wanted to skip a block of code on failure.  Now that is some crappy scripting, but some have held to the old ways so it makes code super difficult to read and understand the flow.

So just be thankful that you have these other nice loopers available, else you'd still be dining on Sinclair steaks.
« Last Edit: November 01, 2008, 11:47:08 AM by TrailMyx »
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline Cerveza

  • Hacksimus Maximus
  • Scripthack
  • *
  • Posts: 5857
  • Activity:
    0%
  • Reputation Power: 80
  • Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!
  • Gender: Male
  • So... Hows that Hopey-Changey thing working out?
  • Respect: +403
  • Referrals: 11
    • View Profile
Re: Looping without GoTo
« Reply #9 on: November 14, 2008, 04:31:36 AM »
0
I do use this as my main loop on all my scripts

Code: [Select]
TM:
repeat
.....
..... loop goes here
.....
until #CharGhost = YES
while #CharGhost = YES
  wait 0
GoTo TM

Ready for the punch line?

Because I've always told TM that my scripts would only have gotos......

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

OVER MY DEAD BODY!

 :o :o :o :o :o :o
 ;D ;D ;D ;D ;D ;D

Get it? #CharGhost = YES, my DEAD body!!

I could actually code that around the goto, but I have it in just for TM.

Only Corpses use gotos  :P
« Last Edit: November 14, 2008, 07:31:09 AM by Cerveza »
XXXXXXXXXX________________________________________] 20%
I've forgotten more about this game then most people will ever know.
Thank you for controlling your children. Their manners reflect your love for them.
Give a man a fish and you feed him for a day. Don't teach a man to fish, and you feed yourself. He's a grown man. Fishing's not that hard.

Offline _C2_

  • AFK FtW
  • Global Moderator
  • *
  • *
  • Posts: 4077
  • Activity:
    0%
  • Reputation Power: 48
  • _C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!
  • RIP Pen Trick
  • Respect: +254
  • Referrals: 4
    • View Profile
Re: Looping without GoTo
« Reply #10 on: November 14, 2008, 07:11:45 AM »
0
Funny stuff!  I've always been able to count on Cerv. for hating goto's but I also have to thank him for it too.   Ever since you started ripping them on past boards, I've challenged myself to try to write without them just for the challenge.  Nowadays it isn't a challenge anymore and I can write very differently.  I see a flow in my code that couldn't exist using strictly goto's.  I still use one here and there for basic ignoring other NPC loops occasionally even though i could edit that out too but it has helped me bring coding up to another level. 

Offline Xclio

  • Officially "The MAN"
  • Elite
  • *
  • *
  • Posts: 981
  • Activity:
    0%
  • Reputation Power: 9
  • Xclio has no influence.
  • Gender: Male
  • Respect: +56
  • Referrals: 1
    • View Profile
Re: Looping without GoTo
« Reply #11 on: November 14, 2008, 08:22:55 AM »
0
Cerv is my reason for hating the goto's as well.  I don't really "hate" them I guess but since I started to avoid them I have been much happier with the flow of my code and it makes debugging alot easier for myself and others I think.

Offline Endless Night

  • Global Moderator
  • *
  • *
  • Posts: 5467
  • Activity:
    0%
  • Reputation Power: 62
  • Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!
  • Respect: +393
  • Referrals: 1
    • View Profile
Re: Looping without GoTo
« Reply #12 on: November 15, 2008, 04:54:03 PM »
0
Most of the funny stuff that happens in the inexperienced scripters scripts are the result of using  gotos and not using Returns to exit a sub.
Outlaw Josey Wales - "Manwink, A Long Gone Scripty, and Endless are always teasing us with their private sections lol. What there realy saying is scripters rule and users drool."
Briza - "Your a living breathing vortex of usefulness."

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Looping without GoTo
« Reply #13 on: November 15, 2008, 07:08:46 PM »
0
Most of the funny stuff that happens in the inexperienced scripters scripts are the result of using  gotos and not using Returns to exit a sub.


That actually was part of my motivation for ScriptUO.  EasyUO gives ZERO feedback regarding the syntax and style of the code that you write.  Worst is that you could have the code line:

Code: [Select]
  barney loves ice %cream

and EasyUO will happily execute past it (doing nothing of course).  That zero feedback is really bad to have especially since EasyUO scripting is the first taste that many have had to "structured" programming languages.  Without a slap on the wrist, bad habits are developed quickly.  This is also why the first real feature I added to ScriptUO was the syntax checker.  Even in the early days of AppleSoft BASIC, there was a general syntax check attempted as the interpreter did its execution.
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline bliss

  • Jr. Member
  • **
  • Posts: 15
  • Activity:
    0%
  • Reputation Power: 0
  • bliss has no influence.
  • Respect: +2
  • Referrals: 0
    • View Profile
Re: Looping without GoTo
« Reply #14 on: December 11, 2008, 08:03:07 AM »
0
ok you guys have totaly confused me now. for some one who is just starting on his path to scripting is it easyer to learn with or with out goto's ?

Tags: