Hello everyone! I have a simple question. I have an inventory window and three buttons on a GUI. I would like to change the graphic of the buttons when player clicks on an inventory item, so that the button looks the same as the inventory item itself (to have the same sprite). Can anyone suggest the best way to do this, so my script won't become a mess? Thank you!
Do you need help determining when player clicked on item, or changing image?
function on_mouse_click(MouseButton btn)
{
<... probably some other code...>
if (btn == eMouseLeftInv)
{
InventoryItem *item = Inventory.GetAtScreenXY(mouse.x, mouse.y);
MyButton.Graphic = item.Graphic;
}
<... probably some other code...>
}
E: I noticed you said "click on item" not "select item", so I changed the code.
Thanks a lot!
function MouseInventory (MouseButton btn) {
if (btn == eMouseLeftInv) {
InventoryItem *item = InventoryItem.GetAtScreenXY (mouse.x, mouse.y);
if (btnWord1.Graphic == 11) btnWord1.NormalGraphic = item.Graphic;
else if (btnWord2.Graphic == 11) btnWord2.NormalGraphic = item.Graphic;
else if (btnWord3.Graphic == 11) btnWord3.NormalGraphic = item.Graphic;
}
}
So far I have this, but nothing is happening. :undecided: Any idea why?
try adding this line for debug purpose
function MouseInventory (MouseButton btn) {
if (btn == eMouseLeftInv) {
InventoryItem *item = InventoryItem.GetAtScreenXY (mouse.x, mouse.y);
if (btnWord1.Graphic == 11) btnWord1.NormalGraphic = item.Graphic;
else if (btnWord2.Graphic == 11) btnWord2.NormalGraphic = item.Graphic;
else if (btnWord3.Graphic == 11) btnWord3.NormalGraphic = item.Graphic;
else Display ("Oops... none of my if is true!"); // Line for debugging
}
}
Thanks. I've already tried adding a few messages like that all over the code. Not one of them fires up.
This is the whole code:
function on_mouse_click(MouseButton button) // called when a mouse button is clicked. button is either LEFT or RIGHT
{
if (IsGamePaused() == 1) // Game is paused, so do nothing (ie. don't allow mouse click)
{}
else if (button == eMouseLeft) {
ProcessClick(mouse.x,mouse.y, mouse.Mode);
}
else if (button == eMouseLeftInv) {
InventoryItem *item = InventoryItem.GetAtScreenXY (mouse.x, mouse.y);
if (btnWord1.Graphic == 11) btnWord1.NormalGraphic = item.Graphic;
else if (btnWord2.Graphic == 11) btnWord2.NormalGraphic = item.Graphic;
else if (btnWord3.Graphic == 11) btnWord3.NormalGraphic = item.Graphic;
}
else // right-click, so cycle cursor
{
mouse.SelectNextMode();
}
}
Before I made a new function. This time I put it under mouse click script. Still, it doesn't work. Inventory window opens and mouse mode is set to interact. When I click on inventory item, the cursor just changes to the image of the item as usual but no change in button graphic.
Go to General Settings and make sure that "Process inventory clicks in script" is True.
Thanks Crimson, had to change the "Override built-in inventory window click handling" in General settings. Youre the best.
Oh, right, its "override" one, I forgot how it is called exactly :).