www.ClassicTW.com http://classictw.com/ |
|
My Very First Attempt to Write a script http://classictw.com/viewtopic.php?f=15&t=34283 |
Page 2 of 2 |
Author: | Assaulter [ Wed Mar 26, 2014 10:07 pm ] |
Post subject: | Re: My Very First Attempt to Write a script |
Worked great. Thanks! |
Author: | Assaulter [ Fri Mar 28, 2014 2:05 am ] |
Post subject: | Re: My Very First Attempt to Write a script |
I have hit another road block. I have tried a few different variations to my current code with little success. I finally thought I had it but the script terminates right after attempting to collect the information. I'm sure there are lots of things wrong with it but any tips would be appreciated. Code: getWordPos CURRENTLINE $pos "Command [TL=" if ($pos <> 1) echo "**" &ANSI_14& " Must Start at Command Prompt. Exiting..." halt end :HowMany getInput $howmany "***" &ANSI_11& "How many planets would you like to Neg?" isNumber $isNumber1 $howmany if ($isNumber1 = FALSE) echo "**" &ANSI_14& "Please enter a number." goto :HowMany elseif ($howmany >= "8") echo "**" &ANSI_14& "This script only supports up to 7 planets" goto :HowMany elseif ($howmany > SECTOR.PLANETCOUNT[CURRENTSECTOR]) echo "**" &ANSI_14& "There aren't that many planets in the sector." goto :HowMany else setvar $count $howmany end setArray $planets $count setArray $used 0 setArray $eqInput 0 setArray $orgInput 0 setArray $gasInput 0 setVar $i 1 setVar $eq 1 setvar $org 1 setvar $gas 1 while ($i <= $count) and ($eq <= $count) and ($org <= $count) and ($gas <= $count) getInput $input "**" &ANSI_11& "What is the number for Planet " & $i & "?" isNumber $isNumber2 $input if ($isNumber2 = FALSE) echo "**" &ANSI_14& "Um, try putting in a number." elseif ($used[$input] = TRUE) echo "**" &ANSI_14& "That planet number isn't unique. Try again." else setVar $planets[$i] $input setVar $used[$input] TRUE end getinput $eqSell "**" &ANSI_11& "How much EQU do you want to sell? --- Enter number of holds, the number 1 for max holds, or the number 2 for none " isNumber $isNumber3 $eqSell if ($isNumber3 = FALSE) echo "**" &ANSI_14& "That wasn't a number!" elseif ($isnumber3 > "65530") echo "**" &ANSI_14& "Ehhhh... No port can afford that much product. Try a smaller number." else setvar $planets[$i][$eq] $eqSell setvar $eqInput[$eqSell] true end getinput $orgSell "**" &ANSI_11& "How much ORG do you want to sell? --- Enter number of holds, the number 1 for max holds, or the number 2 for none " isNumber $isNumber4 $orgSell if ($isNumber4 = FALSE) echo "**" &ANSI_14& "That wasn't a number!" elseif ($isnumber4 > "65530") echo "**" &ANSI_14& "Ehhhh... No port can afford that much product. Try a smaller number." else setvar $planets[$i][$org] $orgSell setvar $orgInput[$orgSell] true end getinput $gasSell "**" &ANSI_11& "How much FUEL do you want to sell? --- Enter number of holds, the number 1 for max holds, or the number 2 for none " isNumber $isNumber5 $gasSell if ($isNumber5 = FALSE) echo "**" &ANSI_14& "That wasn't a number!" elseif ($isnumber5 > "65530") echo "**" &ANSI_14& "Ehhhh... No port can afford that much product. Try a smaller number." else setvar $planets[$i][$gas] $gasSell setvar $gasInput[$gasSell] true end add $i 1 add $eq 1 add $gas 1 add $org 1 end setVar $i 1 setVar $eq 1 setVar $org 1 setVar $eq 1 while ($i <= $count) and ($eq <= $count) and ($org <= $count) and ($gas <= $count) if ($planets[$i][$eq] = 1) SetVar $_CK_PNEGO_EQUIPTOSELL "max" elseif ($planets[$i][$eq] = 2) setvar $_CK_PNEGO_EQUIPTOSELL "-1" else setvar $_CK_PNEGO_EQUIPTOSELL $planets[$i][$eq] end SaveVar $_CK_PNEGO_EQUIPTOSELL if ($planets[$i][$org] = 1) SetVar $_CK_PNEGO_ORGTOSELL "max" elseif ($planets[$i][$org] = 2) setvar $_CK_PNEGO_ORGTOSELL "-1" else setvar $_CK_PNEGO_ORGTOSELL $planets[$i][$org] end SaveVar $_CK_PNEGO_ORGTOSELL if ($planets[$i][$gas] = 1) SetVar $_CK_PNEGO_FUELTOSELL "max" elseif ($planets[$i][$gas] = 2) setvar $_CK_PNEGO_FUELTOSELL "-1" else setvar $_CK_PNEGO_FUELTOSELL $planets[$i][$gas] end SaveVar $_CK_PNEGO_FUELTOSELL send "l" $planets[$i] "*" waiton "Fuel Ore" waiton "elp) [D]" load "scripts\_ck_planet_nego.cts" waiton " --- Done with port" send "q" add $i 1 add $eq 1 add $org 1 add $gas 1 end SetVar $_CK_PNEGO_FUELTOSELL "DELETED" SetVar $_CK_PNEGO_ORGTOSELL "DELETED" SetVar $_CK_PNEGO_EQUIPTOSELL "DELETED" SaveVar $_CK_PNEGO_FUELTOSELL SaveVar $_CK_PNEGO_ORGTOSELL SaveVar $_CK_PNEGO_EQUIPTOSELL halt Thanks |
Author: | OldDragon [ Fri Mar 28, 2014 8:38 pm ] |
Post subject: | Re: My Very First Attempt to Write a script |
I can't speak much about scripting as I don't really know much about TWX beyond what I've looked at to fixed the occasional script. I did go through and add some general programming comments. Code: getWordPos CURRENTLINE $pos "Command [TL=" if ($pos <> 1) echo "**" &ANSI_14& " Must Start at Command Prompt. Exiting..." halt end :HowMany getInput $howmany "***" &ANSI_11& "How many planets would you like to Neg?" isNumber $isNumber1 $howmany if ($isNumber1 = FALSE) echo "**" &ANSI_14& "Please enter a number." goto :HowMany elseif ($howmany >= "8") # would recommend checking a range here (1 to 7) as they could put in a negative number echo "**" &ANSI_14& "This script only supports up to 7 planets" goto :HowMany elseif ($howmany > SECTOR.PLANETCOUNT[CURRENTSECTOR]) echo "**" &ANSI_14& "There aren't that many planets in the sector." goto :HowMany else setvar $count $howmany end setArray $planets $count setArray $used 0 setArray $eqInput 0 setArray $orgInput 0 setArray $gasInput 0 setVar $i 1 setVar $eq 1 # seems like these are supposed to be the indexes of the different types, probably want 1, 2 and 3 setvar $org 1 setvar $gas 1 while ($i <= $count) and ($eq <= $count) and ($org <= $count) and ($gas <= $count) # I imagine you just want to test that $i is less than $count here. getInput $input "**" &ANSI_11& "What is the number for Planet " & $i & "?" isNumber $isNumber2 $input if ($isNumber2 = FALSE) echo "**" &ANSI_14& "Um, try putting in a number." # missing a goto to get a new number elseif ($used[$input] = TRUE) echo "**" &ANSI_14& "That planet number isn't unique. Try again." # missing a goto to get a new number else setVar $planets[$i] $input setVar $used[$input] TRUE end getinput $eqSell "**" &ANSI_11& "How much EQU do you want to sell? --- Enter number of holds, the number 1 for max holds, or the number 2 for none " isNumber $isNumber3 $eqSell if ($isNumber3 = FALSE) echo "**" &ANSI_14& "That wasn't a number!" # missing a goto to get a new number elseif ($isnumber3 > "65530") # should probably be a range check 1 to 65530 echo "**" &ANSI_14& "Ehhhh... No port can afford that much product. Try a smaller number." # missing a goto to get a new number else setvar $planets[$i][$eq] $eqSell setvar $eqInput[$eqSell] true end getinput $orgSell "**" &ANSI_11& "How much ORG do you want to sell? --- Enter number of holds, the number 1 for max holds, or the number 2 for none " isNumber $isNumber4 $orgSell if ($isNumber4 = FALSE) echo "**" &ANSI_14& "That wasn't a number!" # missing a goto to get a new number elseif ($isnumber4 > "65530") # should probably be a range check 1 to 65530 echo "**" &ANSI_14& "Ehhhh... No port can afford that much product. Try a smaller number." # missing a goto to get a new number else setvar $planets[$i][$org] $orgSell setvar $orgInput[$orgSell] true end getinput $gasSell "**" &ANSI_11& "How much FUEL do you want to sell? --- Enter number of holds, the number 1 for max holds, or the number 2 for none " isNumber $isNumber5 $gasSell if ($isNumber5 = FALSE) echo "**" &ANSI_14& "That wasn't a number!" # missing a goto to get a new number elseif ($isnumber5 > "65530") echo "**" &ANSI_14& "Ehhhh... No port can afford that much product. Try a smaller number." # missing a goto to get a new number else setvar $planets[$i][$gas] $gasSell setvar $gasInput[$gasSell] true end add $i 1 add $eq 1 # assuming these are the indexes for the types, they shouldn't be incremented add $gas 1 add $org 1 end setVar $i 1 setVar $eq 1 # again, assuming these were supposed to be specific indexes, they wouldn't need to be reset here. setVar $org 1 setVar $eq 1 while ($i <= $count) and ($eq <= $count) and ($org <= $count) and ($gas <= $count) # again, these are always going to be the same as they are setup currently, so only one needs to be checked. if ($planets[$i][$eq] = 1) SetVar $_CK_PNEGO_EQUIPTOSELL "max" elseif ($planets[$i][$eq] = 2) setvar $_CK_PNEGO_EQUIPTOSELL "-1" else setvar $_CK_PNEGO_EQUIPTOSELL $planets[$i][$eq] end SaveVar $_CK_PNEGO_EQUIPTOSELL if ($planets[$i][$org] = 1) SetVar $_CK_PNEGO_ORGTOSELL "max" elseif ($planets[$i][$org] = 2) setvar $_CK_PNEGO_ORGTOSELL "-1" else setvar $_CK_PNEGO_ORGTOSELL $planets[$i][$org] end SaveVar $_CK_PNEGO_ORGTOSELL if ($planets[$i][$gas] = 1) SetVar $_CK_PNEGO_FUELTOSELL "max" elseif ($planets[$i][$gas] = 2) setvar $_CK_PNEGO_FUELTOSELL "-1" else setvar $_CK_PNEGO_FUELTOSELL $planets[$i][$gas] end SaveVar $_CK_PNEGO_FUELTOSELL send "l" $planets[$i] "*" waiton "Fuel Ore" waiton "elp) [D]" load "scripts\_ck_planet_nego.cts" waiton " --- Done with port" send "q" add $i 1 add $eq 1 # assuming these are the indexes for the types, they shouldn't be incremented add $org 1 add $gas 1 end SetVar $_CK_PNEGO_FUELTOSELL "DELETED" SetVar $_CK_PNEGO_ORGTOSELL "DELETED" SetVar $_CK_PNEGO_EQUIPTOSELL "DELETED" SaveVar $_CK_PNEGO_FUELTOSELL SaveVar $_CK_PNEGO_ORGTOSELL SaveVar $_CK_PNEGO_EQUIPTOSELL halt # this should be unnecessary |
Author: | ElderProphet [ Fri Mar 28, 2014 10:52 pm ] |
Post subject: | Re: My Very First Attempt to Write a script |
It's bad form to post your script and ask someone to fix it for you. Tips for when a script terminates prematurely... Use lots of echos to see where the script is at and what it's doing Check your while/end pairs Check your if/end pairs Move code around to simplify that section of the script Hope that helps. |
Author: | Assaulter [ Mon Mar 31, 2014 1:15 pm ] |
Post subject: | Re: My Very First Attempt to Write a script |
Thank you for your tips OldDragon. I appreciate your help a great deal. ElderProphet. Thanks for your help thus far. You have helped me quite a lot. I guess that is why I didn't realize this forum was above someone trying to learn TWX. I wasn't asking for anyone to fix it. Just tips on something I may be doing wrong. However, I have noted this and will no longer post here. Thanks |
Author: | Cruncher [ Tue Apr 01, 2014 8:24 am ] |
Post subject: | Re: My Very First Attempt to Write a script |
Assaulter wrote: ... I wasn't asking for anyone to fix it. Just tips on something I may be doing wrong... I feel your pain, I'm currently in college to learn programming. Many times I'm stuck simply asking the "right question". It's hard to ask if you aren't clear on what exactly you're looking for. I think if you try to be a little more specific, where exactly is the code hanging up at, what are you trying to accomplish? Some screen shots of errors is sometimes helpful. FYI - I know next to nothing about TWX. I'm currently learning HTML5, CSS3, Responsive Design, C#, JavaScript, ASP.Net, SQL Server. I've been banned from a developer forum for not asking "the right question". ![]() |
Author: | LoneStar [ Tue Apr 01, 2014 8:54 am ] |
Post subject: | Re: My Very First Attempt to Write a script |
Few things I always do when debugging a script: Make sure Variables are unique and not reused in multiple routnes; Narrow down the routine that's hanging or crashing, copy paste it to another txt/ts file and try to run it from there; Start to whittle down the code that 'works' until your left with the code that's broke; Make sure no other scripts are running simultaneously; Step through the code and check variables as you proceed. I use to insert: Code: Waiton #179 & "Turns" If all else fails, find another script source that achieves the same task and study it, then if necessary, walk away for a day and start over from scratch. That's the LONESTAR approved method of debugging ![]() |
Author: | Assaulter [ Tue Apr 01, 2014 11:58 am ] |
Post subject: | Re: My Very First Attempt to Write a script |
Cruncher. Yes it is hard to ask the right question. Im not sure if the code I have put together is supposed to work or not so I really don't know which part to ask about. The only thing I can do at this point is to put some stuff together that seems logical to me and just use trial and error after that. Lonestar. Thanks for the debugging tips. I will definitely use "the LONESTAR approved method of debugging" in the future. ![]() After a couple of days of running things through my head and trying to figure out what I could do differently I decided to fire up my local server and give it another test. It took off and did exactly what it was supposed to do. ![]() Thanks again for your input EDIT: After further testing it wasn't working correctly. It was selling whatever you set the fueltosell variable to of each product. I then reread OldDragon's post and followed his advice and it really does work after extensive testing. Thank you OldDragon. |
Author: | Cruncher [ Tue Apr 01, 2014 7:10 pm ] |
Post subject: | Re: My Very First Attempt to Write a script |
Good Job! Keep at it! |
Author: | Grey Gamer [ Sun Jun 19, 2022 4:55 pm ] |
Post subject: | Re: My Very First Attempt to Write a script |
ElderProphet wrote: It's bad form to post your script and ask someone to fix it for you. I was looking at my code when something happened in-game. I thought that it was an inactivity warning and SWATH sent whatever character it does to keep me in the game. My script finally proceeded--once it received the inactivity warning. I just received another, so I guess that I had better not wait around. |
Page 2 of 2 | All times are UTC - 5 hours |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |