PDA

View Full Version : How do I reuse scripts


hdjdhj
01-22-2008, 09:48 AM
I've created some functions which I would like to be able to share with different scripts. However, I can't figure out how to include those shared scripts. I tried using "require" and there's no compile time problem but at run time, the script can't be found...do I have to explicitly upload each extra script? That seems wrong.

How can I do the equivalent of a c-preprocessor style #include to pull in the source of another script at compile time?

P.S. Below is a hacky way to provide a function called ForceDirectories that will create all needed subfolders given a single path. I've only spent about 5 minutes with the scripting language so I'm sure this isn't the best way to do it, but people might find it handy. I'm sure I could have done it with a single function but I wanted to experiment with the associative arrays.
I would argue however that ForceDirectories should be a built-in action.



-- TrueUpdate missing a lot of convenience stuff so provide it ourselves

function SplitPathIntoTable(path)
local result = {};
local oldPath = path
local newPath = ""
local p = String.Find(oldPath, "\\")
while p > 0 do
local temp = String.Left(oldPath, p)
--newPath = newPath .. temp
Table.Insert(result, Table.Count(result) + 1, temp)
--Folder.Create(newPath)
oldPath = String.Mid(oldPath, p+1, -1)
p = String.Find(oldPath, "\\")
end
-- Get the tail end, if any
if String.Length(oldPath) > 0
then Table.Insert(result, Table.Count(result) + 1, oldPath .. "\\")
end
return result
end

function ForceDirectories(path)
local tree = SplitPathIntoTable(path)
local root = ""
for i, v in tree do
root = root .. v
if i > 1 -- Don't want the C: part forced
then
Folder.Create(root) -- Sigh, no FORCE DIRECTORY
end
end
return root
end

tigran
01-22-2008, 05:17 PM
1. Create an include script (e.g. 'global_data.lua') in notepad or Indigo editor
2. Add whatever functionality you wish to include file
3. Go to Project->Includes menu in TrueUpdate, select Includes tab
4. Add your include script to the list.
5. Click OK. Now you can call any function from global_data.lua in your TrueUpdate script.

hdjdhj
01-23-2008, 09:21 AM
Well, that's the thing --- I couldn't see how to CREATE an include script from inside the Indigo editor. I could certainly create a "new" script, which would show up in another tab of the same project but that script seems to get saved in the same .tu2 (XML) file. There didn't seem to be any way to save that script by itself anywhere.

Further, it seems that that "new" script is like a procedure that one can only call in its entirety, which is not what I wanted.

If I create it in a separate editor, I don't of course get the benefit of the Indigo action editing capabilities.


1. Create an include script (e.g. 'global_data.lua') in notepad or Indigo editor
2. Add whatever functionality you wish to include file
3. Go to Project->Includes menu in TrueUpdate, select Includes tab
4. Add your include script to the list.
5. Click OK. Now you can call any function from global_data.lua in your TrueUpdate script.

Mark
01-24-2008, 09:40 AM
Hi hdjdhj,

What you can do is add your functions to a new script tab, and then "include" it using the TrueUpdate.RunScript() action. This will execute all of the script on that tab into the engine, so if there is code that is not in a function it will be executed.

Note that your new tab will only be available once the Server File has been downloaded.

Other then that you can use tigran's advice. Write your script in a tab in TrueUpdate, and then when you are done, export it to a .lua file.

tigran
01-25-2008, 11:05 AM
It's very easy. If you have the latest version of TrueUpdate, inside installed TrueUpdate (probably in your c:\Program Fiiles) you will find Indigo Rose Script Editor, the executable named IRScriptEditor.exe

You can do the following:
1. create a shortcut to the editor on your desktop
2. open the editor then either create new script or open your existing script. The editor will have all the functionality of TrueUpdate compiler (exactly what you're looking for)
3. when your script is created, you can create file association between your .lua scripts (with .lua extensions) and IRScriptEditor.exe. Next time when you double-click on the script, it will be automatically opened by Indigo Rose Script Editor.

Good luck.