Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Day/Night and Seasons, How would I Implement with widgets?

Hi,

Ive been racking my brains for several hours with this thinking of different ways to do it without too much javascript as my skills with it are pretty basic.

I would like to count the turns using a variable $turns,so as per another post I know i can use <<set $turns to 0>> in the PassageReady. I already have the variable in my $player.turns so can reuse it. What I would like to do is for every lets say 20 turns set another variable $timeofday ["morning", "afternoon", "evening", "night"] and eventually also have seasons as a variable based on the amount of turns. I can figure out how to do the latter though I know its probably something basic I dont know how to count the amount of turns, I was thinking of lets say every 10 turns the $timeofday changes and every 200 the time of season changes. I am going to do these as widgets and add them to PassageReady/PassageDone

To add some more information the purpose is basic 2 different sets of text/actions occur depending on the time of day (the passages can be revisited), also random events will occur depending on time of day. In addition to this other factors such as health (health system already in place) will be effected depending on $season.

I am using Twine 2.1.1 and SugarCube 2.14

Any illumination on this would be greatly appreciated as I think if I can get this one figuered out it should cascade to the other few features I would like to implement.

Comments

  • edited April 2017
    I have simplified this slightly, by reducing the $timeofday to just day/night and the $season to warm/cold I have implimented this in the text of the passage and I have added the <<set $turn++>> to PassageReady. Though I am still struggling with how to count this every 10 turns... Would I be right in thinking this would need to be done as a loop?
  • edited April 2017
    Here's a widget, though it'll need more testing:
    <<widget "turnCycle">>
    
      <<if tags().includes("timeStart")>>
        <<set $time to 0>>
      <<elseif tags().includes("timePause")>>
        <<set $time to $time>>
      <<else>>
        <<set $time to $time + 1>>
      <<endif>>
    
      <<set _clockTime to Math.trunc($time / $dayLength)>>
      <<set _clockTime to _clockTime % $timeofday.length>>
      /%modulo (%) will turn $time into a 0-based repeating sequence of numbers%/
      <<set $displayTime to $timeofday[_clockTime]>>
    
      <<set _seasonTime to Math.trunc($time / $seasonLength)>>
      <<set _seasonTime to _seasonTime % $seasons.length>>
      <<set $displaySeason to $seasons[_seasonTime]>>
    
    <<endwidget>>
    

    You'll need to put this in your StoryInit:
    <<set $timeofday to ["Morning", "Afternoon", "Evening", "Night"]>>
    <<set $seasons to ["Spring", "Summer", "Fall", "Winter"]>>
    <<set $dayLength to 10>>/%change the number of turns it takes to change the time%/
    <<set $seasonLength to 200>>/%number of turns to change the season%/
    

    Then, in PassageReady, invoke it with:
    <<turnCycle>>
    

    It uses passage tags for control. To stop the timer for a passage, tag it with "timePause." You'll need to tag one of your passages with "timeStart" to initialize the timer and get it counting. You can also use the "timeStart" tag to reset the timer to 0.

    For the arrays, they'll accept any number of values and translate that into the code, so you can switch it to day/night by changing the value of the $timeofday array:
    <<set $timeofday to ["day", "night"]>>
    

    You can add more or less to each array as you see fit, the widget should be flexible enough.

    Use the variables $displayTime and $displaySeason for output.
  • Wow that's fantastic. I will test it out shortly. Its an innovative way of aproaching the problem using the tags. I have already used tags to set the "biome" of a passage basically the character has a thirst and hunger bar which goes down a particular level depending on what biome it is. From a quick glance it makes sense but will test it more thoroughly and let you know how I get on.

    Really appreciated thanks :smiley:
  • edited April 2017
    Another quick question which is related to your answer and slight curiosity. I notice looking around at peoples questions/scripts that words with an underscore crop up quite often for example here:

    <<set _clockTime to Math.trunc($time / $dayLength)>>

    _clockTime what is the reason for this underscore?, Is it to denote a temporary variable?

    :smile:
  • Chapel wrote: »
    <<widget "turnCycle">>
    
      <<if tags().includes("timeStart")>>
        <<set $time to 0>>
      <<elseif tags().includes("timePause")>>
        <<set $time to $time>>
      <<else>>
        <<set $time to $time + 1>>
      <<endif>>
    
      <<set _clockTime to Math.trunc($time / $dayLength)>>
      <<set _clockTime to _clockTime % $timeofday.length>>
      /%modulo (%) will turn $time into a 0-based repeating sequence of numbers%/
      <<set $displayTime to $timeofday[_clockTime]>>
    
      <<set _seasonTime to Math.trunc($time / $seasonLength)>>
      <<set _seasonTime to _seasonTime % $seasons.length>>
      <<set $displaySeason to $seasons[_seasonTime]>>
    
    <<endwidget>>
    
    A few points.
    1. The <<end…>> versions of ending tags are deprecated. Using them is disrecommended.
    2. There is an appalling amount of whitespace being generated there for a widget which by its very nature should be silent. I'd suggest wrapping the contents in <<silently>>. For example:
      <<widget "turnCycle">>\
      <<silently>>
      …code…
      <</silently>>\
      <</widget>>
      
    3. Reassigning the $time variable's current value to itself is completely unnecessary if you restructure your logic to something like the following:
      	<<if tags().includes("timeStart")>>
      		<<set $time to 0>>
      	<<elseif not tags().includes("timePause")>>
      		<<set $time to $time + 1>>
      	<</if>>
      

    Arktix wrote: »
    _clockTime what is the reason for this underscore?, Is it to denote a temporary variable?
    Yes. See TwineScript > Variables for more information.
  • Arktix wrote: »
    ...what is the reason for this underscore?, Is it to denote a temporary variable?
    The TwineScript - Variables section of the SugarCube 2 documentation contains the answer to your question.
  • ... The <<end…>> versions of ending tags are deprecated. Using them is disrecommended. ...
    I didn't know that, thanks. I'm so used to using <<end...>> because the red highlight on <<if>> macros that appears when you use <</if>> (in Twine 1.4.2) for some reason annoys me. I didn't know it was deprecated, though, I'll get out of the habit.
    ... There is an appalling amount of whitespace being generated there for a widget which by its very nature should be silent. I'd suggest wrapping the contents in <<silently>>. ...
    Good catch. I was thinking since PassageReady was silent already it wouldn't matter, but there's no reason this widget couldn't be used in a PassageHeader or just a passage in general, so that's definitely a good idea.

    Unless there's an even more pressing reason to do this even in a PassageReady that I'm not thinking of.
    ...Reassigning the $time variable's current value to itself is completely unnecessary if you restructure your logic...
    Yeah, I really, really couldn't think of how to do this. I knew it was unnecessary, but my brain just couldn't get there, haha. Thanks.

    Awesome as always, TME.
  • Thanks for all the replies, finally got to try it and it works! So big thanks to Chapel for the original widget and to TME for the amendments. Also to greyelf for the answer to the other question. The link you added is possibly the only page I didnt look at in depth as I was fairly comfortable with variables from Twine 1.4

    I dont often ask for help on forums but I'm really glad I did ... really helpful thanks very much again.
Sign In or Register to comment.