[SOLVED]Is there an event when I click a machine?
[SOLVED]Is there an event when I click a machine?
Title is my question.
I'm finding the names and descriptions of the events difficult to understand in some cases.
When I click on an assembling machine to open the crafting menu, does an event trigger?
And is there one when I close a machine's crafting menu?
I'm finding the names and descriptions of the events difficult to understand in some cases.
When I click on an assembling machine to open the crafting menu, does an event trigger?
And is there one when I close a machine's crafting menu?
Last edited by DRY411S on Thu Jun 02, 2016 3:49 pm, edited 2 times in total.
Re: Is there an event that triggers when I click on a placed mac
No, and no.
If you want to get ahold of me I'm almost always on Discord.
Re: [SOLVED]Is there an event when I click a machine?
Well that's 'solved' that question thank you. 

Re: [SOLVED]Is there an event when I click a machine?
you could always write a custom event.
I am new to modding myself but it is possible that you could look for the open inventory in control.lua
I think you would use get_inventory(inventory)
if it doesn't equal nil then you have an opened machine.
I would try something like this
something like this should work for what you want.I haven't tested this code at all, but it should get you started with custom event handlers
I am new to modding myself but it is possible that you could look for the open inventory in control.lua
I think you would use get_inventory(inventory)
Code: Select all
inventory.assembling_machine_input
inventory.assembling_machine_output
inventory.assembling_machine_modules
I would try something like this
Code: Select all
events = defines.events
events.machine_opened = script.generate_event_name()
script.on_init(function()
if global.open == nil then
global.open = false;
end
end)
script.on_event({
events.on_tick
}, function(event)
openinv = game.player.get_inventory(defines.inventory.assembling_machine_input);
if openinv ~= nil and global.open == false then
global.open = true;
game.raise_event(events.machine_opened, {
input = openinv.get_contents(),
})
elseif game.player.get_inventory(inventory.assembling_machine_input) == nil and global.open == true then
global.open = false;
end
end)
script.on_event({
events.machine_opened
}, function(event)
--Here goes the code for what you wish to happen when the event fires
end)
-- create an interface for other mods to access raised event.
remote.add_interface("machine_opened", {
event = function()
return events.machine_opened
end
})
Re: [SOLVED]Is there an event when I click a machine?
That's great thank you. If I check this on 'tick', and decide there's something I need to do, I have less than 1/60 second to execute my code, is that right?
Re: [SOLVED]Is there an event when I click a machine?
Here is working code to do what you wish to do.
This will raise the custom event when ever an assembly machine is opened
and no not really, your code can execute however it wants but if you use heavy code you may see performance drops. which is why its best to use custom events like this.
EDIT: this will not work from a save game and has to be done from a new game. I believe there is other events you need to add with on_init to make it declare global.open from a save and whatnot
This will raise the custom event when ever an assembly machine is opened
Code: Select all
require "defines"
require "util"
events = defines.events
events.machine_opened = script.generate_event_name()
script.on_init(function()
if global.open == nil then
global.open = false;
end
end)
function is_machine(entity)
return entity.type == 'assembling-machine'
end
script.on_event({
events.on_tick
}, function(event)
openinv = game.player.opened
if openinv == nil then
global.open = false;
elseif openinv ~= nil and is_machine(openinv) and global.open == false then
global.open = true;
game.raise_event(events.machine_opened, {
entity = openinv,
})
end
end)
script.on_event({
events.machine_opened
}, function(event)
game.player.print("Code you wish to run when ever an assembler is opened")
end)
-- create an interface for other mods to access raised event.
remote.add_interface("machine_opened", {
event = function()
return events.machine_opened
end
})
EDIT: this will not work from a save game and has to be done from a new game. I believe there is other events you need to add with on_init to make it declare global.open from a save and whatnot
Re: [SOLVED]Is there an event when I click a machine?
I think I understand how this works, and many, many thanks for it.
I have one question. If I want this code to work for more than one machine type I should put a test for each of those machine types in the 'is_machine' function. Is that correct?
And if there are several names within the type and I want to test those, I should test that too.
EDIT: I've answered my own question above by trying some code, and confirming that I'm correct. I really don't know why I don't do that more often.
Could you suggest an event that could be 'trapped' when the machine's menu is closed again please?
EDIT: I've answered that question too by trying some code
I have one question. If I want this code to work for more than one machine type I should put a test for each of those machine types in the 'is_machine' function. Is that correct?
And if there are several names within the type and I want to test those, I should test that too.
EDIT: I've answered my own question above by trying some code, and confirming that I'm correct. I really don't know why I don't do that more often.

Could you suggest an event that could be 'trapped' when the machine's menu is closed again please?
EDIT: I've answered that question too by trying some code
Re: [SOLVED]Is there an event when I click a machine?
nice to hear you have gotten it all working with some trial and error, good luck with the mod you are creating
Re: [SOLVED]Is there an event when I click a machine?
Oh. It was my understanding that the inventories could be accessed at any time, not just when the inventory was open. Otherwise how do you tell e.g. when an item has been inserted into the machine?
Re: [SOLVED]Is there an event when I click a machine?
Sure, recipes can be accessed at any time from crafting menus, as long as the recipes are enabled.doc wrote:Oh. It was my understanding that the inventories could be accessed at any time, not just when the inventory was open. Otherwise how do you tell e.g. when an item has been inserted into the machine?
Because a mod I'm writing to recycle things produces a reverse recipe for everything that can be made in an assembly machine (i.e. anything that isn't made in a miner/extractor, refinery, chemical plant or furnace), it is almost doubling the size of the crafting menus when the player takes a look at what is in their 'pockets'.
With this code, I can enable my reverse recipes when a player opens a recycle machine 'assembly' menu, then disable them when they close that menu. That way, they never appear in the player's menu.
That's a long way of saying that I am building the crafting menu dynamically when they open my machine, then removing it when they close the machine.
I got it working. The issue is that with things like all bob's mods installed, and all research complete, it's taking more than 5 seconds to open the menu, and then more than 5 seconds to close it.

Re: [SOLVED]Is there an event when I click a machine?
Sorry, you misunderstood my question.DRY411S wrote:Sure, recipes can be accessed at any time from crafting menus, as long as the recipes are enabled.doc wrote:Oh. It was my understanding that the inventories could be accessed at any time, not just when the inventory was open. Otherwise how do you tell e.g. when an item has been inserted into the machine?
I'm surprised that the inventory of the assembler (not the recipes) can only be accessed (from scripts) when the GUI is open. Chests certainly don't work like that, you can access their inventory at any time, not just when the chest is open. Luckily chests have their own flag to tell you when they are open.
This has unfortunate ramifications for a mod I'm making where I'm trying to give a custom assembly machine special behaviour depending on the items it contains, I don't want the user to have to open the GUI for this to work. It seems like a questionable API design if you can't get the assembler contents normally...
Re: [SOLVED]Is there an event when I click a machine?
Ah I understand. My mod is not concerned with the inventory, it's the crafting menu I'm bothered about. Perhaps in terms of inventory, they work like you describe for chests?