Succulantis Posted September 13, 2019 Share Posted September 13, 2019 Hello!! I should start by saying I’m not entirely well versed with LUA and I’m just starting out. I’m interested in giving a character a perk of having the chance to fall asleep during rain. The problem is I don’t know if this is even possible and if so, I’m not sure how to go about it. I currently have written code, although it doesn’t work (of course). I also don’t know whether said code would go in character or modmain. Here’s the current code I have. -- Rain Sleep local function rainsleep(inst, time) if not inst.watchingrain then inst.watchingrain = true inst:WatchWorldState("israining", onisraining) onisraining(inst, TheWorld.state.israining) if math.random()<0.1 and onisraining then PushEvent("yawn", { grogginess = 8, knockoutduration = time + math.random() }) end end end This is based off mostly from certain posts as well as the mandrake sleep code so... yeah. Help, coding advice, and explanations are greatly appreciated! Link to comment Share on other sites More sharing options...
Ultroman Posted September 13, 2019 Share Posted September 13, 2019 Where is your onisraining function? You refer to it several times, but it is not part of the code snippet. Also, you don't have a "time" variable, although you're using it. There seems to be some confusion about what you're doing. If you are a complete novice with coding, most of our explanations won't make any sense to you anyway. You should start by looking through the newcomer post and taking the Lua Crash Course linked there. The whole post is littered with good information that will save you time and stress, and help you search the game code faster to figure out how things work. That said, it looks like you're kind of on the right path. AFAIK that IS the event to push and those are values that will work, but if I'm reading this right, your character will just have a chance to fall asleep at the exact moment the rain starts, but not additional chances during the rain. Can you explain exactly what you want to have happen? I mean, how long after the rain starts should your character be able to fall asleep? Immediately? How long should they sleep, then? Should they be able to fall asleep several times during one rain "event"? Link to comment Share on other sites More sharing options...
Succulantis Posted September 14, 2019 Author Share Posted September 14, 2019 Ah right, so this code was changed to <0.1 for testing to see if the character would fall asleep in the rain period. However, what I had been wanting to do after getting the sleep part down is get the character to have a chance to fall asleep 1 to 3 times during the rain after about 10 seconds of the rain starting. The length of sleep would be random though it ranges from 10 to 20 seconds. As for onisraining, I did get confused unfortunately as I was following with WX-78’s code but obviously missed the function itself and jumbled it all up, not even considering making the function in my own code. Not sure what I was trying to do with time to be honest other than I saw it in the Mandrake code and attempted to apply it. Link to comment Share on other sites More sharing options...
Ultroman Posted September 14, 2019 Share Posted September 14, 2019 Here. This code has some nice values set up for you to play with. It should allow you to set it up like you want. You can move everything except the last two lines out of and above the master_postinit function in order to reduce clutter in that function, but the last two lines must be in it, and they must be placed AFTER your ismastersim check.. -- Rain Sleep local minInitialSleepDelay = 10 local maxInitialSleepDelay = 15 local minTimeBetweenSleepAttempts = 10 local maxTimeBetweenSleepAttempts = 20 local minSleepTime = 10 local maxSleepTime = 20 local sleepChance = 0.1 local function applyRainSleep(inst, delay) -- Delay calling the in-line sleep-application function by the value given -- in the delay parameter. -- We save the task in a variable on the inst, so we can cancel it when the rain stops. inst.myRainSleepTask = inst:DoTaskInTime(delay, function(inst) -- The delay has run out, so it is time to do a sleep-attempt and restart the task. -- Get a random amount of sleep time. local sleeptime = math.random(minSleepTime, maxSleepTime) -- 10% chance on each "attempt" to make the player sleep. if math.random() < sleepChance then PushEvent("yawn", { grogginess = 8, knockoutduration = sleeptime }) else -- If the sleep was not applied, then we do not need to delay -- the next attempt by the sleep time (see below). sleeptime = 0 end -- We call the applyRainSleep function, in order to restart the task. -- We delay the next attempt by a randomized amount. -- If the player was forced asleep, we need to additionally delay -- for the time they were made to sleep (sleeptime). applyRainSleep(inst, sleeptime + math.random(minTimeBetweenSleepAttempts, maxTimeBetweenSleepAttempts)) end) end local function onisraining(inst, israining) if israining then -- If the continuous myRainSleepTask is not already running... if not inst.myRainSleepTask then -- Start the applyRainSleep loop with a randomized delay. applyRainSleep(inst, math.random(minInitialSleepDelay, maxInitialSleepDelay)) end else -- Stop the sleep task when the rain stops. if inst.myRainSleepTask then inst.myRainSleepTask:Cancel() inst.myRainSleepTask = nil end end end inst:WatchWorldState("israining", onisraining) onisraining(inst, TheWorld.state.israining) Link to comment Share on other sites More sharing options...
Succulantis Posted September 14, 2019 Author Share Posted September 14, 2019 Ah, thank you!! I cannot be more grateful for your help!! Link to comment 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