-
Notifications
You must be signed in to change notification settings - Fork 0
How to use MetaTarget
Эта страница доступна на Русском
GM:OnMapLogicInitialized(Entity controller, string mapName)
Entity controller
- The controller entity map_logic_controller that calls this hook.
string mapName
- The map name for which the controller is trying to initialize the logic.
Use a unique name of the hook and file to avoid conflicts with other addons. It is best to enter part of your map name .
Always check mapName to prevent your script create script errors on other maps.
The mapName may differ from the actual current map name if you set the console variable map_logic_override.
Create a new script in the autorun/server folder, add the OnMapLogicInitialized hook.
In fact, this code can be run from anywhere on the server side.
local function Init(controller, mapName)
-- always check mapName to prevent script errors on other maps
if mapName ~= "rp_pb_industrial17_v2" then
return
end
local button1 = controller:GetMetaTarget("start_button_no")
button1.OnPressed = function(ent, activator)
print(activator, "just pressed", ent)
end
button1:Fire("Press") -- for test only
local testActivator = button1[1] -- direct access to the entity
-- if entity has no name, we can pass it directly
local button2 = controller:GetMetaTarget(ents.GetMapCreatedEntity(2591))
button2.OnPressed = function(ent, activator)
print(activator, "just pressed", ent)
end
button2:Fire("Press", nil, 1, testActivator, testActivator) -- delay 1 sec, activator is previous button
end
hook.Add("OnMapLogicInitialized", "pb_v2_tests", Init)This example will print
+ [NULL Entity] just pressed Entity [1306][func_button]
+ Entity [1306][func_button] just pressed Entity [1292][func_button]Pressing the buttons from the script is not useful, it is done to show how the controller works.
button1 and button2 are MetaTarget objects, not entities.
When we call a method, MetaTarget tries to call it on the entities that it contains, excluding numbers.
A numeric key is getting an entity by its ordinal number.
When we try to assign a new variable to MetaTarget, it perceives this as an attempt to create a new output.
Therefore, the code is button1.OnPressed = function actually creates an output for nested entities.
Method button1:Fire tells the engine to call an input, but this input will be called after the initialization.
Therefore, it does not matter to us whether it is before or after the assignment of outputs.
Please refrain from Entity:Input - Entity.Input method behavior.