Official ScriptUO EasyUO Scripts > Scripting Chat

Dynamic var creation based on %0 value of a gosub?

(1/3) > >>

12TimesOver:
Not sure if I'm on the right track here, any thoughts? I want to have a sub that accepts an unknown number of parameters and performs actions based on each parameter then moves on to the next. I think I have it conceptually but I know I'm not quite there.

So the example is something like the following. I want to pass on an unknown number of vendor properties then scan for a vendor matching one of those properties. Would this be easier to do if I passed a string more like "Property1_Property2_Property3" then match this against #property? I'm new at this so bear with me!


--- Code: ---Gosub Findvendor Blacksmith Weaponsmith Etc

;############################
;Sub FindVendor
;############################
; %1 Vendor property 1
; %2 Vendor property 2
; %3 etc
sub findvendor
   Namespace Push
   Namespace Local XIIxFindVendor
   for %Loop 1 %0
      {
      set %tmp
      set %Property %tmp.%Loop   ;<-----Want %Property = Blacksmith then Weaponsmith then Etc
 
      finditem !VendorTypes G_8
      if #findkind <> -1
         {
         event property #findid
         if %Property in #property
            {
            set %vendor #findid
            ignoreitem reset vendors
            Namespace Clear
            Namespace Pop
            return #true
            }
         }
      }
   ignoreitem reset vendors
   Namespace Clear
   Namespace Pop
return #false

--- End code ---

This is also my first attempt at integrating Namespaces as I totally see the value now, especially when trying to develop standardized subs. In this particular case I'm not quite sure how to return the value of %vendor if I Clear and Pop before the return but not sure how else to do that. Do I need to do a Copy first?

XII

TrailMyx:
Man, you are soooo close!

The first suggestion is that if you are going to use namespaces,  you should go ahead and convert your worker variables to the namespace variables !var.

Second is that you need to search through all the input arguments to the function starting at %1, %2, %3, etc up to how many is in %0.  You can do this by using the !loop variable as an index and then doing this:


--- Code: ---set !Property % . !Loop   ;<-----Want %Property = Blacksmith then Weaponsmith then Etc

--- End code ---

So for your example, !Property will return Blacksmith for %1 and Weaponsmith for %2.  Does this make sense how it's working?  I can explain further if you are confused.

Finally, since you are just returning something, you might as well put your return value in your #RESULT variable.  So you are either returning a #FINDID if successful, or #FALSE if nothing is found.  That way you won't loose the result when you switch back to the home namespace (and also clear the worker namespace like you are doing)

Here's kinda what I'm talking about.


--- Code: ---
;############################
;Sub FindVendor
;############################
; %1 Vendor property 1
; %2 Vendor property 2
; %3 etc
sub findvendor
   Namespace Push
   Namespace Local XIIxFindVendor
   for !Loop 1 %0
      {
      set !tmp
      set !Property % . !Loop   ;<-----Want %Property = Blacksmith then Weaponsmith then Etc
 
      finditem !VendorTypes G_8
      if #findkind <> -1
         {
         event property #findid
         if !Property in #property
            {
            ignoreitem reset vendors
            Namespace Clear
            Namespace Pop
            return #findid
            }
         }
      }
   ignoreitem reset vendors
   Namespace Clear
   Namespace Pop
return #false

--- End code ---

TrailMyx:
One other thing I just noticed is that you are going to want to look around at all the vendors, and for that you need to perform an event property on each one found, so you'll want to further tweak this (also it lets you get rid of the ignoreitem)


--- Code: ---;############################
;Sub FindVendor
;############################
; %1 Vendor property 1
; %2 Vendor property 2
; %3 etc
sub findvendor
   Namespace Push
   Namespace Local XIIxFindVendor
   for !Loop 1 %0
      {
      set !Property % . !Loop   ;<-----Want %Property = Blacksmith then Weaponsmith then Etc
      finditem !VendorTypes G_8
      if #FINDCNT > 0
        {
        for #FINDINDEX 1 #FINDCNT
        {
          event property #findid
          if !Property in #property
            {
            Namespace Clear
            Namespace Pop
            return #findid
            }     
          }
        }
      }
   ignoreitem reset vendors
   Namespace Clear
   Namespace Pop
return #false

--- End code ---

12TimesOver:

--- Quote from: TrailMyx ---The first suggestion is that if you are going to use namespaces,  you should go ahead and convert your worker variables to the namespace variables !var.
--- End quote ---
Ah, ok. I wasn't sure about that.


--- Quote from: TrailMyx ---Second is that you need to search through all the input arguments to the function starting at %1, %2, %3, etc up to how many is in %0.  You can do this by using the !loop variable as an index and then doing this:


--- Code: ---set !Property % . !Loop   ;<-----Want %Property = Blacksmith then Weaponsmith then Etc

--- End code ---

So for your example, !Property will return Blacksmith for %1 and Weaponsmith for %2.  Does this make sense how it's working?  I can explain further if you are confused.
--- End quote ---
No I think I got it. So really my fault in the loop was simply that I wasn't following the right to left concat. rules on the "array" function in that my result would have been something like %var1, %var2, etc rather than the value of %1, %2, etc?


--- Quote from: TrailMyx ---Finally, since you are just returning something, you might as well put your return value in your #RESULT variable.  So you are either returning a #FINDID if successful, or #FALSE if nothing is found.  That way you won't loose the result when you switch back to the home namespace (and also clear the worker namespace like you are doing)
--- End quote ---
Got it! I didn't stop to think that the system variables would continue to be valid after the clear of the Namespace! Very cool.


--- Quote from: TrailMyx ---One other thing I just noticed is that you are going to want to look around at all the vendors, and for that you need to perform an event property on each one found, so you'll want to further tweak this (also it lets you get rid of the ignoreitem)
--- End quote ---
Makes sense. The ignoreitem's are a relic of SM's old script and I wasn't sure about them quite yet. Good to have a different direction!

Thanks a lot for the feedback, I'm obsessing on this thing and have been a little stuck! I know what I'll be working on tonight after the kids head to bed lol.

TrailMyx:
BTW, good to see you starting to use namespaces.  They really are quite useful, but do add a bit of complication when it comes to debugging time.  Just wait until you have to pull your hair out when you forget to "POP" from the worker namespace.  That causes some funky results when the rest of the script was relying on data that's no longer there or at least "hidden".  Just remember to keep an eye on #NSNAME and #NSTYPE to be sure you are working in the namespace you should be working within.

Navigation

[0] Message Index

[#] Next page

Go to full version