PtokaX forum

PtokaX => Feature Proposals => Topic started by: CrazyGuy on 04 November, 2006, 14:52:59

Title: increasing scope of functions
Post by: CrazyGuy on 04 November, 2006, 14:52:59
This is an idea I got out of my programming in VB.
In VB it is possible to add a module in which you can put any "public" variables, classes and functions.
These things added in a module would have a scope throughout all forms and such, making them truely global.

I was wondering if something like this would be possible for LUA as well, since I often have to make the same kind of functions in every script.
It would be handier to have 1 "module", in which i can put these functions and can call upon from different scripts.
Sortof like libraries maybe, but then not externally, but created as a lua script itself.

Maybe it is possible to add an identifier to them as for example VB's Public.

An example function which would be handy to only have to make once and is used alot :

( Public ) function LoadTable(tTable, sFileName)
     local fHandle = io.open(sFileName)
     if fHandle ~= nil then
          fHandle:close()
          tTable.items = dofile(sFileName)
     end
end


I think I use that function in almost all my scripts so it would be handy if the scope of a function like that can be increased by giving it a special identifier and then used throughout all running scripts
Title: Re: increasing scope of functions
Post by: Rincewind on 04 November, 2006, 19:51:10
Surely all you need to do is to add a loadfile to each of your scripts to load the common lua file to get access to the functions you have added to it. This loadfile would in effect do the same as adding the bas module to your VB project and make available the functions within, assuming of course that you had not declared the variables in the "bas" module as local.
Title: Re: increasing scope of functions
Post by: Herodes on 04 November, 2006, 20:28:21
I believe that what Rincewind is proposing is the most viable way of doing it, because I think that each script has its own LuaState so it each script is a separate environment (in the host program, this case PtokaX). The only way that PtokaX would provide such thing could be an object (Lua table) that can be altered by the scripts. but then the checking if the functions are there would be an overkill. loadfile would be much simpler.

This idea was around before in the community. With this kind of thinking, the Scripting Utilities Central threads were started. It wasn't really responding to the community in terms of editing gathering the functions and general inspection of the progress for the utilities function file. Now that there is a wiki I suggest that we should concentrate and make our efforts on there.
Title: Re: increasing scope of functions
Post by: CrazyGuy on 10 November, 2006, 13:18:33
I havent tried to get that to work, but it does seem like that will do the trick 8)

Thnx guys  :)
Title: Re: increasing scope of functions
Post by: Rincewind on 10 November, 2006, 13:54:04
No problem. Think I'm actually going to do this myself rather than continually writing the same functions into each script  :-\
Title: Re: increasing scope of functions
Post by: bastya_elvtars on 10 November, 2006, 21:43:07
I think what you want to achieve is to have a module. Like module "CommonThings" function Something() dothings() end
and you could require "commonthings" and use CommonThings.Something().