[Question] lua guide for a proficient java programmer

Place to get help with not working mods / modding interface.
Post Reply
donoya
Fast Inserter
Fast Inserter
Posts: 119
Joined: Thu Dec 04, 2014 10:55 pm
Contact:

[Question] lua guide for a proficient java programmer

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

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

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

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

donoya
Fast Inserter
Fast Inserter
Posts: 119
Joined: Thu Dec 04, 2014 10:55 pm
Contact:

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

Post by donoya »

Thanks! That was very helpful. I'm sure I'll have more questions regarding common factorio modding practices in the future.

d3x0r
Filter Inserter
Filter Inserter
Posts: 316
Joined: Sun Jun 04, 2017 8:56 am
Contact:

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

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

User avatar
nucleargen
Long Handed Inserter
Long Handed Inserter
Posts: 64
Joined: Sat May 07, 2016 4:51 pm
Contact:

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

Post by nucleargen »

Very helpful might be http://lua-users.org/wiki/.
The brightest future in the center of a nuclear explosion...2003©nucleargen

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

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

Post 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

donoya
Fast Inserter
Fast Inserter
Posts: 119
Joined: Thu Dec 04, 2014 10:55 pm
Contact:

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

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


Post Reply

Return to “Modding help”