PDA

View Full Version : Logging custom message on entity touch


hlstriker
04-03-2009, 06:02 PM
Is there a way for me to log a custom message when a player touches an entity?

Dexter
04-04-2009, 08:51 AM
Sure.. easiest and quickest way I can think of is


some_thing = info_ff_script:new(
{
-- your stuff here w/e blah
touchflags = {AllowFlags.kOnlyPlayers,AllowFlags.kBlue, AllowFlags.kRed, AllowFlags.kYellow, AllowFlags.kGreen}
})

function some_thing:touch( touch_ent )
if IsPlayer( touch_ent ) then
ConsoleToAll("A player touched my info script!")
end
end


Naturally that will send the message to everyone. You can cast the player if you need info from it

squeek.
04-04-2009, 09:17 AM
I think he means server logs.

Plus, ConsoleToAll() only sends the message to the server's console.

You're looking for LogLuaEvent()

SERVER LOGGING: added valve-standard LUA triggered logs. use like this:

LogLuaEvent(int victimID, int attackerID, string eventname)
LogLuaEvent(int victimID, int attackerID, string eventname, string key0, string value0)
LogLuaEvent(int victimID, int attackerID, string eventname, string key0, string value0, string key0, string value0, string key1, string value1)
LogLuaEvent(int victimID, int attackerID, string eventname, string key0, string value0, string key0, string value0, string key1, string value1, string key2, string value2)

e.g.

LogLuaEvent(0, player:GetId(), "flag_touch", "flag_name", flag:GetName())

outputs

L 09/18/2008 - 00:50:33: "AfterShock<1><STEAM_ID_LAN><#FF_TEAM_BLUE>" triggered "flag_touch" (flag_name "yellow_flag")

For world triggered stuff just called with ID 0 for both players

e.g.

LogLuaEvent(0, 0, "flag_returned", "flag_name", flag:GetName());

outputs

L 09/18/2008 - 00:58:58: World triggered "flag_returned" (flag_name "green_flag")


There are some examples in base_teamplay.lua:

LogLuaEvent(player:GetId(), 0, "flag_dropped", "flag_name", flag:GetName(), "player_origin", (string.format("%0.2f",player:GetOrigin().x) .. ", " .. string.format("%0.2f",player:GetOrigin().y) .. ", " .. string.format("%0.1f",player:GetOrigin().z) ));

Dexter
04-04-2009, 10:11 AM
ohhh. I'm retarded. Very nice and easy

hlstriker
04-04-2009, 04:51 PM
Alright, thanks :)

Also, how does the LUA know what entity in the map to use?

some_thing = info_ff_script:new(
{
-- your stuff here w/e blah
touchflags = {AllowFlags.kOnlyPlayers,AllowFlags.kBlue, AllowFlags.kRed, AllowFlags.kYellow, AllowFlags.kGreen}
})

Is this creating the entity itself or is it only creating a hook to the entity?

Crazycarl
04-05-2009, 12:49 AM
It's a hook. The entity has to be in the map already so that players can interact with it. These are usuallyan info_ff_script or trigger_ff_script, but any named trigger can be used.

hlstriker
04-06-2009, 06:03 AM
I might be missing something but I'm still confused on how to specify which entity to hook :(

squeek.
04-06-2009, 06:33 AM
I might be missing something but I'm still confused on how to specify which entity to hook :(

some_thing = info_ff_script:new({})

Will hook ALL entities named "some_thing" (even non-info_ff_script entities). You can't really discriminate between entities of the same name within lua.

EDIT: What exactly are you trying to do?

hlstriker
04-06-2009, 05:23 PM
I was thinking 'some_thing' was a variable but I guess that really doesn't make sense when I look at the prototype :S

I'm making a speed run plugin for skill maps and need to detect when a player touches the start/end timers. Currently SourceMod can't hook touches for FF so I decided to use a hackish method and use LUA to log when a player touches the entity so I can hook the log in SourceMod :D

(I'll eventually need to figure out how to add a delay between client touches so it doesn't spam the log too.)

hlstriker
04-09-2009, 06:18 AM
I got the entity touches working but now I'm having problems trying to create a delay between each touch.

You can see I'm just trying to use a simple bool array...
- When player spawns set players array index to true.
- When player touches ent, set array index to false, and AddSchedule.
- AddSchedules function resets players array index to true.

How can I do this?

-----------------------------------------------------------------------------
-- Delay for touching the timer so it doesn't constantly spam the player
-----------------------------------------------------------------------------
local bCanTouchTimer[] -- How do I make an array?

function sr_delay_reset(player)
bCanTouchTimer[player] = true
end

function player_spawn(player)
bCanTouchTimer[player] = true
end


-----------------------------------------------------------------------------
-- Start Timer
-----------------------------------------------------------------------------
sr_start_timer = info_ff_script:new({
model = "models/items/ball/ball.mdl",
touchflags = {AllowFlags.kOnlyPlayers, AllowFlags.kBlue, AllowFlags.kRed, AllowFlags.kYellow, AllowFlags.kGreen}
})

function sr_start_timer:precache( )
PrecacheModel(self.model)
end

function sr_start_timer:touch(touch_entity)
if IsPlayer(touch_entity) then
local player = CastToPlayer(touch_entity)

-- Check the delay
if bCanTouchTimer[player] then
RemoveHudItem(player, "touched_start")
AddSchedule("sr_touch_delay", 2, sr_delay_reset, player)
bCanTouchTimer[player] = false
end
end
end

squeek.
04-09-2009, 06:28 AM
Try using player:GetId() for the index of the array.

And you make an array using:
local bCanTouchTimer = {}

hlstriker
04-09-2009, 07:08 PM
Thanks it's working perfect now :)