Author Topic: Sub in a Sub???  (Read 8247 times)

0 Members and 1 Guest are viewing this topic.

Offline xxcaptainxxTopic starter

  • Jr. Member
  • **
  • Posts: 47
  • Activity:
    0%
  • Reputation Power: 0
  • xxcaptainxx has no influence.
  • Respect: +1
  • Referrals: 0
    • View Profile
Sub in a Sub???
« on: August 26, 2008, 07:00:26 AM »
0
can you have a sub within a sub???  i think so but i would hate to waste a lot of time trying to code an uncodeable :)  thanks all

Offline talkmill

  • Jr. Member
  • **
  • Posts: 36
  • Activity:
    0%
  • Reputation Power: 0
  • talkmill has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: Sub in a Sub???
« Reply #1 on: August 26, 2008, 07:16:03 AM »
0
I'm not a pro at this at all but did some tests. Some pro please correct me if i'm wrong but it seems like:

Code: [Select]
sub test1
sub test2
return
return

work the same way as:

Code: [Select]
sub test1
return

sub test2
return

What exactly is it you are trying to acomplish?

Offline xxcaptainxxTopic starter

  • Jr. Member
  • **
  • Posts: 47
  • Activity:
    0%
  • Reputation Power: 0
  • xxcaptainxx has no influence.
  • Respect: +1
  • Referrals: 0
    • View Profile
Re: Sub in a Sub???
« Reply #2 on: August 26, 2008, 07:22:18 AM »
0
Well in one of my subs i want to check if i have a certain resource and then if i don't i want to run another sub to restock it

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: Sub in a Sub???
« Reply #3 on: August 26, 2008, 08:42:06 AM »
0
Sub in a sub is bad form.  I never do it; it makes code difficult to read and harder to debug but can work if you implement it correctly.  I would stay away from that kind of programming practice is I was you.

With that said, even some coding pros (Kal In Ex) use subs in subs.  I guess if you know what you are doing then you can do anything...
« Last Edit: August 26, 2008, 08:44:15 AM by TrailMyx »
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline talkmill

  • Jr. Member
  • **
  • Posts: 36
  • Activity:
    0%
  • Reputation Power: 0
  • talkmill has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: Sub in a Sub???
« Reply #4 on: August 26, 2008, 08:57:56 AM »
0
Do you meen something like this?

Code: [Select]
sub stockresources
gosub checkresources
if #return < %wantedresources
  gosub restockresources
return

if thats the case i would say calling other subs from other subs should be done if it helps understandability/abstraction. Otherwise not :)
But then again i'm much more used to other programming languages then easyuo so i probably should be quiet (have a hard time doing that though  ;D )


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: Sub in a Sub???
« Reply #5 on: August 26, 2008, 09:08:37 AM »
0
Well, you can certainly justify using them.  Suppose a routine has a specific function used all over the script, but is mainly used in a specific subroutine.  Why not?

I guess I know from experience writing ScriptUO that when you hit a SUB tag, it does NOTHING.  It's just a tag marking a location for the GOSUBS to go.  So there's no overhead from a SUB; the overhead comes from the GOSUB since you have to setup the %0, %1, %2, %etc variables and perform the stack interaction and execution pointer jump.

However for me, I just would never do that.  I like code to have a specific flow to it.  Sub'n'subing just makes me think back to the good'ol days of coding BASIC using line numbers (MSBASIC, BASICA, ApplesoftBASIC, etc)  I'm not a fan of spaghetti coding, and this really smells like fresh pasta.  lol.  Thankfully, I'm allergic to pasta now.
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline Tidus

  • Lazy
  • Administrator
  • *
  • *
  • Posts: 1291
  • Activity:
    0%
  • Reputation Power: 15
  • Tidus is working their way up.Tidus is working their way up.Tidus is working their way up.
  • Gender: Male
  • Mind Blown
  • Respect: +151
  • Referrals: 2
    • View Profile
    • Ultimate Apparel
Re: Sub in a Sub???
« Reply #6 on: August 26, 2008, 09:43:53 AM »
0
i like to use the #result feature.  If it needs to go to a certain spot and it does it every time under the circumstance i use #true.  and make  an if statement in my main repeat sub so that it says if it returned true it goes here. otherwise it just continues on.
For those who have fought for it, freedom has a taste the protected will never know ~ Anonymous, Vietnam, 1968

Offline xxcaptainxxTopic starter

  • Jr. Member
  • **
  • Posts: 47
  • Activity:
    0%
  • Reputation Power: 0
  • xxcaptainxx has no influence.
  • Respect: +1
  • Referrals: 0
    • View Profile
Re: Sub in a Sub???
« Reply #7 on: August 26, 2008, 09:49:16 AM »
0
would one of you wise ones look at my heal and warn int he debug it is hurting my brain :)

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: Sub in a Sub???
« Reply #8 on: August 26, 2008, 01:56:34 PM »
0
You should try and implement some of the suggestions from CS, it'll work better that way.  Be sure to update your first post since I'm sure you have changed it some and add the recommendations.  Just be sure to balance your braces " { } ".

Ultimately, the best way to handle bandies is to do journal scanning; so I understand that looking at the Healing Commander here at ScriptUO can cause you some headaches.  I remember A-hardy had a journal-based bandage scanner; I'm not sure where that is now but it probably won't make your head hurt as much.
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline Bobkyou

  • Newbie
  • *
  • Posts: 6
  • Activity:
    0%
  • Reputation Power: 0
  • Bobkyou has no influence.
  • Respect: 0
  • Referrals: 1
    • View Profile
Re: Sub in a Sub???
« Reply #9 on: September 09, 2008, 10:13:10 AM »
0
Would excessive use of subs in subs potentially cause a memory leak?  I don't know how easyuo is coded, but I would imagine it keeps track of each time gosub is called, and would need to see a return for each gosub to release the memory space.

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: Sub in a Sub???
« Reply #10 on: September 09, 2008, 10:31:21 AM »
0
Would excessive use of subs in subs potentially cause a memory leak?  I don't know how easyuo is coded, but I would imagine it keeps track of each time gosub is called, and would need to see a return for each gosub to release the memory space.

I can't remember exactly where I read on EasyUO about this, but I believe I remember mention of safeguards in EasyUO that prevents you from doing this too much.  I'll look and see if I can dredge up the thread.
Please read the ScriptUO site RULES
Come play RIFT with me!

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: Sub in a Sub???
« Reply #11 on: September 09, 2008, 04:54:29 PM »
0
Oh duh, right out of the EasyUO Wiki.

http://wiki.easyuo.com/index.php/Gosub

Quote
   You must not jump out of a sub! Use return to properly terminate a sub routine. To prevent a stack overflow, EUO only supports 1000 consecutive GoSubs without returning. Remember this when using recursion! When the GoSub stack is about to get 1001 levels, the very first level in the bottom of the stack is deleted to make room.

Right now in ScriptUO, it's unlimited.  At least until you use all your memory and crash your computer.  ;) I will put similar restrictions on it.
Please read the ScriptUO site RULES
Come play RIFT with me!

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: Sub in a Sub???
« Reply #12 on: September 09, 2008, 05:06:06 PM »
0
I can not think of a single circumstance that would justifiy coding a sub in a sub..  Makes no sense at all..
Sub A
  display ok a
  sub b
    display ok b
   return
return


Below Produces the same result as above.. without the funny coding practices.

Sub A
  display ok a
  gosub B
Return

Sub b
  display ok b
return
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: Sub in a Sub???
« Reply #13 on: September 09, 2008, 05:25:14 PM »
0
I can not think of a single circumstance that would justifiy coding a sub in a sub..  Makes no sense at all..

ROFL, yeh, the only time I ever encounter it is when a new coder is learning the ropes and doesn't really understand the concept of a gosub vs. a goto.  If it's not clear to anyone, just let us know and we can explain the concept for ya'll.
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline Bobkyou

  • Newbie
  • *
  • Posts: 6
  • Activity:
    0%
  • Reputation Power: 0
  • Bobkyou has no influence.
  • Respect: 0
  • Referrals: 1
    • View Profile
Re: Sub in a Sub???
« Reply #14 on: September 09, 2008, 06:35:32 PM »
0
Well recursion would call subs from subs, and recursion can do in a very small amount of code what would take many lines to accomplish.  However since gosubs are more cpu intensive than other activities, recursion ( I believe ) would become very slow in easyuo very fast.  Never actually run tests with it in easy uo, just remembering little programs I played with back in the day.

Tags: