View unanswered posts | View active topics It is currently Thu Mar 28, 2024 3:39 am



Reply to topic  [ 9 posts ] 
 Help with includes and gosub 
Author Message
Sergeant

Joined: Mon Jun 08, 2020 4:32 pm
Posts: 7
Unread post Help with includes and gosub
Hi all,

Let me preface this with I'm a novice at scripting.

I was hoping someone could assist with a script I'm trying to get working for moving a planet.

I'm getting stumped when it comes to includes/gosub.

The first script is a simple test script to call a sub routine in the include. If i put a return in the include I get an error message "Script run-time error in PLANETMOVER': Return without gosub, line 70, cmd 57"

But if I don't have a return, it doesn't complete the main script.

I've tried picking apart other scripts and they don't seem to exihibit this behavior so it must be something I'm doing wrong and just can't pinpoint what.

Test Script
Code:
#Script to test planetMover


GetInput $PlanetMover~PlanetID "Which Planet are we moving?"
Getinput $PlanetMover~Nsector "Which Sector do you want to move to?"

include "include\planetmover"


# check if we can run it from here
cutText CURRENTLINE $location 1 14
   if ($location = "Planet command") or ($Location = "Command [TL=00")
      Goto :Proceed
   Else
      ClientMessage "This Script must be run from a Planet or Command Prompt"
      Goto :End
   End
:Proceed
   GoSub $PlanetMover~Proceed
   IF ($planetMover~Pmoved = 1)
      SetVar $Message 1
      ClientMessage "Planet is now in " & $PlanetMover~$Nsector
   Else
      ClientMessage "What went wong"
   End
   
:End

include "include\planetmover"


Include Script
Code:
#Planet Mover script
#
# SUB:      :Pmove
# Purpose:  Lands on designated planet and warps to designated sector
# Passed:   $PlanetID = Planet Number of planet to move
# Passed:   $Nsector = Sector to warp planet to
# Returned: $PMoved - 1=Successful 2=Move not Successful

# check if we can run it from here

#   SetVar $Planetid 32
#   SetVar $Nsector
   
cutText CURRENTLINE $location 1 14
   if ($location = "Planet command") or ($Location = "Command [TL=00")
      Goto :Proceed
   Else
      SetVar $Pmoved 2
      ClientMessage "You must be at a Planet or Command Prompt"
      Halt
   End

:Proceed
   
   If ($location = "Command [TL=00")
      Send "L" & $PlanetID & "*"
      Waitfor "Planet command"
      Send "C"
   Else
      Send "C"
      Waitfor "Citadel command"
   End

:Warptime
   Send "P"
   Waitfor "What sector do you want to warp this planet to?"
   Send $Nsector "*"
   SetTextTrigger Warpok :WarpOk "Locating beam pinpointed,"
   SetTextTrigger Warpbad :Warpbad "You do not have any fighters in"
   Pause
   


:Warpbad
   SetVar $PMoved 2
   ClientMessage "No Fighters in destination"
   KillTrigger Warpok
   KillTrigger Warpbad
   if ($location = "Planet command")
   Send "Q"
   Else
   Send "Q Q"
   End   
   Halt
   
:WarpOk
   Send "Y"
   KillTrigger Warpok
   KillTrigger Warpbad
   SetTextTrigger Done :Done "Planet is now in sector "
   Pause

:Done
   if ($location = "Planet command")
   Send "Q"
   Else
   Send "Q Q"
   End
   SetVar $PMoved 1
return   


Any help would be appreciated.

Thanks


Wed May 05, 2021 9:55 am
Profile
Lieutenant J.G.

Joined: Mon Dec 01, 2014 5:39 pm
Posts: 440
Unread post Re: Help with includes and gosub
Ok, the program logic goes top down. It will enter a "sub" without a gosub, and enter a include, without calling it directly.

So by having the first include right after the two GetInput's, will mean it runs those first two getInputs, then enters the include. Where no doubt it hits a

:sub

return

and hence the error.

So move all includes to the bottom. Only have one call of the same include, looks like you have two.

Then after your main logic put a halt

i.e. generic flow of a stand alone script

SET VAR STUFF
MAIN LOOP/LOGIC
halt <-- this stops the script going further
GOSUB Functions (i.e. :subname return)
INCLUDES

all includes would usually be in format
:SUBNAME

return





Code:
#Script to test planetMover


GetInput $PlanetMover~PlanetID "Which Planet are we moving?"
Getinput $PlanetMover~Nsector "Which Sector do you want to move to?"


# check if we can run it from here
## May not matter - but I'd either use the getword  to get first word and see if it equals "Planet" or "Command"
## better still use quikstats! Micro also added a internal quikstats command to more recent TWX release.

cutText CURRENTLINE $location 1 14

   if ($location = "Planet command") or ($Location = "Command [TL=00")
      Goto :Proceed
   Else
      ClientMessage "This Script must be run from a Planet or Command Prompt"
      Goto :End
   End
   

:Proceed
   GoSub $PlanetMover~Proceed
   IF ($planetMover~Pmoved = 1)
      SetVar $Message 1
      ClientMessage "Planet is now in " & $PlanetMover~$Nsector
   Else
      ClientMessage "What went wong"
   End
   
:End
   
halt
# (program ends here) else it'd continue into the include file and do random stuff

include "include\planetmover"


Thu May 06, 2021 7:28 am
Profile
Sergeant

Joined: Mon Jun 08, 2020 4:32 pm
Posts: 7
Unread post Re: Help with includes and gosub
That makes sense. Thank you for the education!

This will help with a few other self-inflicted failures I've attempted.


Thu May 06, 2021 2:34 pm
Profile
Lieutenant J.G.

Joined: Mon Dec 01, 2014 5:39 pm
Posts: 440
Unread post Re: Help with includes and gosub
Dalad wrote:
That makes sense. Thank you for the education!

This will help with a few other self-inflicted failures I've attempted.


Yeah, I taught myself to script too a long time ago, with zero clue, no background etc, I feel the pain.

"you don't know, what you don't know" is so true when it comes to programming when you start at a base of nothing.

Good luck!

One more thing: Do you have other peoples scripts other than the standard scripts that come with TWX? they are great scripts, well written, lots of includes etc. However, sometimes it's easier to just find peoples "one file" scripts. This may teach bad habits like not using includes for code reusage, however, having everything in the same .ts file makes it easier to understand the concepts of programming/scripting.

At this point you are learning to program, not learning to be a snobby elitist programming <insert expletive here> . So start small, and work up.
Fairly sure the newer TWX has heaps of scripts included, else there is a sticky post from Helix which is titled something like "All the resources/scripts I have". It's got 100's if not 1000's of scripts, lots of single file examples.


Thu May 06, 2021 8:00 pm
Profile
Sergeant

Joined: Mon Jun 08, 2020 4:32 pm
Posts: 7
Unread post Re: Help with includes and gosub
I think I have version 2.6 of twxproxy which came with quite a few besides the pack1/pack2 which I've used and tried to pick apart. I've also downloaded several of the collections from this forum.

Thanks for the advice, much appreciated!


Thu May 06, 2021 9:18 pm
Profile
Sergeant

Joined: Mon Jun 08, 2020 4:32 pm
Posts: 7
Unread post Re: Help with includes and gosub
Ok, I must have missed something in the lesson.

The modified script gives the following error: Script run-time error in 'PMOVETEST.TS': Bad goto label '0', line 18, cmd 27

Pmovetest.ts
Code:
#Script to test planetMover


GetInput $PlanetMover~PlanetID "Which Planet are we moving?"
Getinput $PlanetMover~Nsector "Which Sector do you want to move to?"



# check if we can run it from here
cutText CURRENTLINE $location 1 14
   if ($location = "Planet command") or ($Location = "Command [TL=00")
      Goto :Proceed
   Else
      ClientMessage "This Script must be run from a Planet or Command Prompt"
      Goto :End
   End
:Proceed
   GoSub $PlanetMover~Proceed
   IF ($planetMover~Pmoved = 1)
      SetVar $Message 1
      ClientMessage "Planet is now in " & $PlanetMover~$Nsector
   Else
      ClientMessage "What went wong"
   End
   
:End
Halt

include "include\planetmover"


PlanetMover.ts
Code:
#Planet Mover script
#
# SUB:      :Pmove
# Purpose:  Lands on designated planet and warps to designated sector
# Passed:   $PlanetID = Planet Number of planet to move
# Passed:   $Nsector = Sector to warp planet to
# Returned: $PMoved - 1=Successful 2=Move not Successful

# check if we can run it from here

#   SetVar $Planetid 32
#   SetVar $Nsector
   
cutText CURRENTLINE $location 1 14
   if ($location = "Planet command") or ($Location = "Command [TL=00")
      Goto :Proceed
   Else
      SetVar $Pmoved 2
      ClientMessage "You must be at a Planet or Command Prompt"
      Halt
   End

:Proceed
   
   If ($location = "Command [TL=00")
      Send "L" & $PlanetID & "*"
      Waitfor "Planet command"
      Send "C"
   Else
      Send "C"
      Waitfor "Citadel command"
   End

:Warptime
   Send "P"
   Waitfor "What sector do you want to warp this planet to?"
   Send $Nsector "*"
   SetTextTrigger Warpok :WarpOk "Locating beam pinpointed,"
   SetTextTrigger Warpbad :Warpbad "You do not have any fighters in"
   Pause
   


:Warpbad
   SetVar $PMoved 2
   ClientMessage "No Fighters in destination"
   KillTrigger Warpok
   KillTrigger Warpbad
   if ($location = "Planet command")
   Send "Q"
   Else
   Send "Q Q"
   End   
Return
   
:WarpOk
   Send "Y"
   KillTrigger Warpok
   KillTrigger Warpbad
   SetTextTrigger Done :Done "Planet is now in sector "
   Pause

:Done
   if ($location = "Planet command")
   Send "Q"
   Else
   Send "Q Q"
   End
   SetVar $PMoved 1
Return   


Fri May 07, 2021 3:08 pm
Profile
Lieutenant J.G.

Joined: Mon Dec 01, 2014 5:39 pm
Posts: 440
Unread post Re: Help with includes and gosub
Code:
GoSub $PlanetMover~Proceed

#should be

GoSub :PlanetMover~Proceed

First one references a VAR second references a LABEL

Also, just as a note, in most languages words like "END" would be reserved, meaning it'd throw a compile error. TWX doesn't do this, but it's just confusing and potenially if you made a typo you'd get difficult to find bugs.

i.e. goto :end

or worse imagine reading something like this


Code:
if (1 = 2)
   gosub :else
else
   gosub :if
end

halt
:else
# do else stuff

return
:if

return


It's really bad for you/other people reading your scripts, which isn't your intention, and I'm not being snotty, but as someone who has ALL the bad programming habits (just ask anyone who's read my scripts haha), this one I even agree is best to start with good habits :)

I mean, your using includes, I didn't use a include till my scripts starting hitting 2-3000 lines :)


Fri May 07, 2021 5:28 pm
Profile
Sergeant

Joined: Mon Jun 08, 2020 4:32 pm
Posts: 7
Unread post Re: Help with includes and gosub
stumped by my own lack of reading comprehension :)

And once again, Thanks! for helping me understand


Fri May 07, 2021 9:28 pm
Profile
Lieutenant J.G.

Joined: Mon Dec 01, 2014 5:39 pm
Posts: 440
Unread post Re: Help with includes and gosub
Dalad wrote:
stumped by my own lack of reading comprehension :)

And once again, Thanks! for helping me understand


hit up one of the TW Discords also, plenty of helpful people there, i mean, we'll all probably end up in some long winded, and pointless argument on how things should be done.

Why not start a fire with a innocent question? :)


Mon May 10, 2021 10:23 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.