Page 1 of 1

What is game.player.opened [done]

Posted: Tue Sep 22, 2015 4:56 pm
by matjojo
I am working on the new version of Equivalent Exchange(EE2), I need to make a GUI when an entity(container) is opened, I found the mod Galactic Trade which does this but could not figure out how he/she does it.
Is there some Function like game.on_init to use when wanting to achieve the discussed result?

Or is there another thing I could do?

As usual I'm open for giving extra information.


matjojo

Re: function called when entity is opened

Posted: Wed Sep 23, 2015 5:06 pm
by ThaPear
The only way I can think of is to check player.opened every tick/few ticks and do things whenever it changes.

Re: function called when entity is opened

Posted: Wed Sep 23, 2015 6:27 pm
by matjojo
ThaPear wrote:The only way I can think of is to check player.opened every tick/few ticks and do things whenever it changes.
well, thanks al lot, I didn't even think of searching this on the player side of things, I'll report back oncy I have something more to say other than thanks!

Re: function called when entity is opened

Posted: Thu Sep 24, 2015 8:21 am
by matjojo
So yeah, although the idea you had probably will work I am failing at another piece now, it probably is something really small but my code:

Code: Select all

require("util")
require("defines")

--the code
game.on_event(defines.events.on_tick, function(event)
if game.player.opened == "energycondenser" then
	EEisopened(1)
end
end)

function EEisopened (a)
game.player.gui.testframe.caption = "Game detected the opening of the Energy Condenser"

end
Does not work, It give this error:
Error wrote:'5' attempt to index global 'game' A nill value
Now line 5 is the line which has game.on_event(defines.events.on_tick, function(event) on it. But, I copied this line straight outta( :D ) Galactic trade 0.6.4 beacuse for the Gui thing I already had downloaded that mod. But still, The code didn't work. Even if I copy the line exactly. What am I missing?

I will be trying to delete pieces of the galactic trade mod to see which part of the code makes this work. but as always I'm open for more info.

Re: function called when entity is opened

Posted: Thu Sep 24, 2015 9:17 am
by prg
Are you maybe doing this in data.lua? There would be no game available, try control.lua.

Re: function called when entity is opened

Posted: Thu Sep 24, 2015 3:23 pm
by matjojo
prg wrote:Are you maybe doing this in data.lua? There would be no game available, try control.lua.

nope, this is in control.lua
I just for a joke deleted control.lua from the require list in data.lua and the game booted, I do not remember reading about this anywhere, But it booted at least.

Re: function called when entity is opened

Posted: Thu Sep 24, 2015 3:29 pm
by prg
What made you think requiring control.lua from data.lua would be a good idea? It certainly doesn't belong in there.

Re: function called when entity is opened

Posted: Thu Sep 24, 2015 4:42 pm
by matjojo
prg wrote:What made you think requiring control.lua from data.lua would be a good idea? It certainly doesn't belong in there.
I never actually used custom code in EE before, So I never learned how to use control.lua good enough to know, I just assumed so.

What is game.player.opened

Posted: Fri Sep 25, 2015 6:47 pm
by matjojo
ThaPear wrote:The only way I can think of is to check player.opened every tick/few ticks and do things whenever it changes.
So, now I have this:

Code: Select all

game.on_event(defines.events.on_tick, function(event)
a = game.player.opened
if(a == "energy-condenser")
then
	game.player.print("The game detected the opening of the energy condenser")
elseif(a == nil)
then[quote][/quote]
	game.player.print("its still nil")
elseif(a == "container.energy-condenser")
then
	game.player.print("its the thing in the code")
else
	game.player.print("its something else")
end
end)
but every time I open the energy condenser it just says "its something else". And if nothing is opened (obviously) it says "its still nil" so I have tried a load of different things that game.player.opened might be, but I can't figure it out. The wiki says this:
Lua/Entity The entity that owns the GUI the player currently has open. Nil if none.
So the wiki didn't really tell much for use (for me).

my entities.lua contains this: (snippet)

Code: Select all

type = "container",
    name = "energy-condenser",
So what actually is stored in game.player.opened?

Re: What is game.player.opened

Posted: Fri Sep 25, 2015 6:49 pm
by Rseding91
The LuaEntity object that was opened.

Re: What is game.player.opened

Posted: Fri Sep 25, 2015 6:53 pm
by matjojo
Rseding91 wrote:The LuaEntity object that was opened.

yeah, but what would that be when the same of the entity opened is "energy-condenser"
note: I did try to print game.player.opened, it didn't work.

Re: What is game.player.opened

Posted: Fri Sep 25, 2015 6:53 pm
by ratchetfreak
in other words a.name == "energy-condenser" will be the true

Re: What is game.player.opened

Posted: Fri Sep 25, 2015 6:56 pm
by matjojo
ratchetfreak wrote:in other words a == "energy-condenser" will be the true

but, it does not, As said, it prints "its something else", which only gets printed if game.player.print ~= "energy-condenser" or nil.

so it is something else.

Re: What is game.player.opened

Posted: Fri Sep 25, 2015 7:26 pm
by Rseding91
matjojo wrote:
Rseding91 wrote:The LuaEntity object that was opened.

yeah, but what would that be when the same of the entity opened is "energy-condenser"
note: I did try to print game.player.opened, it didn't work.
Because it's the LuaEntity: https://forums.factorio.com/wiki/inde ... Lua/Entity

It's a reference to the entity opened.

Re: What is game.player.opened

Posted: Fri Sep 25, 2015 7:27 pm
by Klonan
you need to call the 'if' on not the entity, but the entities name

so in your case you would go

Code: Select all

if a.name =="energy-condenser" then game.players[1]print("Found condensor")

Re: What is game.player.opened

Posted: Fri Sep 25, 2015 9:50 pm
by ratchetfreak
matjojo wrote:
ratchetfreak wrote:in other words a == "energy-condenser" will be the true

but, it does not, As said, it prints "its something else", which only gets printed if game.player.print ~= "energy-condenser" or nil.

so it is something else.
sorry I forgot to add the ".name" after pasting

Re: What is game.player.opened

Posted: Sun Sep 27, 2015 5:52 pm
by matjojo
ratchetfreak wrote:
matjojo wrote:
ratchetfreak wrote:in other words a == "energy-condenser" will be the true

but, it does not, As said, it prints "its something else", which only gets printed if game.player.print ~= "energy-condenser" or nil.

so it is something else.
sorry I forgot to add the ".name" after pasting
that worked, be sure never to check "if a.name == nil" that will break the game. but just checking "if a.name == "energy-condenser"" does work. (even if it is nil)