• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

[SOLVED] Namespace of the LUA scripts

Status
Not open for further replies.

precompiled

New member
Hello,

I've just started playing around with Eluna and have two smaller questions so far, would be nice to get some help over here :)

1) localization of the scripts: I read on the FAQ in the forum, that it's best practice to "local'ize" the vars and functions via either just local em or putting em in a local table. So I expected not localized variables/functions to be global. But when I use CREATURE_EVENT_SMTH in my script (which I defined in eluna_globals.lua) it just derefences to nil.
Does this mean the scripts are in their respective local .. namespace (or however its called in LUA) or am I doin smth wrong?

2) coding style: Is there any "official" coding style? As the scripts have a kinda ... random chosen coding style (at least it doenst change inside a script as far as I can tell)

Thanks in advance!

P.S. What's that rule about deactivating my adBlock Oo
 
Last edited:

Rochet2

Moderator / Eluna Dev
Are you actually using eluna_globals.lua?
If yes then did you require it?

The lua scripts are loaded in a random order (not guaranteed to be ordered by file path and file name even though it might be like that atm)
This means that eluna_globals.lua might load AFTER your own script and thus anything defined in eluna_globals.lua will be nil for your script.

You can fix that problem by doing require("eluna_globals") at the top of your script to load it before your script.


And yes. There is currently only one lua state. Any global variable will be accessible from any script.
You should not do var = 123 because this will not be deleted (freed) unless you explicitly "delete" it by doing var = nil and doing so can create collision with other scripts as some other script can use
the same variable name, effectively overwriting your variable or you overwriting someone else's variable.

There is no code standard, at least any made by the Eluna developers and contributors.
However to give some kind of reference, see here: http://lua-users.org/wiki/LuaStyleGuide



btw. all script files can be thought of as functions. These functions are run when the server starts up. They are run in random order unless you use require to force something to load before something else.
Each function is run only once unless coded otherwise.
So if within a normal function you can do:

Code:
function f()
    a = "hello"
end

f()
print(a)

Then this works with a script as well so that script A has the content
Code:
a = "hello"

and script B has the content
Code:
require("A")
print(a)

Not sure if that is easier or harder to understand, but that is basically how it works and how I imagine it.
 

precompiled

New member
Ah okay, yeah totally understandable. Do you require the scripts by just the name or the full/relative path? Like can you just require("eluna_globals") from any script in any subdirectory (then the scripts must have different file names right?)?

I didn't require it so far, but I read that you can enforce pre-loading with using the .ext file extension. Did that and the script appears before the script I tested it with (in the mangosd console output). Or did I misunderstood smth with the ext feature here? :)

Btw: Thanks for the fast and detailed answer!

Edit: yeah the require did the trick, thanks a lot! :)
 
Last edited:

Rochet2

Moderator / Eluna Dev
Ah okay, yeah totally understandable. Do you require the scripts by just the name or the full/relative path? Like can you just require("eluna_globals") from any script in any subdirectory (then the scripts must have different file names right?)?

Yes. Eluna requires the files in lua_scripts to have unique names. If you have two files with same name, only one is loaded and an error is logged telling you to rename either one.

require() works as defined here: http://www.lua.org/manual/5.2/manual.html#pdf-require
Eluna appends the whole lua_scripts folder tree into package.path, not including hidden folders
Then the scripts are loaded by, in a way, calling require("filenamewithoutextension") on each script file that is in the folder tree, unless the file is hidden.
The files that have .ext ending are handled as any other lua script except that they run before any other normal lua script.

This means that when you use require("filenamewithoutextension") you will not load the script twice even if the engine already loaded it because the engine also uses require() to load all the files.
However .. the thing about require() is that it will cache the return value of the script or "true" in a map with the path passed to require as the key.
What this practically means is that if you have the following folder structure:
Code:
lua_scripts
    myFolder
        myScript.lua
Then you can require the script multiple times by using require("myscript"); require("myscript.lua"); require("myFolder/myscript") and so on.


So as a summary:
You should always require your files with require("filenamewithoutextension") if they are within the lua_scripts folder and are not hidden or you will load the script twice.
You can hide your files and folders to not load them.
You can use any paths you want with require, as long as you keep the first point in mind.
Your scripts must have unique names if they are loaded by the engine automatically.

I guess that should cover about everything you need to know about require() in eluna.


It is possible that this behavior changes at some point.
Some things that I myself have considered have been removing the package.path appending and using the relative path for loading the scripts with the file extension to allow loading scripts with same name and different extensions
and another one would be to remove .ext so that using require() is required
and one could be that lua_scripts is divided into two parts from which one is loaded automatically and the other you have to require/run manually/by script.


I didn't require it so far, but I read that you can enforce pre-loading with using the .ext file extension. Did that and the script appears before the script I tested it with (in the mangosd console output). Or did I misunderstood smth with the ext feature here? :)

it is quite unclear what you did here. You added .ext to which file exactly? And what was the result with it? Did it not work as you expected/read?
 
Last edited:

precompiled

New member
it is quite unclear what you did here. You added .ext to which file exactly? And what was the result with it? Did it not work as you expected/read?

Sorry for being unclear :) I tried to enforce loading of the eluna_gloabs.lua by renaming it to eluna_globals.ext, which seemed to work first (the console output of the cMaNGOS server listed the eluna_globals file before any other lua script using variables defined in the eluna_globals script), but I still got the nil reference error^^
I refactored my scripts now, so that every script has a `require("eluna_globals")` in one of the first lines. That works fine, Thanks again! :)

If you have the time and passion feel free to have a look at the scripts (its just a fork of ur Scripts repo: https://github.com/sattelite/ElunaScripts ).
 
Status
Not open for further replies.
Top