Author Topic: Problem with calling a gosub within a sub in a for loop (argh!)  (Read 4312 times)

0 Members and 1 Guest are viewing this topic.

Offline brehTopic starter

  • Newbie
  • *
  • Posts: 8
  • Activity:
    0%
  • Reputation Power: 1
  • breh has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
0
Ok this is making me crazy.

I'm trying to cycle through a certain number of names, and passing them on to scanjournal within a sub. The problem is that while I can get the names to be in the %1 %2 %3 etc. variables, when I call the gosub within the sub, it overwrites the %2 %3 with its own %2 %3 variables. How do I avoid that?

Here is a code tidbit:

Code: [Select]



gosub example_goSub name1 name2 name3 name4


sub example_goSub
set %_cnt 1
while %_cnt < %0
{
set %message
display ok MSG: %message % . %_cnt , #spc
   set %message % . %_cnt , #spc
   display ok MSG: %message %_cnt
   gosub TM_AdvJournalScan network VALID_ADVANCE %message hi
   display ok MSNG2: %message %_cnt
;set %_cnt %_cnt + 1
display ok %_cnt
}
return



sub TM_AdvJournalSync
  namespace push
  namespace local TM_AdvJS_ , %1
  set !_jindex #jindex + 1
  if %0 > 1
    set !lpc_set %2
  namespace pop
  set !TM_FunctionCalled #TRUE
return

sub TM_AdvJournalScan
  namespace push
  namespace local TM_AdvJS_ , %1
  set !args %2
  set !temp_lpc #LPC
  if !lpc_set = N/A
    set #LPC 1000
  else
    set #LPC !lpc_set
  set !num_args %0
  if VALID in !args
  {
     set !charname %3
     set !first_arg 4
  }
  else
      set !first_arg 3
  if !_jindex = N/A
    set !_jindex #jindex
  if !charname = N/A
  {
    set !charname #CHARNAME
    AdvJournalScan_loop1:
      str pos !charname #SPC
      if #STRRES <> 0
      {
        set !val #STRRES - 1
        str left !charname !val
        set !left #STRRES
        set !val !val + 1
        str del !charname 1 !val
        set !charname !left , _ , #STRRES
        goto AdvJournalScan_loop1
      }
  }
  set !index !first_arg
  repeat
    set !temp_jindex !_jindex
    set !text % . !index
    while !temp_jindex <= #jindex
    {
      scanjournal !temp_jindex
      str pos #JOURNAL !charname 1
      set !namepos #STRRES
      str count #JOURNAL !charname
      set !namecnt #STRRES
      str pos #JOURNAL :_ 1
      set !smcpos #STRRES
      str pos #JOURNAL !text 1
      set !textpos #STRRES
      if !textpos < !smcpos && !smcpos <> 0 || !smcpos = 1 || :_ notin #JOURNAL || VALID notin !args
        set !pass #TRUE
      else
        set !pass #FALSE
      if ( !text in #journal && ( ( !namepos = 1 && !namecnt <= 1 ) || !pass ) )
      {
        set !temp_jindex !temp_jindex + 1
        if ADVANCE in !args
          set !_jindex !temp_jindex
        set #LPC !temp_lpc
        namespace pop
        set !TM_FunctionCalled #TRUE
        return #TRUE
      }
      set !temp_jindex !temp_jindex + 1
    }
    set !index !index + 1
  until !index - !first_arg > !num_args - !first_arg
  set #LPC !temp_lpc
  namespace pop
  set !TM_FunctionCalled #TRUE
return #FALSE

Offline Khameleon

  • Script Tester - Team Leader
  • Elite
  • *
  • *
  • Posts: 2574
  • Activity:
    0%
  • Reputation Power: 30
  • Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!
  • Gender: Male
  • Respect: +238
  • Referrals: 0
    • View Profile
Re: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #1 on: April 02, 2011, 06:02:38 AM »
0
your going to have to learn Namespace.  this is a good tutorial to get your feet wet.

Offline brehTopic starter

  • Newbie
  • *
  • Posts: 8
  • Activity:
    0%
  • Reputation Power: 1
  • breh has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #2 on: April 02, 2011, 06:05:40 AM »
0
your going to have to learn Namespace.  this is a good tutorial to get your feet wet.

Thanks, but that doesnt work. In the sub there is already namespace (the TM_AdvJournalScan) so that doesnt solve it.

Offline Khameleon

  • Script Tester - Team Leader
  • Elite
  • *
  • *
  • Posts: 2574
  • Activity:
    0%
  • Reputation Power: 30
  • Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!
  • Gender: Male
  • Respect: +238
  • Referrals: 0
    • View Profile
Re: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #3 on: April 02, 2011, 06:17:04 AM »
0
YOU have to use it in your SUBS.. name space will store and separate each set of variables as their own. 

Offline brehTopic starter

  • Newbie
  • *
  • Posts: 8
  • Activity:
    0%
  • Reputation Power: 1
  • breh has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #4 on: April 02, 2011, 06:29:23 AM »
0
Hmm but as far as I can read from that site I am doing it correctly?

once i go into gosub TM_AdvJournalScan network VALID_ADVANCE %message hi

I create a namespace as seen here:

sub TM_AdvJournalScan
  namespace push
  namespace local TM_AdvJS_ , %1

.....

and at the end when I exit

namespace pop

to re-create the old namespace, correct?

It still doesnt work :(

Offline 12TimesOver

  • 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: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #5 on: April 02, 2011, 08:49:53 AM »
0
Ok this is making me crazy.

I'm trying to cycle through a certain number of names, and passing them on to scanjournal within a sub. The problem is that while I can get the names to be in the %1 %2 %3 etc. variables, when I call the gosub within the sub, it overwrites the %2 %3 with its own %2 %3 variables. How do I avoid that?

Unless I'm not quite understanding what you are trying to do...

Maybe you should first be setting some variables to the values you are sending to the sub then use those not the %1, %2 etc:

Code: [Select]
gosub Mysub val1 val2 val3

sub Mysub
   set %Mysub1 %1
   set %Mysub2 %2
   set %Mysub3 %3

   do stuff with %Mysub1
   do stuff with %Mysub2
   do stuff with %Mysub3
return

Adding the Namespace to your sub is even better but it isn't always necessary to use a Namespace, it really depends on what you are trying to accomplish and where in the script the sub is.

Maybe I'm misunderstanding you though, I'm VERY tired ;)



X
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 brehTopic starter

  • Newbie
  • *
  • Posts: 8
  • Activity:
    0%
  • Reputation Power: 1
  • breh has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #6 on: April 02, 2011, 08:56:11 AM »
0
Ok this is making me crazy.

I'm trying to cycle through a certain number of names, and passing them on to scanjournal within a sub. The problem is that while I can get the names to be in the %1 %2 %3 etc. variables, when I call the gosub within the sub, it overwrites the %2 %3 with its own %2 %3 variables. How do I avoid that?

Unless I'm not quite understanding what you are trying to do...

Maybe you should first be setting some variables to the values you are sending to the sub then use those not the %1, %2 etc:

Code: [Select]
gosub Mysub val1 val2 val3

sub Mysub
   set %Mysub1 %1
   set %Mysub2 %2
   set %Mysub3 %3

   do stuff with %Mysub1
   do stuff with %Mysub2
   do stuff with %Mysub3
return

Adding the Namespace to your sub is even better but it isn't always necessary to use a Namespace, it really depends on what you are trying to accomplish and where in the script the sub is.

Maybe I'm misunderstanding you though, I'm VERY tired ;)



X

Hmm, what im trying to do is feed the sub a bunch of names, which it then checks via journal to say if any of those names said something (in this example hi).

Again the problem is as you can see if you run the script that the gosub makes it so the initial %1 %2 &3 &4 gets overwritten by the %1 %2 %3 of the subscript.

Scrripty

  • Guest
Re: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #7 on: April 02, 2011, 11:15:08 AM »
0
Well you've got your counter increase commented out...  You gotta set your count to count + 1 somewhere... uncomment it and it will work.  You also have your formatting a bit wrong for the while statement.  It wasn't counting the 4th variable and setting it.  Doing it the way I did it below will ensure it checks the variables 1-4 that are sent to the sub.  It should look more like this:

Code: [Select]
gosub example_goSub name1 name2 name3 name4
halt

sub example_goSub
set %_cnt 0
while %_cnt < %0
{
set %_cnt %_cnt + 1
set %message
display ok MSG: %message % . %_cnt , #spc
   set %message % . %_cnt , #spc
   display ok MSG: %message %_cnt
   gosub TM_AdvJournalScan network VALID_ADVANCE %message hi
   display ok MSNG2: %message %_cnt
display ok %_cnt
}
return
« Last Edit: April 02, 2011, 11:22:16 AM by Scripty »

Offline brehTopic starter

  • Newbie
  • *
  • Posts: 8
  • Activity:
    0%
  • Reputation Power: 1
  • breh has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #8 on: April 02, 2011, 11:45:08 AM »
0
I get this:

Name1 (correct)

then VALID_ADVANCE (which is wrong should be name2)

thenV ALID_ADVANCE (which should be name3)

then i get hi (which is wrong should be name4)

So still not working :(

Scrripty

  • Guest
Re: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #9 on: April 02, 2011, 12:02:45 PM »
0
Well I wasn't trying to make it work, I was trying to fix your formatting errors.  I dont use TM's journal scanner, so you'll have to talk to someone with more experience with it.  I'm pretty sure tho that VALID should not be used in this case.  VALID invokes spam filtering that will keep things being said from others from giving a positive in a search.  Not sure how it would interact with your scans tho.  Sounds like you need to go find hit thread and do a lot more reading.

Offline brehTopic starter

  • Newbie
  • *
  • Posts: 8
  • Activity:
    0%
  • Reputation Power: 1
  • breh has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #10 on: April 02, 2011, 12:36:42 PM »
0
Ok thanks anyway :) hopefully someone else can shed some light on it.

Offline 12TimesOver

  • 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: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #11 on: April 03, 2011, 03:56:05 AM »
0
Ok thanks anyway :) hopefully someone else can shed some light on it.
Like I said, don't use %1 %2 etc, assign them to variables that won't be overwritten and use those instead.

X
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 manwinc

  • Elite
  • *
  • *
  • Posts: 2556
  • Activity:
    0%
  • Reputation Power: 32
  • manwinc is a rising star!manwinc is a rising star!manwinc is a rising star!manwinc is a rising star!manwinc is a rising star!manwinc is a rising star!
  • Gender: Male
  • "The Devs Hard at Work"
  • Respect: +123
  • Referrals: 1
    • View Profile
Re: Problem with calling a gosub within a sub in a for loop (argh!)
« Reply #12 on: April 03, 2011, 08:09:22 AM »
0
Exactly what 12x said, once you get to the sub, set new variables for the values of %1.

Essentially %1 %2 %3 %n are placeholders used by subs, so the second you use another sub it will override the old placeholders. Only way around that is to set your own variables at the start of the sub to %1 %2 %3 etc
Monkeys and Typewriters!

" Oh I know, We'll make a Boss Encounter that requires 3 keys per player to enter, Then we'll make it not a closed instance so you never know if you are going to pop into a fresh room or a boss that has 1% Health left with 20 dudes smashing its face in, wasting your time and effort"

Tags: