Jump to content

I don't know how to process GUI responses


Joseph
 Share

Recommended Posts

I trying to create a gui inside the game using the template in main.lua and menu.lua, but when i try to process the user response, it seems like the Main.lua take the event off the eventqueue so i connot process the event into my script. I didn't understand the logic to put the processing routine of my gui into my script.

Link to comment
Share on other sites

I solved the problem with a global variable, but i don't know if it's the right way. I'm trying to create a menu that appears when a user click door and let the user choose a place to go (that is another map). So i did it that way:

1-I created the gui using Menu.lua as template

2-i wrote a code in the updatephysics of one object called computer that verifies if the user clicks the door (using camera:pick) and then shows the menu (that select the place to go)

3-i inserted a global variable in Main.lua called eventosGUI that stores the event

    while EventQueue:Peek() do
        local event
        event = EventQueue:Wait()
        eventosGUI = event
        event = gamemenu:ProcessEvent(event)
    end

4-then inside the updatephysics of the computer object, i compare eventosGUI.source with the button widget and if its true, i call changemapname = "name of the map"

But there is another problem. The cancel button that i put at the end of the menu is acting strangelly:

1-The player clicks the door

2-the menu opens normally

3- the player clicks the cancel button

4-the menu closes

5-the player click the door for the second time

6-the menu just flashes

7-the player click the door for the third time

8-the menu opens normally

So i solved this ploblem with a flag called corrigeQueue initially setted to false (but i don't know if it's the right way):

        if eventosGUI ~= nil then
...

            if eventosGUI.source == self.mudaLocalGui.botaoCancelar then
                if not self.corrigeQueue then
                    self.paineldegui = false
                    self.mouse_padrao = false
                    self.jogador.script.modoAndar = true
                    self.mudaLocalGui:Hide()
                    janela:HideMouse()
                    self.corrigeQueue = true
                else
                    self.corrigeQueue = false
                end
            end

            eventosGUI = nil

end
 

I tried to flushkeys but it doesn't solved the problem, and i put a Debug:Assert(false, tostring(eventosGUI)) before showing the menu and it returns nil, but i know that the flashing is occurring because eventosGUI ~=nil and eventosGUI.source == self.mudaLocalGui.botaoCancelar returns true

 

Link to comment
Share on other sites

  • 2 months later...

using a global variable like that will result in undefined behaviour.
make sure you process each event before the while loop ends.

you would be better off to store a pointer to mudaLocalGui as global variable,
add a process function to your script,

and call it from your event loop. (remove the pointer after closing the menu ;) )

personally i would use some kind of guimanager script, instead of processing your events in Main.lua.
example of a gui manager:

import("Scripts/MainMenu.lua")
import("Scripts/GameMap.lua")
import("Scripts/GameGUI.lua")

function BuildGUIManager(context)
	local Manager = {}
	--
	Manager.gamemenu = BuildMenu(context, Manager)
	Manager.gamemenu:Show()
	--
	Manager.gamemap = BuildGameMap(context)
	Manager.gamemap:Hide()
	--
	Manager.gameui = BuildGameGUI(context, Manager)
	Manager.gameui:Hide()
	
	------------------------------------------------
	function Manager:MenuHidden()
		return self.gamemenu:Hidden()
	end
	function Manager:ShowGameMenu()
		self.gamemenu:Show()
		self.gameui:Hide()
	end
	function Manager:HideGameMenu()
		self.gameui:Show()
		self.gamemenu:Hide()
	end
	--
	function Manager:Update()
		--
		if self.gamemenu:Hidden() == false then self.gamemenu:Update() end
		if self.gamemap:Hidden() == false then self.gamemap:Update() end
		if self.gameui:Hidden() == false then self.gameui:Update() end
		--
		while EventQueue:Peek() do
			local event = EventQueue:Wait()
			local event_processed = false
			--process menu
			if self.gamemenu:Hidden() == false then
				if self.gamemenu:ProcessEvent(event) == false then
					--exit game
					return false
				end
			end
			--process map
			if self.gamemap:Hidden() == false then
				event_processed = self.gamemap:ProcessEvent(event)
			end
			--process ui
			if event_processed == false and self.gameui:Hidden() == false then
				self.gameui:ProcessEvent(event)
			end
		end
		--
		return true
	end
	--
	
	--
	return Manager
end

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...