Jump to content

Recommended Posts

With Don't Starve we can use:
 

GetPlayer().components.playercontroller:ShakeCamera(inst, "FULL", 0.2, 0.02, .25, 40)

But in DST how to use this?

I'm starting to learn how to create MOD yesterday. And I'm not good at English.
So hope everyone will understand what I said :D.

Thanks in advance.
 

 

Link to comment
https://forums.kleientertainment.com/forums/topic/56318-how-to-use-shakecamera/
Share on other sites

All player prefabs have

local function ShakeCamera(inst, mode, duration, speed, scale, source, maxDist)player.ShakeCamera = ShakeCamera

So for example,

-- source inst = deerclops entityfor k, v in pairs(AllPlayers) do   v:ShakeCamera(CAMERASHAKE.SIDE, .5, .05, .1, inst, 40)end

To reference concrete players you will need to get the concrete player via the targeting of whatever you are managing.

 

A player can reference himself via using ThePlayer, instead of GetPlayer.

All player prefabs have

local function ShakeCamera(inst, mode, duration, speed, scale, source, maxDist)player.ShakeCamera = ShakeCamera

So for example,

-- source inst = deerclops entityfor k, v in pairs(AllPlayers) do   v:ShakeCamera(CAMERASHAKE.SIDE, .5, .05, .1, inst, 40)end

To reference concrete players you will need to get the concrete player via the targeting of whatever you are managing.

 

A player can reference himself via using ThePlayer, instead of GetPlayer.

 

I'm put the code to modmain and error: "CAMERASHAKE.SIDE nil value".

And I try to take the code from "camerashake.lua" then put to my modmain and new error come:

 

    local SIDE =    {        --------------------------------------------------------------------------        -- 0   S   1        --------------------------------------------------------------------------        GLOBAL.Vector3(-1, 0):GetNormalized(),        GLOBAL.Vector3( 1, 0):GetNormalized(),    },
COROUTINE 100363 SCRIPT CRASH:[string "scripts/prefabs/player_common.lua"]:1065: calling 'set_local' on bad self (number expected, got table)LUA ERROR stack traceback:        =[C] in function 'set_local'        scripts/prefabs/player_common.lua(1065,1) in function 'ShakeCamera'        ../mods/Auto Planting DST/modmain.lua(77,1)	[00:00:36]: COROUTINE 100363 SCRIPT CRASH:[string "scripts/prefabs/player_common.lua"]:1065: calling 'set_local' on bad self (number expected, got table)LUA ERROR stack traceback:        =[C] in function 'set_local'        scripts/prefabs/player_common.lua(1065,1) in function 'ShakeCamera'        ../mods/Auto Planting DST/modmain.lua(77,1)[00:00:36]: COROUTINE 100363 SCRIPT CRASH:[string "scripts/prefabs/player_common.lua"]:1065: calling 'set_local' on bad self (number expected, got table)LUA ERROR stack traceback:        =[C] in function 'set_local'        scripts/prefabs/player_common.lua(1065,1) in function 'ShakeCamera'        ../mods/Auto Planting DST/modmain.lua(77,1)	[00:00:36]: SCRIPT ERROR! Showing error screen	[00:00:52]: Force aborting...

And line 77 mine is:

GLOBAL.ThePlayer:ShakeCamera(SIDE, .5, .05, .1, inst, 40)

 

Warning.

That will only shake the host if the code is ran server side.

 

ThePlayer will only reference clients if ran client side.

(e.g using a netvar and making the client shake his screen based on the change)

 

Thanks you for warning. But I don't  know how to use netvar :( .

That's why I used

local inst = GLOBAL.SpawnPrefab("deerclops") -- example, entity is sourcefor k, v in pairs(GLOBAL.AllPlayers) do   v:ShakeCamera(CAMERASHAKE.SIDE, .5, .05, .1, inst, 40)end

So you loop through AllPlayers, pick each player entity v by v, and shake their cameras.

 

Else, you have to get the other player entities via context.

Or via a function that returns the closest player to the entity that shakes the screen.

 

For example, the spear has a function

local function onunequip(inst, owner)    owner.AnimState:Hide("ARM_carry")    owner.AnimState:Show("ARM_normal")end

where inst is the spear, and owner is the player.

 

So you put owner:ShakeCamera.

That's why I used

local inst = GLOBAL.SpawnPrefab("deerclops") -- example, entity is sourcefor k, v in pairs(GLOBAL.AllPlayers) do   v:ShakeCamera(CAMERASHAKE.SIDE, .5, .05, .1, inst, 40)end

So you loop through AllPlayers, pick each player entity v by v, and shake their cameras.

 

Else, you have to get the other player entities via context.

Or via a function that returns the closest player to the entity that shakes the screen.

 

For example, the spear has a function

local function onunequip(inst, owner)    owner.AnimState:Hide("ARM_carry")    owner.AnimState:Show("ARM_normal")end

where inst is the spear, and owner is the player.

 

So you put owner:ShakeCamera.

 

One more question. I want to Shake camera all player are in range so what function to get distance from inst to player.

Thanks you. :)

Well, to check for distances you have

for k, v in pairs(GLOBAL.AllPlayers) do	-- v is a player entity	-- inst is a source entity, another thing, like a deerclops	-- distance is within 5	if v:GetDistanceSqToInst(inst) < (5 * 5) then		v:ShakeCamera(GLOBAL.CAMERASHAKE.SIDE, .5, .05, .1, inst, 40)	endend

GetDistanceSqToInst gives the distance squared. So you either give another squared distance to compare or use math.sqrt.

 

Still, pay attention

local function ShakeCamera(inst, mode, duration, speed, scale, source, maxDist)
, maxDist)

that's the parameter to adjust the max distance.

Well, to check for distances you have

for k, v in pairs(GLOBAL.AllPlayers) do	-- v is a player entity	-- inst is a source entity, another thing, like a deerclops	-- distance is within 5	if v:GetDistanceSqToInst(inst) < (5 * 5) then		v:ShakeCamera(GLOBAL.CAMERASHAKE.SIDE, .5, .05, .1, inst, 40)	endend

GetDistanceSqToInst gives the distance squared. So you either give another squared distance to compare or use math.sqrt.

 

Still, pay attention

local function ShakeCamera(inst, mode, duration, speed, scale, source, maxDist)
, maxDist)

that's the parameter to adjust the max distance.

 

I have the original code like this:

GLOBAL.ThePlayer:ShakeCamera(GLOBAL.CAMERASHAKE.FULL, 0.2, 0.02, .25, inst, 40)

So now I use this:

for k, v in pairs(GLOBAL.AllPlayers) do	-- v is a player entity	-- inst is a source entity, another thing, like a deerclops	-- distance is within 5	if v:GetDistanceSqToInst(inst) < (40 * 40) then            v:ShakeCamera(GLOBAL.CAMERASHAKE.FULL, 0.2, 0.02, .25, inst, 40)	endend

It's mean all player in range 40 will shake camera right?

Edited by zUsername

This is the function of player_common:

local function ShakeCamera(inst, mode, duration, speed, scale, source, maxDist)    if source ~= nil and maxDist ~= nil then        local distSq = inst:GetDistanceSqToInst(source)        local k = math.max(0, math.min(1, distSq / (maxDist * maxDist)))        scale = easing.outQuad(k, scale, -scale, 1)    end    --normalize for net_byte    duration = math.floor((duration >= 16 and 16 or duration) * 16 + .5) - 1    speed = math.floor((speed >= 1 and 1 or speed) * 256 + .5) - 1    scale = math.floor((scale >= 8 and 8 or scale) * 32 + .5) - 1    if scale > 0 and speed > 0 and duration > 0 then        if TheWorld.ismastersim then            --Forces a netvar to be dirty regardless of value            inst.player_classified.camerashakemode:set_local(mode)            inst.player_classified.camerashakemode:set(mode)            --            inst.player_classified.camerashaketime:set(duration)            inst.player_classified.camerashakespeed:set(speed)            inst.player_classified.camerashakescale:set(scale)        end        if inst.HUD ~= nil then            TheCamera:Shake(                mode,                (duration + 1) / 16,                (speed + 1) / 256,                (scale + 1) / 32            )        end    endend

If there is a source and maxDist, then the camera shake will be more or less intense depending on distance.

e.g Deerclops stomps when you are near or away.

In that case, you write:

for k, v in pairs(GLOBAL.AllPlayers) do	v:ShakeCamera(GLOBAL.CAMERASHAKE.FULL, 0.2, 0.02, 0.25, inst, 40)end

If you want to check for the 40 distance and shake all cameras equally despite the distance, then we remove the source and maxDistance, but we have to check with another function, thus:

for k, v in pairs(GLOBAL.AllPlayers) do	if v:GetDistanceSqToInst(inst) < (40 * 40) then		v:ShakeCamera(GLOBAL.CAMERASHAKE.FULL, 0.2, 0.02, 0.25)	endend
Edited by DarkXero

 

This is the function of player_common:

local function ShakeCamera(inst, mode, duration, speed, scale, source, maxDist)    if source ~= nil and maxDist ~= nil then        local distSq = inst:GetDistanceSqToInst(source)        local k = math.max(0, math.min(1, distSq / (maxDist * maxDist)))        scale = easing.outQuad(k, scale, -scale, 1)    end    --normalize for net_byte    duration = math.floor((duration >= 16 and 16 or duration) * 16 + .5) - 1    speed = math.floor((speed >= 1 and 1 or speed) * 256 + .5) - 1    scale = math.floor((scale >= 8 and 8 or scale) * 32 + .5) - 1    if scale > 0 and speed > 0 and duration > 0 then        if TheWorld.ismastersim then            --Forces a netvar to be dirty regardless of value            inst.player_classified.camerashakemode:set_local(mode)            inst.player_classified.camerashakemode:set(mode)            --            inst.player_classified.camerashaketime:set(duration)            inst.player_classified.camerashakespeed:set(speed)            inst.player_classified.camerashakescale:set(scale)        end        if inst.HUD ~= nil then            TheCamera:Shake(                mode,                (duration + 1) / 16,                (speed + 1) / 256,                (scale + 1) / 32            )        end    endend

If there is a source and maxDist, then the camera shake will be more or less intense depending on distance.

e.g Deerclops stomps when you are near or away.

In that case, you write:

for k, v in pairs(GLOBAL.AllPlayers) do	v:ShakeCamera(GLOBAL.CAMERASHAKE.FULL, 0.2, 0.02, 0.25, inst, 40)end

If you want to check for the 40 distance and shake all cameras equally despite the distance, then we remove the source and maxDistance, but we have to check with another function, thus:

for k, v in pairs(GLOBAL.AllPlayers) do	if v:GetDistanceSqToInst(inst) < (40 * 40) then		v:ShakeCamera(GLOBAL.CAMERASHAKE.FULL, 0.2, 0.02, 0.25)	endend

 

Wow. Really really thanks you for your details explanation.

BIG THANKS sending to you <3.

 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...