I’m doing something very similar to this with the default GUI. The player gets to choose a weapon upgrade after leveling up. I store the upgrades in a module and randomly select ones that are available.
For showing/hiding gui nodes, I use scale. Not sure if it’s the best way, but it works for me.
Example
local function hide_weapon_options(self)
for i = 1, 3 do
gui.set_scale(self.effect_option[i], vmath.vector3())
end
end
To show the weapon options
local function show_weapon_options(self)
-- Make sure table is reset
self.selection = {}
-- Prepare copy of weapon index
local index_copy = {}
-- Add available weapons to index copy, weapons are stored in a module
for i = 1, #weapons.index do
if weapons.index[i].available then
index_copy[i] = weapons.index[i]
end
end
-- Randomly select 3 weapons
for i = 1, max_selection do
local random = math.random(1, #index_copy)
self.selection[i] = index_copy[random]
-- Remove weapon from copy to avoid duplicates
table.remove(index_copy, random)
-- Store the name when picking it later
local hash = self.selection[i].hash
-- Info for the gui nodes
local id, effect, description = weapons.get_effects(hash)
-- To use later for input selection
self.selection[i].id = id
-- print(effect, description)
gui.set_text(gui.get_node("effect" .. i), effect)
gui.set_text(gui.get_node("description" .. i), description)
gui.set_scale(self.effect_option[i], vmath.vector3(1,1,1))
end
end
Selecting an upgrade
local function on_touch_release(self, action)
for i = 1, #self.effect_option do
if gui.pick_node(self.effect_option[i], action.x, action.y) then
add_weapon_effect(self, i)
end
end
end
function on_input(self, action_id, action)
if action_id == TOUCH and action.released then
on_touch_release(self, action)
end
end