Jump to content

How to perform action with widget button click


Andy90
 Share

Go to solution Solved by Andy90,

Recommended Posts

This is my code but nothing happesn

 

function CreateInventory()
	inventoryUI = Widget:Panel(10, 10, 300, 500, base);
	x = 20;
	y = 20;
	local sep = 20;

	label = Widget:Label("Items:", x, y, 300, 14, inventoryUI);
	y = y + sep;

	itemList = Widget:ListBox(x, y, 260, 300, inventoryUI);
	y = y + 300 + sep;

	btnDrop = Widget:Button("Drop", x, y, 260, 26, inventoryUI);
	btnDrop.onclick = function() 
		System:Print("hello");
	end
	y = y + sep;

	inventoryUI:Hide();
end

function Script:Start()
	CreateInventory()
end

function Script:UpdateWorld()
	if (window:KeyHit(Key.I)) then
		if (inventoryUI:Hidden()) then
			inventoryUI:Show()
			mousePos = window:GetMousePosition()
			self.player.script.canMove = false
			window:ShowMouse()
		else
			inventoryUI:Hide()
			window:SetMousePosition(mousePos.x, mousePos.y)
			self.player.script.canMove = true
			window:HideMouse()
		end
	end

	while EventQueue:Peek() do
		local event = EventQueue:Wait()
		if event.source == widget then
			print("OK!")
		end
	end
end

 

Link to comment
Share on other sites

  • Solution

Okey i figured out the problem and how to fix it. It might be usefull for some other people so i post my solution. First of all i found this post 

this means you can have only one event queue. There is allready one within the main.lua. If you want process the events within multiple scripts you need to edit the event queue like this
 

Main.lua

	--Update events
	while EventQueue:Peek() do
		local event = EventQueue:Wait()
		event = gamemenu:ProcessEvent(event)
		-- Own Event Callbacks paste the following bellow
		for x = 0, world:CountEntities() - 1 do
			local entity = world:GetEntity(x);
			local entityScript = entity.script;
			if entityScript and entityScript.ProcessEvent then
				entityScript:ProcessEvent(event);
			end
		end
	end

 with this edit the main loop will iterate all entitys in your map and checks if the entity has a script attached and if the script have the "ProcessEvent" function. If this statement true it will call the ProcessEvent function within this script and sets the event message as parameter. So you can now insert this function in your scripts and handle events in diffrent scripts like this
 

Example Script attached to an entity

function Script:ProcessEvent(event)
	System:Print(event.source);
	if event.source == btnDrop then
		System:Print("Hello Button Event!");	
	end
end

 

  • Like 1
Link to comment
Share on other sites

 

I found another solution which could improve the performance when you have more than 1000 entities in your scene.
With the new solution, you won't need to iterate through all entities anymore. Instead, we'll create an EventHandler Script where we
can register widgets with callback methods. When a widget is clicked, it will trigger the specified function.

Let's start by creating the EventHandler. In this new file, we'll create a table called "EventHandler" and two functions:
"EventHandler:AddListener" function which adds a new listener to the EventHandler, and the "EventHandler:ProcessEvent" function
which gets called when an event is triggered.

1. EventHandler.Lua

EventHandler = {}

function EventHandler:AddListener(widget, callback, parent)
	listener = {
		widget = widget, 
		callback = callback,
		parent = parent
	};
	table.insert(self, listener);
end

function EventHandler:ProcessEvent(event)
	for _, listener in ipairs(self) do
		if event.source == listener.widget then
			listener.callback(event, listener.parent);
		end
	end
end

 

 

 

As you can see, the EventHandler:AddListener function takes three parameters: the widget that triggers the event, the callback function to execute when the event is triggered, and the parent script.

 

2. Implementing the EventHandler
To implement the EventHandler, you'll need to import it within the main.lua file.

 

 

 

import("Scripts/Menu.lua")
import("Scripts/EventHandler.lua") --this is our script within the main.lua

After this step, we will modify the EventQueue within the main.lua file. It should look like this afterwards:

while EventQueue:Peek() do
	local event = EventQueue:Wait()
	event = gamemenu:ProcessEvent(event)
	-- Here the Event Handler Process
	EventHandler:ProcessEvent(event);
end

 

3. Using the EventHandler for your widgets
After creating and implementing the EventHandler, it's time to use it for your widgets. Let's create a button widget and add a listener to the EventHandler.

-- Create a ui and add a listener to the EventHandler for the button click
function Script:CreateInventory()
	inventoryUI = Widget:Panel(10, 10, 300, 500, base)
	x = 20
	y = 20
	local sep = 20

	label = Widget:Label("Items:", x, y, 300, 14, inventoryUI)
	y = y + sep

	itemList = Widget:ListBox(x, y, 260, 300, inventoryUI)
	y = y + 300 + sep

	btnDrop = Widget:Button("Drop", x, y, 260, 26, inventoryUI)
  	-- add your listener for the btnDrop here.
	EventHandler:AddListener(btnDrop, DropButtonClick, self); 
	y = y + sep

	inventoryUI:Hide()
end

-- this is the function which gets called when the button get clicked. 
function DropButtonClick(event, parent)
	System:Print(parent.player:GetPosition());
end

 

 

  • Like 1
Link to comment
Share on other sites

Ah, that's fantastic! I'm eager to start working with the Ultra Engine soon. It seems like an incredibly robust platform for game development. I'm particularly impressed by the incorporation of AI features. Artificial Intelligence is undoubtedly the next frontier. Perhaps it might also be worth considering the possibility of implementing AI to assist in script creation.

During my design study, many people struggled with programming. A feature like this could be really helpful for them.

  • Like 1
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...