Page 1 of 1

[Question] lua guide for a proficient java programmer

Posted: Tue Aug 08, 2017 8:21 pm
by donoya
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

Posted: Tue Aug 08, 2017 10:29 pm
by DaveMcW
at the bottom of config.lua, there should be a line like:

Code: Select all

return M
So

Code: Select all

local c = require("config")
really means

Code: Select all

local c = tableM
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.

Re: [Question] lua guide for a proficient java programmer

Posted: Tue Aug 08, 2017 10:49 pm
by donoya
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

Posted: Wed Aug 09, 2017 1:48 am
by d3x0r
There is no continue.

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
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.

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
Other than that; can't use compound assignments (x = ( a = b ) ) fails.

Re: [Question] lua guide for a proficient java programmer

Posted: Wed Aug 09, 2017 5:53 am
by nucleargen
Very helpful might be http://lua-users.org/wiki/.

Re: [Question] lua guide for a proficient java programmer

Posted: Wed Aug 09, 2017 8:11 am
by Optera
There is no goto; but using a similar structure to above can accomplish the same thing
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).

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

Re: [Question] lua guide for a proficient java programmer

Posted: Wed Aug 09, 2017 9:01 pm
by donoya
nucleargen wrote:Very helpful might be http://lua-users.org/wiki/.
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.

Re: [Question] lua guide for a proficient java programmer

Posted: Thu Aug 10, 2017 11:52 am
by darkfrei
Are you read Lua in 15 minutes?
http://tylerneylon.com/a/learn-lua/