Enumerate "game" object via LUA?

Place to post guides, observations, things related to modding that are not mods themselves.
Plawerth
Fast Inserter
Fast Inserter
Posts: 120
Joined: Thu Mar 02, 2017 12:57 am
Contact:

Enumerate "game" object via LUA?

Post by Plawerth »

There is no good place for this question. This isn't really modding but more about learning LUA and poking around at stuff.

Is there some way to query the "game" object to list all of its functions? I am trying to get a list of things like "game.player", "game.surface", etc.

Apparently it's a table but using pairs() doesn't do anything.

Code: Select all

/c 
local count = 0
for _ in pairs(game) do
    count = count + 1
    game.player.print("Table entry: " .. _)
end
game.player.print("Total entries: " .. count)
/c local count = 0 for _ in pairs(game) do count = count + 1 game.player.print("Table entry: " .. _) end game.player.print("Total entries: " .. count)

Output:
Table entry: __self
Total entries: 1


So, that's not the right way to do this, if it is even possible..
osldgoth
Long Handed Inserter
Long Handed Inserter
Posts: 93
Joined: Thu Feb 26, 2015 3:52 am
Contact:

Re: Enumerate "game" object via LUA?

Post by osldgoth »

quyxkh
Smart Inserter
Smart Inserter
Posts: 1031
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Enumerate "game" object via LUA?

Post by quyxkh »

Code: Select all

/c game.print(game.help())
or if you run it from a command line you can print the help to your terminal with

Code: Select all

/c print(game.help())
where it'll preserve line breaks and you've got the scrollback buffer and all

edit: p.s. Pure lua objects are more convenient for lua accessors, e.g. they allow native lua introspection and all, but garbage-collected structures that would have to be duplicated and synchronized in every mod's lua context (let alone the map globals) aren't an efficient-enough way to run a game engine. Lots of game-engine data is accessible read-only in `game`, for instance if game was provided as pure lua data, synchronizing game.tick and all the rest for every mod would be a nightmare. You could provide the individual items as bare userdata but that has its own problems.
Post Reply

Return to “Modding discussion”