Skip to content

Need Help w/ Simple Hunger & Thirst System for Single Player

I'm trying to make a simple Hunger & Thirst System for a Single-Player module that does the following:
  1. If the PC goes without water for more than a day - take 1 point of damage each day from dehydration.
  2. If the PC goes without food for more than a day - set a variable that marks starvation level and after starvation level hits 3, the PC takes 1 point of damage each day
  3. Drinking from Waterskin resets timer
  4. Eating food resets timer

I found this function by @Proleric

int bhHour() {return ((GetCalendarYear() - 1) * 12 * 30 * 24) + ((GetCalendarMonth() - 1) * 30 * 24)
+ ((GetCalendarDay() - 1) * 24) + GetTimeHour();}

And used it to set the start time in the module's pre-enter using

SetLocalInt(oPC, "StartHour", bhHour());

I can fudge my way through most of it, BUT how do I use this function to check for elapsed time? I just can't wrap my head around it.

Comments

  • I should probably tag @Shadooow too - he's a wizard with this kind of stuff...
  • ProlericProleric Member Posts: 1,281
    When the clock starts (e.g. just had water) store T1 = bhHour() as a local int.

    Subsequently (checking on heartbeat or whatever) elapse time in hours is bhHour() - T1.

    When that goes over 24, apply penalty.
  • ProlericProleric Member Posts: 1,281
    Another method:

    After drinking,
    SetLocalInt(oPC, "LastDrinkTime", bhHour());
    
    DelayCommand(IntToFloat(24 * HoursToSeconds()), ExecuteScript("foobar", oPC));
    
    The script foobar will run 24 game hours later to apply penalties if LastDrinkTime was 24 hours ago (but do nothing if the timestamp has been updated by drinking in the meanwhile).

    Personally, I feel a little uncomfortable with delaying scripts for very long periods, preferring to check regularly to shut down unwanted delayed scripts, but this is probably irrational on my part as I haven't actually encountered any problems with it.
  • [Deleted User][Deleted User] Posts: 0
    edited March 2020
    I was developing ulcers trying to get everything to fire reliably so I basically wound up treating the PC the same way as I do torches - storing int variables on the PC:

    The PC has 480 Thirst Charges (24 hours) and 1440 Hunger Charges (72 Hours). Every Module Heartbeat, the number of charges is reduced by 1. When the charges reach 0 the effect is applied and the charges are reset to 20 (1 hour) for the Thirst Charges and 480 (24 hours) for the Hunger Charges.

    Eating and drinking somethings resets the charges to their initial values.

    Using charges I can also make adjustments for the quality of the food and drink, amount, etc. Probably not the best method, but I can understand this stuff well-enough.
  • ProlericProleric Member Posts: 1,281
    I imagine that will work just as well.

    If you're thinking of publishing for general use, strictly speaking 480 should read ((24 / 6) * HoursToSeconds()) and so on, because the module author can alter the Minutes per Hour in module properties.

    Perhaps other readers can confirm whether the assumption of 6 seconds per heartbeat is precisely reliable. It feels more natural to me to refer to the game date directly, especially since some of my timed quests run for game weeks, where a discrepancy could become visible to the player... but I'm probably being hypervigilant.
  • Well I've learned through testing (at least in my case) that the first Heartbeat is actually 5 seconds into the game as opposed to 6 seconds (as confirmed by that TIME library Shadooow showed me).

    I'm not planning on publishing for general use - the Hunger/Thirst is module specific. Thus, I can tweak it easily enough if I change the game clock settings in the module properties.
Sign In or Register to comment.