ScriptUO

Official ScriptUO EasyUO Scripts => Script Debug => Topic started by: Crisis on July 12, 2017, 09:30:32 PM

Title: FINDCOL
Post by: Crisis on July 12, 2017, 09:30:32 PM
I am trying to get this to work but not much luck. Findcol doesn't really have an example that I can use to help. http://wiki.easyuo.com/index.php?title=FindCol

I tried this sub but no luck
Code: easyuo
  1. Sub AddCraftedItems3
  2.    finditem %SB_Engrave_Tool_Color C_ , #backpackid
  3.    set %crafted ( #findcnt + %stackmoved )
  4.    Menu Font Size 10
  5.    Menu Font Color Black
  6.    menu set Crafted %crafted
  7. Return
  8. ;===================================================================

At the beginning of the Script, I set the item color like this

Code: easyuo
  1. set %SB_Engrave_Tool_color 1165
Title: Re: FINDCOL
Post by: TrailMyx on July 12, 2017, 09:38:53 PM
Here's something I'm putting together on the fly....

Code: easyuo
  1. ; finds the count of a specific colored item.  Returns one as #LOBJECTID, returns 0 if nothing is found - #LOBJECTID not touched
  2. sub FindColoredItem
  3.   namespace push
  4.   namespace local FCI
  5.   set !finditem %1
  6.   set !container %2
  7.   set !color %3
  8.   set !rval 0
  9.   finditem !finditem C_ , !container
  10.   if #FINDCNT > 0
  11.   {
  12.     for #FINDINDEX 1 #FINDCNT
  13.     {
  14.       if #FINDCOL = !color
  15.       {
  16.         set !rval !rval + 1
  17.         set #LOBJECTID #FINDID
  18.       }
  19.     }
  20.   }
  21.   set #RESULT !rval
  22.   namespace pop
  23. return #RESULT
  24.  
Title: Re: FINDCOL
Post by: The Ghost on July 13, 2017, 04:34:15 AM
This is the sub that I use to move colour items.

if the colour is 0, so no need to add it.   
Code: easyuo
  1. ; ========================== SUB RESTOCK ==============================
  2. ;
  3. ;gosub TG_Restock (Container) (ItemStock)  (Amount) (colour)
  4. ; %1 Container to restock from
  5. ; %2 Item to restock
  6. ; %3 Number of items to take
  7. ; %4 Colour Item (optional)
  8. ; Returns #FALSE if item to move is not found
  9. ; gosub TG_Restock %Resource_Secure %Wood %Fletching_Drag_Amount
  10. ; gosub TG_Restock %Resource_Secure %Sand %Sand_Drag_Amount 2413
  11. ;
  12. sub TG_Restock
  13.    menu Edit StatusBox 80 20 121 Restoking %Restock
  14.    Namespace Push
  15.    Namespace Local TG_Restock
  16.    if %0 < 4
  17.    set %4 0
  18.    finditem %2 C_ , %1
  19.    For #findindex 1 #findcnt
  20.  {
  21.        if #findkind = -1
  22.           {
  23.           NameSpace Pop
  24.           Return #FALSE
  25.           }
  26.  
  27.     if #findcol = %4 && #findType = %2
  28.        {
  29.          exevent drag #findid %3
  30.          wait !sWait
  31.          exevent dropc #backpackid
  32.          wait !sWait
  33.         }
  34.   }
  35.    Namespace Pop
  36.      wait !sWait
  37. return
  38. ; ----------------------------------------------------------------
  39.  
Title: Re: FINDCOL
Post by: BobOzarius on July 13, 2017, 11:07:24 PM
He's setting the %SB_Engrave_Tool_Color to 1165, then trying to search for the COLOR of the item with a #finditem, on the #backpackid.  He's not understanding you can't search for a color with #findid, you need to set the %SB_Engrave_Tool_Color to the color of the tool you're searching for, like you did. 1165. Then search for the #itemTYPE or #itemID of the kind of tool you are looking for. Or just use one of their options above that works. Hope that explains why your code wasn't working. #findcol is populated AFTER you do a search for any tools that match the type or id of the tool you are searching for. If it does, you loop through the #findindex until you find a tool that #findcol matches 1165, and that's the tool you "use" with set #lobject #findid. And use that object...

set %SB_Engrave_Tool_color 1165
Title: Re: FINDCOL
Post by: Crisis on July 14, 2017, 12:30:06 AM
Here's something I'm putting together on the fly....

Code: easyuo
  1. ; finds the count of a specific colored item.  Returns one as #LOBJECTID, returns 0 if nothing is found - #LOBJECTID not touched
  2. sub FindColoredItem
  3.   namespace push
  4.   namespace local FCI
  5.   set !finditem %1
  6.   set !container %2
  7.   set !color %3
  8.   set !rval 0
  9.   finditem !finditem C_ , !container
  10.   if #FINDCNT > 0
  11.   {
  12.     for #FINDINDEX 1 #FINDCNT
  13.     {
  14.       if #FINDCOL = !color
  15.       {
  16.         set !rval !rval + 1
  17.         set #LOBJECTID #FINDID
  18.       }
  19.     }
  20.   }
  21.   set #RESULT
  22.   namespace pop
  23. return #RESULT !rval
  24.  

I want to thank everyone for the replies so far. I have seen a few different ways and I keep getting stuck with it not counting anything or counting both engraving tool and scribe pen.

TM I understand yours fairly well the only thing that is stumping me is the set !rval 0

I added your sub in my script with no changes and I am calling it like this
Code: easyuo
  1. gosub FindColoredItem %Spellbook_Engraving_Tool #backpackid 1165

I need it to end up if it finds the tool in the backpack and it is colored 1165, it markes it %crafted. I tried to edit yours a little to make it %crafted but no luck. I changed return #RESULT !rval to return #RESULT %crafted  :-[

After looking at many examples, I tried this
Code: easyuo
  1. Sub AddCraftedItems3
  2.    ;set %SB_Engrave_Tool_Color 1165
  3.    finditem %1 C_ , #backpackid
  4.    For #findindex 1 #findcnt
  5.    if #findcol = 1165 && #findType = PBG
  6.    {
  7.    set %crafted ( #findcnt + %stackmoved )
  8.    }
  9.    Menu Font Size 10
  10.    Menu Font Color Black
  11.    menu set Crafted %crafted
  12. Return

However it is not judging by color and still finding both :(
Title: Re: FINDCOL
Post by: TrailMyx on July 14, 2017, 06:34:06 AM
Actually be careful with the one you posted, it looks like there's a typo that made it into your version.  It doesn't look like it's in mine. !rval just holds the count of the number of colored ones found.  I use !rval and assign that to #RESULT before I do the "namespace pop" so that #RESULT will have the value.  If you pop before you assign, then #RESULT will probably get assigned N/A.

Look at the end of the one you posted and this one to see:

Code: easyuo
  1. ; finds the count of a specific colored item.  Returns one as #LOBJECTID, returns 0 if nothing is found - #LOBJECTID not touched
  2. sub FindColoredItem
  3.   namespace push
  4.   namespace local FCI
  5.   set !finditem %1
  6.   set !container %2
  7.   set !color %3
  8.   set !rval 0
  9.   finditem !finditem C_ , !container
  10.   if #FINDCNT > 0
  11.   {
  12.     for #FINDINDEX 1 #FINDCNT
  13.     {
  14.       if #FINDCOL = !color
  15.       {
  16.         set !rval !rval + 1
  17.         set #LOBJECTID #FINDID
  18.       }
  19.     }
  20.   }
  21.   set #RESULT !rval
  22.   namespace pop
  23. return #RESULT
  24.  

But I see what you are trying to do.  You are just counting the items of a specific color that were crafted in your pack.  I was under the impression that you were looking for a specific color tool to craft with.  heh

I guess the goal here was to make sure you could see how #FINDCOL works in practice.  Looks like it's clear to you now.  :)
Title: Re: FINDCOL
Post by: Crisis on July 16, 2017, 07:54:33 PM
I am still completely stumped on this findcol thing. I am crafting spellbook engraving tools which are PBG just like the scribe tools. scribe tool color = 0 and spell book engraving tool color = 1165 My menu counts the items crafted and it counts %crafted

In the crafting part of the script, I call the sub
Code: easyuo
  1. goSub AddCraftedItems3 %Spellbook_Engraving_Tool

and the sub is
Code: easyuo
  1. Sub AddCraftedItems3
  2.    finditem %1 C_ , #backpackid
  3.    For #findindex 1 #findcnt
  4.    {
  5.     if #findType = PBG && #findcol = 1165
  6.     {
  7.     set %crafted ( #findcnt + %stackmoved )
  8.     }
  9.    }
  10.    Menu Font Size 10
  11.    Menu Font Color Black
  12.    menu set Crafted %crafted
  13. Return
It still counts both items.

I have tried yours TM, modded it a little to fit what I was doing

I call yours here
Code: easyuo
  1. gosub FindColoredItem %Spellbook_Engraving_Tool #backpackid 1165

Code: easyuo
  1. sub FindColoredItem  
  2.   namespace push
  3.   namespace local FCI
  4.   set !finditem %1
  5.   set !container %2
  6.   set !color %3
  7.   set %crafted 0
  8.   finditem !finditem C_ , !container
  9.   if #FINDCNT > 0
  10.   {
  11.     for #FINDINDEX 1 #FINDCNT
  12.     {
  13.       if #FINDCOL = !color
  14.       {
  15.         set %crafted ( #findcnt + %stackmoved )
  16.         set #LOBJECTID #FINDID
  17.       }
  18.     }
  19.   }
  20.   set #RESULT %crafted
  21.   Menu Font Size 10
  22.   Menu Font Color Black
  23.   menu set Crafted %crafted
  24.   namespace pop
  25. return #RESULT
Title: Re: FINDCOL
Post by: TrailMyx on July 16, 2017, 09:11:10 PM
Is the one you moded working that I originally wrote?

Oh, also you might want to just add one instead of #findcount.  I'm assuming the engraving tool isn't stackable.
Code: easyuo
  1. set %crafted %stackmoved + 1
  2.  
Title: Re: FINDCOL
Post by: Crisis on July 17, 2017, 05:43:45 AM
Is the one you moded working that I originally wrote?

Oh, also you might want to just add one instead of #findcount.  I'm assuming the engraving tool isn't stackable.
Code: easyuo
  1. set %crafted %stackmoved + 1
  2.  

When I modded yours so it would set it as crafted, it would work but still counted the tools as well. I would set it to craft 5, I had 1 scribe tool in my backpack, it would end up crafting 4 engraving tools, counting the scribe tool as 5 and move all 5 into the secure. Without modding yours, it would endlessly create because the menu called for %crafted to check vs the amount entered in. I always have problems getting findcol to work. I had a fairly decent crafting script for making cannon supplies started but gave up because charcoal, black powder, potash, and saltpeter all have the same findid and I had a lot of difficulty with findcol to pull the right ingredients.


When I used the %stackmoved +1, it would count every attempt whether successful or not which is why I went away from it. I wasn't sure if it would work or not. The engraving tools I believe are 100% chance so it wouldn't affect them.

Title: Re: FINDCOL
Post by: TrailMyx on July 17, 2017, 06:25:00 AM
What's the purpose of the %stackmoved variable?  I'm having a hard time making the math work in my head about what happens every time you iterate through the FOR statement and you're adding that to #FINDCNT each time.

Suppose you have 3 tools matching the color 1165 with 7 total in the pack and a stackmoved value set to 10(for example)

%crafted = 0
in the for loop:

found 1: %crafted = #findcnt + %stackmoved --> %crafted = 7 + 10 = 17
found 2: %crafted = #findcnt + %stackmoved --> %crafted = 7 + 10 = 17
found 3: %crafted = #findcnt + %stackmoved --> %crafted = 7 + 10 = 17

Don't you want it to work more like this:
%crafted = 0
in the for loop:

found 1: %crafted = %crafted + 1 --> %crafted = 0 + 1 = 1
found 2: %crafted = %crafted + 1 --> %crafted = 1 + 1 = 2
found 3: %crafted = %crafted + 1 --> %crafted = 2 + 1 = 3

%crafted = %crafted + %stackmoved --> 3 + 10 = 13

??

You probably need to put a "pause" at the beginning of your function, and setup watch points of your variables and then single step through your code to make sure it's working the way you think it should be.

Code: easyuo
  1.     sub FindColoredItem  
  2.       namespace push
  3.       namespace local FCI
  4.       set !finditem %1
  5.       set !container %2
  6.       set !color %3
  7.       set %crafted 0
  8.       finditem !finditem C_ , !container
  9.       if #FINDCNT > 0
  10.       {
  11.         for #FINDINDEX 1 #FINDCNT
  12.         {
  13.           if #FINDCOL = !color
  14.           {
  15.             set %crafted %crafted + 1
  16.             set #LOBJECTID #FINDID
  17.           }
  18.         }
  19.       }
  20.       set #RESULT %crafted + %stackmoved
  21.       Menu Font Size 10
  22.       Menu Font Color Black
  23.       menu set Crafted %crafted
  24.       namespace pop
  25.     return #RESULT
  26.  
Title: Re: FINDCOL
Post by: Crisis on July 17, 2017, 01:12:09 PM
I am the definition of newb scripter lol. I have never had any programming instruction and probably know enough to break things. Everything I know has come from dissecting other scripts trying to see what they do and how they do it. I have learned a lot from the s different subs that you wrote. I am using stacked because when I took it off it didn't work. I had used it in the past when dealing with stacked items (scrolls) and it needed it there.

I was leary about using
Code: easyuo
  1. set %crafted %crafted + 1
because when I used it before, it would count every attempt at crafting, including fails. I had my script set to craft gate travels and I would set it to craft 500 scrolls. It would count every attempt so when it was done counting 500, I actually only had like 420 scrolls.

Your change got it to work and it only counted the engraving tools and not the scribe tool so now I will have to adjust the clear pack sub to not take the scribe tool when it takes the engraving tools. I really wish UO would not use the same findid number for different items lol. As always TM, I greatly appreciate your patience and assistance!
Title: Re: FINDCOL
Post by: TrailMyx on July 17, 2017, 02:07:03 PM
Glad I could help.  I do highly recommend pausing in a sub that's giving you issues, and set up some watched variables and single-step through the code.  Most of the time the problem will be very evident when you start seeing the misbehavior happening in your watched variables in real-time.  The debugging function Cheffe included in EasyUO is actually quite useful but sadly not many take advantage of it.
Title: Re: FINDCOL
Post by: Crisis on July 17, 2017, 03:00:21 PM
I designed my clearpack sub to remove the colored engraving tools after your sub.

Code: easyuo
  1. sub ClearPack2
  2. ;gosub ClearPack2 (Color)  (Item)  (Destination)
  3.   set !color %1
  4.   finditem %2 C_ , #backpackid
  5.   if #findcnt < 1
  6.     return
  7.   for #findindex 1 #findcnt
  8.   {
  9.     if #FINDCOL = !color
  10.           {
  11.            exevent drag #findid #findstack
  12.            exevent dropc %3
  13.            wait 20
  14.           }
  15.   }
  16. return
  17.  

It seems to be working, did I do anything that looks odd? I was just guessing based off of yours. I also thought about combining this and my other clear pack to save size/lines but then I thought I may need to get every scroll color and stuff like that which would be more of a pain than an extra 10ish lines of code in an already titanic sized script lol. Any merit to that or is there a generic color code that could work if I wasn't truly worried about color?

Or can I reverse the numbers so color is %3 and if color is not used it will just use %1 to move items?
Title: Re: FINDCOL
Post by: The Ghost on July 17, 2017, 04:57:47 PM
From what I read here,look like you want to craft new tools when yo have none left and craft 5 new one.     If that the case,  I have something that work well,   it even take for a recourse container if needed.    It event use ManWinc  craft sub, should be easy to add or mod your version
Title: Re: FINDCOL
Post by: Crisis on July 17, 2017, 05:27:58 PM
From what I read here,look like you want to craft new tools when yo have none left and craft 5 new one.     If that the case,  I have something that work well,   it even take for a recourse container if needed.    It event use ManWinc  craft sub, should be easy to add or mod your version

Nah, this was crafting spellbook engraving tools which have the same itemid as the scribe tools. What was happening was the script was counting scribe tools and engraving tools as the same thing so if I set it to craft 5 and I had 2 scribe tools, it would craft 3 engraving tools and then move the engraving tools and scribe tools to the secure. I was trying to use findcol to differentiate the engraving tools from the scribe tools which TM was able to help me with.  :D
Title: Re: FINDCOL
Post by: TrailMyx on July 17, 2017, 08:49:29 PM
Heh, I'm going to nit-pick you here.  Here's pretty much exactly the same code you have, but "worded" in a way that's a bit clearer:

Code: easyuo
  1. sub ClearPack2
  2.   namespace push
  3.   namespace local CP2
  4.   set !color %1
  5.   set !item %2
  6.   set !destination %3
  7.   finditem !item C_ , #backpackid
  8.   if #findcnt > 0
  9.   {
  10.     for #findindex 1 #findcnt
  11.     {
  12.       if #FINDCOL = !color
  13.       {
  14.         exevent drag #findid #findstack
  15.         exevent dropc !destination
  16.         wait 20
  17.       }
  18.     }
  19.   }
  20.   namespace pop
  21. return
  22.  

1) I've found it's always a bit clearer to name your input arguments and list them as assignments at the beginning of a function.  It's just easier to read once you find their referneces down in the code
2) Try like mad to make sure there's only one exit from a sub.  Sometimes it's not very clear where the returns are, so just flipping your IF comparison, I managed to make the code flow from top to bottom better
3) If you really want me to help you understand the namespace push/pop and your used of !color vs. %color or something else, we can get into that.  Here's at least a starting explanation I put together some time ago:

http://www.scriptuo.com/index.php?topic=108.msg370#msg370
Title: Re: FINDCOL
Post by: Crisis on July 17, 2017, 09:34:04 PM
I appreciate the nitpicking, it makes me better.  8)

I will check out the link and get back to you. I am all for learning more, scripting can be nerve-wracking at times but I really enjoy it.
Title: Re: FINDCOL
Post by: TrailMyx on July 18, 2017, 06:55:40 AM
I appreciate the nitpicking, it makes me better.  8)

I will check out the link and get back to you. I am all for learning more, scripting can be nerve-wracking at times but I really enjoy it.

That's really the only reason why I mention it.  It seems  like you are all over the learning thing.  Also, we've observed how people learn to script over the years here and since most people don't really have a classical programming background like some of us, then it makes sense that people learn the best from code examples.  Knowing that, it's just helpful to leave your code as helpful and clear as possible.  I had no idea people would cut my stuff up and use it for their own thing, but it's kinda cool knowing that's going on. :)
Title: Re: FINDCOL
Post by: Crisis on July 18, 2017, 07:21:05 AM
As I understand things better, I can get closer and closer to writing my own subs. The sub that I was originally using was my own mishmash, I was trying to incorporate different things that I found when looking for findcol. When you showed me how to do the findcol, I tried to incorporate that into my original clear pack. Thats the good and bad thing about coding. Due to the programming rules, there are only so many ways to do something original but at the same time, because of that, people can look and see what others have done. It wasn't too long ago that I had no idea what %1 %2 etc meant but now I totally get it. Before looking at the sub you wrote for findcol, I did not know that you could set things as !color !container etc. I also understand the easyuo wiki better though many of their examples are too simplistic to help much.

My goal as I get better is to one day soon, write all subs and a script from scratch without looking at anything that anyone has done including myself. I try not to just look at something and use it, but adapt it to fit my needs and learn from it as well. I quickly found I was in over my head with a miner program so I am going to start small. My next project will be to write a new buying program to recall and buy from vendors. What I hope to do is plan out all my subs in an outline to see what I will need and then start making them one at a time from scratch and see how I can do. Then I am going to try and swap them in other working scripts that I have to test them. Once they are working, make a sub library for myself to borrow from to craft new things. I may not write a bunch of scripts that have not been done before but hopefully they will work better than older ones due to changes and people will want to use mine. I have a lot of big plans and looking forward to getting them started. I am so proud of my legendary scribe script right now because I know how much I have had to work I had to put in to trouble shoot problems, especially since it is so big. One day I may be able to use only subs from my library to make it better and shorter!! I know I am thinking ahead of my skills right now but I will get there!!
Title: Re: FINDCOL
Post by: TrailMyx on July 18, 2017, 08:30:51 AM
You'll enjoy having your own library of subs to draw from.  I find that after I got my subs up and running, throwing together a script is no big deal because I just cut/paste the subs in and write a little bit of glue-code to make it go.  Mostly then its just making the UI function correctly.  That's why I started with the basics like journal handling and data storage.
Title: Re: FINDCOL
Post by: BobOzarius on August 03, 2017, 10:51:36 PM
Eventually you'll get to a point where you never want to use other people's subs because it's annoying to read them and debug them. Using your own code is much simpler. Write some basic standard subs and start to learn the call function, and you'll be well on your way to writing your own library. I have a few files that have standard subs for literally everything, written to my standards. And I just use a military SOP type procedure to update the subs inside those files. When I find a better way, I update. Overall, your entire library will benefit and as you add more and more standard subs to the file, you'll require less and less time to create large scripts. Because most everything will already be written by you and callable from your called standard sub files.  ;)  Sorry, SOP stands for Standard Operating Procedure. That just means when you find a better way, you adopt it. It makes it easy to keep your scripts updated, because when you update one sub, you literally update every script you have in your library that calls that file.