Here's how I solved passing sound level (gain) from GUI to collection

Simple things like this trip up us newbies. I spent way to long trying to figure out how I can have my music volume slider on my main gui menu. Issue is you can’t address go.set or go.get properties from the collection…especially since the collection doesn’t even exist yet.

So I just write the setting to a file. Then when the collection loads I retrieve that setting. In fact, it works perfectly this way as it keeps the setting between game sessions.

--save volume setting so it can be accessed by other collections
local function load_music_volume()
	local filename = sys.get_save_file("kabocha", "musicvolume")
	local data = sys.load(filename)
	return data.volume or .5  
end

local function save_music_volume(volume)
	local filename = sys.get_save_file("kabocha", "musicvolume")
	sys.save(filename, { volume = volume })  
end
2 Likes

In future it might be worth looking at Lua modules. These can be used to store global data and functions shared between collections.

Note that you need to enable “Shared State” in game.project->script

1 Like

I considered it, but I think saving it to disk was the easiest way for this. I do need more time with LUA and defold to really optimize best practices. Thank you for the feedback.

What would I gain from using a module vice sharing it via the disk file as I need to save the volume level to disk anyway?

Efficiency and convenience. It’s normal practice to have a global pool of variables and/or functions.

The process of loading a file and extracting a single variable is quite involved “behind the scenes”. Having a global variable would be more efficienet (and easier to manage).

1 Like