[Question] lua guide for a proficient java programmer
[Question] lua guide for a proficient java programmer
Hello. I've been trying to wrap my head around lua and how it's applied in factorio modding for many hours, now. The only problem is, I'm a java programmer, and java is so different from lua. Can someone explain to me how lua works? Like for example, I've looked at some other mods to try and get an idea and noticed that in data.lua, there's a local variable that's set to a value of require("config"), and then later, has .DEBUG called on that variable. I was able to find a lua file called config, and there is a variable with the same name that's set to a table and then has .DEBUG set to false a few lines below that. While I understand the idea behind this, I don't understand why or how it works.
Re: [Question] lua guide for a proficient java programmer
at the bottom of config.lua, there should be a line like:
So
really means
This is a standard Lua pattern for making private member variables and functions. Everything in the table is public, everything else declared local in config.lua is private.
Code: Select all
return M
Code: Select all
local c = require("config")
Code: Select all
local c = tableM
Re: [Question] lua guide for a proficient java programmer
Thanks! That was very helpful. I'm sure I'll have more questions regarding common factorio modding practices in the future.
Re: [Question] lua guide for a proficient java programmer
There is no continue.
is one way to implement a continue in a for loop. (or while loop)
There is no goto; but using a similar structure to above can accomplish the same thing
should almost always use tostring(val) to concat to a string; which will handle true/false/nil/table pointer properly.
log( "This is something..."..tostring(game.prototypes["locomotive"]).."." );
unless you absolutely know it is a number... which will do a tostring on the number anyway.
For formatting decimal numbers I made a couple quick functions...
which take a number, a round of the very low decimal bits from it.
Not a lot of help; but I made a function to generally log the contents of a value (although most values end as 'UserData:' which this doesn't help log.
log_keys( someValues ); recusively dump all keys in a table and all the values in each of those keys.
Does work well in a data.lua for like log_keys( data.raw ) which will dump the internal table of all things.
Other than that; can't use compound assignments (x = ( a = b ) ) fails.
Code: Select all
for i=1,10 do
repeat
-- do some stuff
if( something ) break; -- a point to continue
-- do other stuff
until true
end
is one way to implement a continue in a for loop. (or while loop)
There is no goto; but using a similar structure to above can accomplish the same thing
should almost always use tostring(val) to concat to a string; which will handle true/false/nil/table pointer properly.
log( "This is something..."..tostring(game.prototypes["locomotive"]).."." );
unless you absolutely know it is a number... which will do a tostring on the number anyway.
For formatting decimal numbers I made a couple quick functions...
Code: Select all
function lowPrec( n )
if n < 0 then
return n + ( (-n) % 0.0001 )
else
return n - n % 0.0001
end
end
function lowerPrec( n )
if n < 0 then
return n + ( (-n) % 0.1 )
else
return n - n % 0.1
end
end
Not a lot of help; but I made a function to generally log the contents of a value (although most values end as 'UserData:' which this doesn't help log.
log_keys( someValues ); recusively dump all keys in a table and all the values in each of those keys.
Does work well in a data.lua for like log_keys( data.raw ) which will dump the internal table of all things.
Code: Select all
local function _log_keys(prefix,object)
for _, __ in pairs(object) do
log( prefix.._.."="..tostring(__) );
--if( type(__)=="string" or type(__)=="number" or type(__)=="function" or type(__)=="boolean" or type(__)=="nil"or type(__)=="thread") then
if( type(__)=="userdata" ) then
local meta = getmetatable(__) ;
if meta then
_log_keys( prefix.." ", getmetatable(__) );
else
log( "NIL Userdata?" );
end
elseif type(__) == "table" then
_log_keys( prefix.." ", __ );
end
end
end
local function log_keys(object)
_log_keys( "", object )
end
- nucleargen
- Long Handed Inserter
- Posts: 64
- Joined: Sat May 07, 2016 4:51 pm
- Contact:
Re: [Question] lua guide for a proficient java programmer
Very helpful might be http://lua-users.org/wiki/.
The brightest future in the center of a nuclear explosion...2003©nucleargen
Re: [Question] lua guide for a proficient java programmer
Lua knows goto, I use goto when I want continue functionality withing large loops with multiple break and continue triggers. It keeps the hierarchy flatter and more readable in large loops (~200 lines).There is no goto; but using a similar structure to above can accomplish the same thing
Code: Select all
for reqIndex, request in pairs (global.Dispatcher.Requests) do
-- ensure validity of request stop
local stopID = request.stopID
local requestStation = global.LogisticTrainStops[stopID]
if not requestStation or not (requestStation.entity and requestStation.entity.valid) then
goto skipRequestItem -- station was removed since request was generated
end
-- more code
::skipRequestItem:: -- use goto since lua doesn't know continue
end -- for global.Dispatcher.Requests
return orders
end
My Mods: mods.factorio.com
Re: [Question] lua guide for a proficient java programmer
I checked this out and saw the language comparisons, but java wasn't listed in there, only javascript. If you can find a language comparison between lua and java (or C# since it's very similar to java), let me know.nucleargen wrote:Very helpful might be http://lua-users.org/wiki/.
Re: [Question] lua guide for a proficient java programmer
Are you read Lua in 15 minutes?
http://tylerneylon.com/a/learn-lua/
http://tylerneylon.com/a/learn-lua/