Event for playable character near player


BakaSchwarz

Recommended Posts

So i want to implement a system, where the my character only loses sanity at night, when he/she is alone.

I figured that i should maybe look for an event which tells me if there are any players nearby. Does such an event exist?
If yes, is it good practice to use said event for this usecase or is there is more suitable solution?

 

Thank you in advance!

Link to comment
Share on other sites

First off, this is the DS modding forum. For DST, you want to go to the DST modding forum.

 

As for how to detect nearby players... there's a playerproximity component that you maybe could attach to your character. Or you periodically fire TheSim:FindEntities(inst.Transform:GetWorldPosition(),4,{"player"}) to get all players within 4 (a ground turf) range.

Link to comment
Share on other sites

First off, this is the DS modding forum. For DST, you want to go to the DST modding forum.

 

As for how to detect nearby players... there's a playerproximity component that you maybe could attach to your character. Or you periodically fire TheSim:FindEntities(inst.Transform:GetWorldPosition(),4,{"player"}) to get all players within 4 (a ground turf) range.

 

First, thank you for your reply and you are right. I am sorry for that. Can i somehow move this thread?

 

The playerprox component would be perfect, but it also detects the player himself. 

 

I tried using the SetOnPlayerNear function, which calls a function whenever a player is near my player.

 

That it also triggers from my player wouldn't be a problem if it didn't block the function entirely.

 

From what i see the SetOnPlayerNear function only gets called once when a player is near which is of course my player and causes it to never trigger again.

 

I'll try the periodic call for now and come back here later.

Link to comment
Share on other sites


Okay, when i use your provided example i get:


[string "../mods/glace/scripts/prefabs/glace.lua"]:68: bad argument #2 to 'FindEntities' (number expected, got table)LUA ERROR stack traceback:=[C]:-1 in (method) FindEntities (C) <-1--1>../mods/glace/scripts/prefabs/glace.lua:68 in (field) fn (Lua) <67-69>scripts/scheduler.lua:187 in (method) OnTick (Lua) <161-217>   self =      running = table: 0x98d5eb0      waitingfortick = table: 0x98d5ed8      tasks = table: 0x98d5e68      waking = table: 0x22456870      attime = table: 0x98d5f00      hibernating = table: 0x98d5fa0   tick = 60   k = PERIODIC 109034: 2.000000   v = true   already_dead = nilscripts/scheduler.lua:398 in (global) RunScheduler (Lua) <396-404>   tick = 60scripts/update.lua:162 in () ? (Lua) <146-219>   dt = 0.033333335071802   tick = 60   i = 60 [string "../mods/glace/scripts/prefabs/glace.lua"]:68: bad argument #2 to 'FindEntities' (number expected, got table)LUA ERROR stack traceback:    =[C]:-1 in (method) FindEntities (C) <-1--1>    ../mods/glace/scripts/prefabs/glace.lua:68 in (field) fn (Lua) <67-69>    scripts/scheduler.lua:187 in (method) OnTick (Lua) <161-217>    scripts/scheduler.lua:398 in (global) RunScheduler (Lua) <396-404>    scripts/update.lua:162 in () ? (Lua) <146-219> SCRIPT ERROR! Showing error screen

The code that i used is the following:

local common_postinit = function(inst)   inst:AddTag("fridge")  inst.MiniMapEntity:SetIcon( "glace.tex" )  inst:DoPeriodicTask(2, function()    print(TheSim:FindEntities(inst.Transform:GetWorldPosition(), 4 ,{"player"}))  end)end
Link to comment
Share on other sites

Okay, i solved my problem with this code:

inst:DoPeriodicTask(2, function()  local x, y, z = inst.Transform:GetWorldPosition()  local ents = TheSim:FindEntities(x, y, z, 4 ,{"player"})  if table.getn(ents) > 1 then    inst.components.sanity.night_drain_mult = 0    inst.components.sanity.current = inst.components.sanity.current + 5  else    inst.components.sanity.night_drain_mult = 2.5  endend)

 Again, thank you for pointing me in the right direction!

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.