I do not fully understand your code. Can you help a little bit please?
- So, first I’ve made GUI.go with GUI.gui and attached GUI.gui_script to it. All files are in /main/GUI. Added GUI.go to main.collection. Am I right that I don’t need to change anything in this file? (Except of hash(“lClick”) to hash(“LMB”))
GUI.gui_script:
local buttons = 0
local listButtons = {}
local listBoxes = {}
local listOwners = {}
local thisBtn = {}
function on_message(self, message_id, message, sender)
if message_id == hash("Make_button") then
print("ORDERED BUTTON .............................")
make_button(sender,message[1],message[2],message[3],message[4],message[5])
end
end
function on_input(self, action_id, action)
if action_id == hash("LMB") then
for i=1,buttons,1 do
if listBoxes[i] ~= nil and listBoxes[i] ~= Deleted then
local size = gui.get_size(listBoxes[i])
thisPos = gui.get_position(listBoxes[i])
cX = thisPos.x - (size.x / 2)
cX_ = thisPos.x + (size.x / 2)
cY = thisPos.y - (size.y / 2)
cY_ = thisPos.y + (size.y / 2)
if action.x >= cX and action.x <= cX_ then
if action.y >= cY and action.y <= cY_ then
gui.delete_node(listButtons[i])
gui.delete_node(listBoxes[i])
listButtons[i] = nil
listBoxes[i] = nil
thisBtn[1] = i
msg.post(listOwners[i], "Button_press", thisBtn)
print("CLICKED: " .. tostring(listOwners[i]) .. "/Button: " .. i)
end
end
end
end
end
end
function make_button(owner,text,chars,font,pos,bound) -- Owner, Text, (Size of each character in pixels), pos, size of button in vmath.vector3() format.
print(tostring(owner) .. " ORDERED BUTTON")
buttons = buttons + 1
if bound == nil then
bound = vmath.vector3(0,0,0)
bound.x = chars * (string.len(text) + 10)
bound.y = 4 * chars
end
buttonBox = gui.new_box_node(pos,bound)
button = gui.new_text_node(pos,text)
set_anchor_gui({buttonBox,button},pos)
gui.set_color(buttonBox,vmath.vector4(.7,.7,.7,1))
gui.set_color(button,vmath.vector4(1,1,1,1))
listBoxes[buttons] = buttonBox
listButtons[buttons] = button
listOwners[buttons] = owner
gui.set_font(button,font)
local tab = {buttons}
msg.post(owner, "Made_button", tab)
print("MADE BUTTON: " .. buttons)
end
- Created on_button_message.lua in /main/GUI
on_button_message.lua:
function on_button_message(message_id, message, btnHash, thisBtnNums)
if message_id == hash("Made_button") then
btnHash = btnHash + 1
thisBtnNums[btnHash] = message[1]
return "made"
elseif message_id == hash("Button_press") then
return "press"
end
end
- My turret.script is attached to every turret.go in main.collection. Script has id = behavior_TurretReactor. For example url of second turret is /turret2#behavior_TurretReactor
I’ve added to turret.script the following:
require "/main/GUI/on_button_message"
Little question here. Should not I specify path here something like:
require "main.GUI.on_button_message"
Or both are right?
Next:
local btnHash = 0
local thisBtnNums = {}
function on_message(self, message_id, message, sender)
if on_button_message(message_id,message,btnHash,thisBtnNums) == "made" then
elseif on_button_message(message_id,message,btnHash,thisBtnNums) == "press" then
if message[1] == thisBtnNums[1] then -- This would correlate to the order of the created buttons. Upgrade is thisBtnNums[1], On/Off is [2], Special is [3]
self.state = self.state + 1
elseif message[1] == thisBtnNums[2] then
-- .... Etc.
end
end
end
function update(self, dt)
if message_id == hash("upgrade") == true and once == false then
once = true
list = {"Upgrade", GETfont_size(font), font,vmath.vector3(100,300,0), nil}
msg.post("GUI#GUI", "Make_button", list) -- [] = wherever your UI script is with the Make_button() function.
list = {"On/Off", GETfont_size(font), font,vmath.vector3(100,200,0), nil}
msg.post("[]", "Make_button", list)
list = {"Special", GETfont_size(font), font,vmath.vector3(100,100,0), nil}
msg.post("[]", "Make_button", list)
end
end
So, what I need to fix to make all this stuff working? Tried some approaches, but it seems that I miss something. Thanks!
UPD. Also, it would be really helpful to have some minimum working prototype from you Like britzl’s extensions. Your code can be really useful for others in future, but without working example it’s a bit hard to implement it (if one’s knowledge of Lua is limited).