-
Notifications
You must be signed in to change notification settings - Fork 0
Entity.Input method behavior
Эта страница доступна на Русском
I recommend using the Entity:Fire method in your code, instead of Entity:Input. Contrary to popular belief Entity:Fire also accepts the arguments activator and caller, and also has the argument delay, which is incredibly useful. The Entity:Input method fires the input instantly, before the controller initialization is completed, this can lead to unpredictable behavior. However, the controller itself internally uses Entity:Input to add outputs via AddOutput input, this is done for compatibility, no more.
local button = controller:GetMetaTarget("start_button_no")
button:Input("Press", ply, ply, nil)
button.OnPressed = function(ent, activator)
print(activator, "just pressed", ent)
end
button:Input("Press", ply, ply, nil)
button.OnPressed = function(ent, activator)
print(activator, "just activated", ent)
endIn the sample above, the Press input will be triggered even before the OnPressed output is added via AddOutput. However, the second Press should logically print just pressed. Actually prints just activated. You can see the order of calls using the console command developer 2.
(19.77) input TIMON_Z1535: start_button_no.Press()
(19.77) input map_logic_controller1: start_button_no.AddOutput(OnPressed map_logic_controller1:__OnPressed::0:-1)
(19.77) input TIMON_Z1535: start_button_no.Press()
(19.77) output: (func_button,start_button_no) -> (map_logic_controller1,__OnPressed)()
+ [MapLogic] Initialized successfully
# AcceptInput: __OnPressed Player [1][TIMON_Z1535] Entity [1306][func_button] nil
+ Player [1][TIMON_Z1535] just activated Entity [1306][func_button]We use the same sample, but replace Entity:Input with Entity:Fire. Now the Press inputs will work after initialization is completed, and there will be two outputs!
Naturally, both of them will output just activated, because we have redefined the Lua function.
(11.30) input map_logic_controller1: start_button_no.AddOutput(OnPressed map_logic_controller1:__OnPressed::0:-1)
+ [MapLogic] Initialized successfully
(11.30) input TIMON_Z1535: start_button_no.Press()
(11.30) output: (func_button,start_button_no) -> (map_logic_controller1,__OnPressed)()
(11.30) input TIMON_Z1535: start_button_no.Press()
(11.30) output: (func_button,start_button_no) -> (map_logic_controller1,__OnPressed)()
# AcceptInput: __OnPressed Player [1][TIMON_Z1535] Entity [1306][func_button] nil
+ Player [1][TIMON_Z1535] just activated Entity [1306][func_button]
# AcceptInput: __OnPressed Player [1][TIMON_Z1535] Entity [1306][func_button] nil
+ Player [1][TIMON_Z1535] just activated Entity [1306][func_button]