Fredgerd Posted November 24, 2013 Share Posted November 24, 2013 I've seen some mods do it for season or weather with if GetSeasonManager() and GetSeasonManager(): Link to comment https://forums.kleientertainment.com/forums/topic/29945-how-would-i-do-an-if-for-time-of-day-day-dusk-night/ Share on other sites More sharing options...
debugman18 Posted November 24, 2013 Share Posted November 24, 2013 (edited) I've seen some mods do it for season or weather with if GetSeasonManager() and GetSeasonManager():if GetClock().phase == "day" then print "Example"end Edited November 24, 2013 by debugman18 Link to comment https://forums.kleientertainment.com/forums/topic/29945-how-would-i-do-an-if-for-time-of-day-day-dusk-night/#findComment-376856 Share on other sites More sharing options...
Fredgerd Posted November 24, 2013 Author Share Posted November 24, 2013 (edited) Thanks! Is the phase for dusk "dusk" or something else? Edited November 24, 2013 by Fredgerd Link to comment https://forums.kleientertainment.com/forums/topic/29945-how-would-i-do-an-if-for-time-of-day-day-dusk-night/#findComment-376863 Share on other sites More sharing options...
seronis Posted November 24, 2013 Share Posted November 24, 2013 Thanks! Is the phase for dusk "dusk" or something else?dont_starve/data/scripts/components/clock.lua seems to imply you are correct Link to comment https://forums.kleientertainment.com/forums/topic/29945-how-would-i-do-an-if-for-time-of-day-day-dusk-night/#findComment-376916 Share on other sites More sharing options...
Fredgerd Posted November 24, 2013 Author Share Posted November 24, 2013 dont_starve/data/scripts/components/clock.lua seems to imply you are correctcool What am I doing wrong with this particular snippet, if you wouldn't mind taking a look? The intention is to increase movespeed during dusk/night. It doesn't seem to have any effect though.local function shinobi (inst, data) if GetClock().phase == "dusk" then inst.walkspeed = 10 inst.runspeed = 10 elseif GetClock().phase == "night" then inst.walkspeed = 10 inst.runspeed = 10 elseif GetClock().phase == "day" then inst.walkspeed = 4 inst.runspeed = 4 endend Link to comment https://forums.kleientertainment.com/forums/topic/29945-how-would-i-do-an-if-for-time-of-day-day-dusk-night/#findComment-376926 Share on other sites More sharing options...
squeek Posted November 24, 2013 Share Posted November 24, 2013 (edited) First, walkspeed and runspeed are only a part of the locomotor component, so you should be using inst.components.locomotor.runspeed and inst.components.locomotor.walkspeed.But, more importantly, where is that function called from? It has the parameters of an event callback, but I'm not sure what event you could be listening for that gets pushed whenever the clock phase changes.If you want to use event listeners, you can do:local function MakeFast( inst, data ) inst.components.locomotor.runspeed = 10 inst.components.locomotor.walkspeed = 10endlocal function MakeSlow( inst, data ) inst.components.locomotor.runspeed = 4 inst.components.locomotor.walkspeed = 4endlocal function SetUpSpeed( inst ) if GetClock():IsDay() then MakeSlow( inst ) else MakeFast( inst ) endend-- all of the following should go in your character prefab's 'fn' function: -- to set up the initial speeds based on the current phase: -- see for why this needs a zero-time task (it might not actually need a zero-time task, but it seems likely) inst:DoTaskInTime( 0, SetUpSpeed ) -- listen for phase changes inst:ListenForEvent( "daytime", MakeSlow, GetWorld() ) inst:ListenForEvent( "dusktime", MakeFast, GetWorld() ) inst:ListenForEvent( "nighttime", MakeFast, GetWorld() ) -- as a safety, probably not necessary Edited November 24, 2013 by squeek Link to comment https://forums.kleientertainment.com/forums/topic/29945-how-would-i-do-an-if-for-time-of-day-day-dusk-night/#findComment-376928 Share on other sites More sharing options...
Fredgerd Posted November 24, 2013 Author Share Posted November 24, 2013 (edited) Edit: Just realized I missed the line of code for DoTaskInTime. Gonna add that in and thats probably the problem. Sorry. Edit2: Nope, still causes the same crash. Didn't work quite right ...data/../mods/wakizashi/scripts/prefabs/wakizashi.lua:46: attempt to index field 'locomotor' (a nil value)LUA ERROR stack traceback:.../wakizashi.lua (46,1) in function 'fn'.../entityscript.lua (634,1) in function 'PushEvent'.../clock.lua (457,1) in function 'StartDusk'.../clock.lua (227,1) in function 'NextPhase'.../clock.lua (309,1) in function 'OnUpdate'.../update.lua (104,1) This is my full prefab if thats helpful. Sorry if I'm missing something obvious. Lua is new to me.local MakePlayerCharacter = require "prefabs/player_common"local assets = { Asset( "ANIM", "anim/player_basic.zip" ), Asset( "ANIM", "anim/player_idles_shiver.zip" ), Asset( "ANIM", "anim/player_actions.zip" ), Asset( "ANIM", "anim/player_actions_axe.zip" ), Asset( "ANIM", "anim/player_actions_pickaxe.zip" ), Asset( "ANIM", "anim/player_actions_shovel.zip" ), Asset( "ANIM", "anim/player_actions_blowdart.zip" ), Asset( "ANIM", "anim/player_actions_eat.zip" ), Asset( "ANIM", "anim/player_actions_item.zip" ), Asset( "ANIM", "anim/player_actions_uniqueitem.zip" ), Asset( "ANIM", "anim/player_actions_bugnet.zip" ), Asset( "ANIM", "anim/player_actions_fishing.zip" ), Asset( "ANIM", "anim/player_actions_boomerang.zip" ), Asset( "ANIM", "anim/player_bush_hat.zip" ), Asset( "ANIM", "anim/player_attacks.zip" ), Asset( "ANIM", "anim/player_idles.zip" ), Asset( "ANIM", "anim/player_rebirth.zip" ), Asset( "ANIM", "anim/player_jump.zip" ), Asset( "ANIM", "anim/player_amulet_resurrect.zip" ), Asset( "ANIM", "anim/player_teleport.zip" ), Asset( "ANIM", "anim/wilson_fx.zip" ), Asset( "ANIM", "anim/player_one_man_band.zip" ), Asset( "ANIM", "anim/shadow_hands.zip" ), Asset( "SOUND", "sound/sfx.fsb" ), Asset( "SOUND", "sound/wilson.fsb" ), Asset( "ANIM", "anim/beard.zip" ), -- Don't forget to include your character's custom assets! Asset( "ANIM", "anim/wakizashi.zip" ),}local prefabs = {}local start_inv = { "nightmare_ninjato"}local function MakeFast( inst, data ) inst.components.locomotor.runspeed = 10 inst.components.locomotor.walkspeed = 10end local function MakeSlow( inst, data ) inst.components.locomotor.runspeed = 4 inst.components.locomotor.walkspeed = 4end local function SetUpSpeed( inst ) if GetClock():IsDay() then MakeSlow( inst ) else MakeFast( inst ) endendlocal fn = function(inst) -- sounds inst.soundsname = "wilson" -- map icon inst.MiniMapEntity:SetIcon( "wilson.png" ) -- go fast in the darkinst:DoTaskInTime( 0, SetUpSpeed ) inst:ListenForEvent( "daytime", MakeSlow, GetWorld() ) inst:ListenForEvent( "dusktime", MakeFast, GetWorld() ) inst:ListenForEvent( "nighttime", MakeFast, GetWorld() )endSTRINGS.CHARACTERS.WAKIZASHI = require "speech_wakizashi"return MakePlayerCharacter("wakizashi", prefabs, assets, fn, start_inv) Edited November 24, 2013 by Fredgerd Link to comment https://forums.kleientertainment.com/forums/topic/29945-how-would-i-do-an-if-for-time-of-day-day-dusk-night/#findComment-376934 Share on other sites More sharing options...
squeek Posted November 24, 2013 Share Posted November 24, 2013 (edited) Edit: Just realized I missed the line of code for DoTaskInTime. Gonna add that in and thats probably the problem. Sorry. Edit2: Nope, still causes the same crash.Whoops, looks like I have been incorrect about how ListenForEvent works all this time! Since the world is passed as the source of the event, it turns out that when the event is pushed, the callback will get called with the world as the first parameter. I thought for sure it would still pass the listening prefab, not the source of the event. Try this: inst:ListenForEvent( "daytime", function() MakeSlow(inst) end, GetWorld() ) inst:ListenForEvent( "dusktime", function() MakeFast(inst) end, GetWorld() ) inst:ListenForEvent( "nighttime", function() MakeFast(inst) end, GetWorld() ) Edited November 24, 2013 by squeek Link to comment https://forums.kleientertainment.com/forums/topic/29945-how-would-i-do-an-if-for-time-of-day-day-dusk-night/#findComment-376936 Share on other sites More sharing options...
simplex Posted November 24, 2013 Share Posted November 24, 2013 Whoops, looks like I have been incorrect about how ListenForEvent works all this time! Since the world is passed as the source of the event, it turns out that when the event is pushed, the callback will get called with the world as the first parameter. I thought for sure it would still pass the listening prefab, not the source of the event. I know, right? This is so annoying and illogical... The only difference between doinginst:ListenForEvent("foo", bar, source)andsource:ListenForEvent("foo", bar)is that in the former the event listener will be removed whenever either of inst and source are. So it's just a matter of cleanup. Link to comment https://forums.kleientertainment.com/forums/topic/29945-how-would-i-do-an-if-for-time-of-day-day-dusk-night/#findComment-376950 Share on other sites More sharing options...
Fredgerd Posted November 25, 2013 Author Share Posted November 25, 2013 Whoops, looks like I have been incorrect about how ListenForEvent works all this time! Since the world is passed as the source of the event, it turns out that when the event is pushed, the callback will get called with the world as the first parameter. I thought for sure it would still pass the listening prefab, not the source of the event. Try this: inst:ListenForEvent( "daytime", function() MakeSlow(inst) end, GetWorld() ) inst:ListenForEvent( "dusktime", function() MakeFast(inst) end, GetWorld() ) inst:ListenForEvent( "nighttime", function() MakeFast(inst) end, GetWorld() ) Awesome, it works, thanks so much! Two other things I'm wondering how to do. One is probably easy, the other might not be possible. The first is I want to make certain body items (specifically Backpack, Krampus Sack and Armor) unwearable. I imagine this would work similar to diet restriction powers but I'm not sure exactly what to do. The second is to reduce the agro range of certain monsters at night/dusk. I have no idea if modifying agro range is even possible, but if so I think it would fit well with this character since I'm aiming for sort of a stealth build. Link to comment https://forums.kleientertainment.com/forums/topic/29945-how-would-i-do-an-if-for-time-of-day-day-dusk-night/#findComment-377572 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now