increasing scope of functions
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

increasing scope of functions

Started by CrazyGuy, 04 November, 2006, 14:52:59

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

CrazyGuy

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

Rincewind

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.

Herodes

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.

CrazyGuy

I havent tried to get that to work, but it does seem like that will do the trick 8)

Thnx guys  :)

Rincewind

No problem. Think I'm actually going to do this myself rather than continually writing the same functions into each script  :-\

bastya_elvtars

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().
Everything could have been anything else and it would have just as much meaning.

SMF spam blocked by CleanTalk