Handy Function to get base game name

Post Reply
User avatar
sirrobzeroone
Member
Posts: 593
Joined: Mon Jul 16, 2018 07:56
GitHub: sirrobzeroone
Contact:

Handy Function to get base game name

by sirrobzeroone » Post

I couldn't see an inbuilt way to get the base game_name which I wanted to more realiably check what the underlying game base was rather than looking for a core mod or registered node

I wrote a small function which retrieves the game name from the world.mt file, this works with standard secure settings in place and also works on first/initial load. Function will always return a value, currently set to "unknown" if name can't be found...which should only happen if the world.mt file structure changes significantly - but that could be changed to return nil or false:

Code: Select all

local function game_name()
	local world = minetest.get_worldpath()
	local file = io.open(world.."/world.mt", "r")
	local game_name = "unknown"
	  for line in file:lines() do		
			if string.find(line, "gameid") then
				game_name = string.match(line, "= (.*)")
				file:close()
				break
			end		
	  end
	return game_name
end

local name_of_game = game_name()
I expect there might be an easier way to do this, so I'm very happy to take suggestions and improvements :)

Just putting this up in case someone else needs something similar in the future.

User avatar
Mantar
Member
Posts: 582
Joined: Thu Oct 05, 2017 18:46
Contact:

Re: Handy Function to get base game name

by Mantar » Post

Clever!
Lead dev of Exile, git repo: https://codeberg.org/Mantar/Exile

User avatar
Linuxdirk
Member
Posts: 3216
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: Handy Function to get base game name

by Linuxdirk » Post

Since world.mt is a key-value storage you can use it with the settings API.

Code: Select all

local game_id = Settings(minetest.get_worldpath()..DIR_DELIM..'world.mt'):get('gameid')

User avatar
sirrobzeroone
Member
Posts: 593
Joined: Mon Jul 16, 2018 07:56
GitHub: sirrobzeroone
Contact:

Re: Handy Function to get base game name

by sirrobzeroone » Post

Linuxdirk wrote:
Mon Oct 18, 2021 06:58
Since world.mt is a key-value storage you can use it with the settings API.

Code: Select all

local game_id = Settings(minetest.get_worldpath()..DIR_DELIM..'world.mt'):get('gameid')
Thanks Linuxdirk, much cleaner :)

User avatar
Linuxdirk
Member
Posts: 3216
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: Handy Function to get base game name

by Linuxdirk » Post

sirrobzeroone wrote:
Fri Oct 22, 2021 11:24
Thanks Linuxdirk, much cleaner :)
Sure, you're welcome. A lot of Minetest files are key-value storages ... so the settings API is the way to go :)

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests