www.ClassicTW.com
http://classictw.com/

Passing a variable in a include
http://classictw.com/viewtopic.php?f=15&t=34747
Page 1 of 1

Author:  Kaus [ Tue Nov 17, 2015 10:49 am ]
Post subject:  Passing a variable in a include

I have been unable to pass a declared variable in a include, wondering if I'm missing a trick?

While I'm on the subject do we really have to declare the root in the below example?

Where root may be different I had hoped ~/ would suffice.

e.g.
Code:
getFileList $list "twx\include" & "\*.ts"
setvar $idx 1
While ($idx <= $list)
   Include "C:\TWX\include\" & $list[$idx]
   add $idx 1
end


Returns "Script compilation error: Unable to process include file 'C:\TWX\include\" & $LIST[$IDX]', line X (TEST.TS)"

I'm considering writing the GetFileList to a cfg with pre/post pend and calling it that way, just want to make sure I'm not missing a trick to passing it natively.

Author:  Archy [ Tue Nov 17, 2015 4:30 pm ]
Post subject:  Re: Passing a variable in a include

you need to give the include command the path if the include file is not in the script folder. normally the include files are kept in the scripts\include folder so you should find that a relative address from the script folder will do, such as ..

include include\includefilename.ts.


to pass an include variable, assign it using the include file variables full address. as in ..

SetVar $Z_Lib~scriptname $scriptname

where $Z_Lib.ts is the include file name (leave out the .ts extension) and scriptname is the variable inside the include. The tilde character is necessary. The $scriptname variable is the variable being passed.

Author:  Kaus [ Wed Nov 18, 2015 10:20 am ]
Post subject:  Re: Passing a variable in a include

Archy wrote:
you need to give the include command the path if the include file is not in the script folder.


Archy, thanks for the response. What I'm trying to accomplish is passing the include filename as partially a variable in the script without the need for individual declaration. Thereby allowing flexibility for that folder and automatic creation of include file library at run-time. I could dump the getFileList to a document then read off that but I was hoping I could do so without that added step.

e.g. the above code vs.

include script\include.ts
include c:\twx\script\include.ts
include script\someotherinclude.ts

Author:  Micro [ Wed Nov 18, 2015 12:34 pm ]
Post subject:  Re: Passing a variable in a include

Since include is a compiler directive, I don't think you can pass the file name as a variable, or use it inside a loop. The script isn't actually running at compile time when the include files are added.

Author:  Archy [ Wed Nov 18, 2015 4:25 pm ]
Post subject:  Re: Passing a variable in a include

Micro wrote:
Since include is a compiler directive, I don't think you can pass the file name as a variable, or use it inside a loop. The script isn't actually running at compile time when the include files are added.


Yea I think you are right..

Sorry Kaus.. now I get what you are trying to do.. Micro is right (I think).
If you REALLY want to automate adding all include files in the folder, you might need to write a batch file to add the include commands to the end of the script before compile. You could put the actual compile command in the batch file too. (make it a 1 click process).

Author:  LoneStar [ Thu Nov 19, 2015 4:40 am ]
Post subject:  Re: Passing a variable in a include

Hi there!

You could do something similar to what my LSECT (Lonestar's Express Colonize & Tow), script does: At the end of the main script is code that sets variables in a long variable-string, and then outputs a new script file (after deleting any existing version), and then loads that Newley generated script.

Or, just bug EP, for a newer version of TWX that will allow global variables. I would probably return for that!!

Author:  Kaus [ Thu Nov 19, 2015 6:06 pm ]
Post subject:  Re: Passing a variable in a include

LoneStar wrote:
Hi there!

You could do something similar to what my LSECT (Lonestar's Express Colonize & Tow), script does: At the end of the main script is code that sets variables in a long variable-string, and then outputs a new script file (after deleting any existing version), and then loads that Newley generated script.

Or, just bug EP, for a newer version of TWX that will allow global variables. I would probably return for that!!


Hey LS,

Interesting approach, I read your TS and I like the :WriteBehind approach as a potential fix. I've tried mostly everything else so far (variations of calling it natively as well as writing to a subscript) none worked and I guess logically they shouldn't since it's a compile process (thxs micro didn't know that).

Anyways the intent here is to work in tandem with my MOM rewrite in that I would prefer that non-core bot functions are modular and therefor included vs. being standalone. With the caveat that if someone wants to add a function or improve exiting code they simply drag and drop (modify the cfg) and wala it's there at load-time. I guess I could write a similar function as yours to write the includes first first followed by the bulk of code and then reload with the correct folder includes. My concern being loop conditions.

I'll let you know how it goes, it may not be possible with the current TWX implementation natively but what fun would it be to not try :-)

Global variables would certainly make this easier.

Author:  ElderProphet [ Thu Nov 19, 2015 11:39 pm ]
Post subject:  Re: Passing a variable in a include

It's late, and I'm not following what you're unable to do. Please, restate it with examples, and I'll try to advise.

Author:  Kaus [ Fri Nov 20, 2015 12:08 pm ]
Post subject:  Re: Passing a variable in a include

ElderProphet wrote:
It's late, and I'm not following what you're unable to do. Please, restate it with examples, and I'll try to advise.


Desired State: I would like to dynamically set "Includes" within a script via variable or externally created reference using the write command.

Purpose: Include list which can grow or shrink at run-time based on folder contents.

Example:
Code:
getFileList $list "twx\include" & "\*.ts"
setvar $idx 1
While ($idx <= $list)
   Include "C:\TWX\include\" & $list[$idx]
   add $idx 1
end


Issues and attempted variations:
I can't pass a created variable as part of the include declaration. (e.g. setVar $temp "C:\")

I can't reference a file which holds the include list. (e.g. calling list.txt which houses the include list)

Building the list via variable first then rebuilding the script causes loop conditions. I haven't completely discounted this variation yet, it seems to me this is plausible if I can manipulate the text better. (to remove the loop condition)

Author:  ElderProphet [ Sat Nov 21, 2015 11:19 pm ]
Post subject:  Re: Passing a variable in a include

Ok, I copy now, thanks.

Let me unpack what the include directive does, and that may add some clarity.

Let's say you have an include file called logo.ts, and it includes one header and one variable, as follows:
Code:
setVar $myLogo "*EP haxorz TW"
:echoLogo
echo $myLogo
return
You have logo.ts saved in the include folder. Now, at the bottom of some script, you add the line: include "include\logo"
Effectively, that line is replaced with the following lines:
Code:
setVar $logo~myLogo "*EP haxors TW"
:logo~echoLogo
echo $logo~myLogo
return
So effectively, each line of the include file is copied to the calling script, and "logo~" is inserted after any : and $ symbols.

The compiled code of any running script can't be changed. You can add lines to a script and then launch it though, as discussed. I'll keep mulling it over. Global variables... for now the best bet is sector parameters, which takes several lines of code, but I'll put some thought into it during my next coding session.

In your example, you're trying to include an unknown list of scripts. Help me understand why. How can a calling script make use of include scripts that it doesn't know about? Or how can included scripts make use of other included scripts that aren't known ahead of time? What is the use case?

Author:  Vid Kid [ Sun Nov 22, 2015 8:38 pm ]
Post subject:  Re: Passing a variable in a include

I think what he is trying to do is make another way to loop an include directory , so that the main script can use those scripts as internals.

Verse the current way , placing them in sub-folder to be be called.

I do not see why this technique is not good enough.

Vid

Author:  Kaus [ Mon Nov 23, 2015 12:57 pm ]
Post subject:  Re: Passing a variable in a include

ElderProphet wrote:
In your example, you're trying to include an unknown list of scripts. Help me understand why. How can a calling script make use of include scripts that it doesn't know about? Or how can included scripts make use of other included scripts that aren't known ahead of time?


Honestly when I embarked on seeing If I could do it. I had envisioned standardizing a include list that could be called by the main script while having a modular code that I could plop into any script I was working on without the need of a page long include list.
(code reduction, less housekeeping, add-on interface)

More I just wanted to see if it was possible vs. having a set of includes (current list) that you just copy and paste into various scripts. However that last sentence in your reply really kinda framed the more significant issue for me. Even if it is possible ,writing a script which also will check for new variables and define those variables prolly isn't something I'm going to write, assuming it's even possible.

Author:  Micro [ Tue Nov 24, 2015 9:33 am ]
Post subject:  Re: Passing a variable in a include

The downside it that all of your scripts would be much larger than necessary, because the entire include file becomes part of the compiled script. It would also increase compile time, but I don't think it would impact performance significantly when running.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/