Creating a basic time loop with day phases

I’m new to coding and to working with twine/ sugarcube, so I’m looking for any help if possible. I’ve found a decent code that I’m trying to implement into a game I’m developing for a basic time look with phases for the day and I’m encountering trouble with making it work. Any help would be greatly appreciated. Here is the code I’m using in a StoryInIt passage:

> /* Game Date */
> <<set setup.datePart = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]>>
> <<set $gameDate = 0>>
> 
> /* Game Time */
> <<set setup.dayPart = ["Morning", "Afternoon", "Evening", "Night"]>>
> <<set $gameTime = 0>>
> 
> <<if setup.dayPart[$gameTime] is "Night">>
>   <<set $gameTime = 0>>
> <<endif>>
> 
> <<if setup.dayPart[$gameTime] is "Morning">>
>   You wake up in the morning feeling refreshed.
> <<endif>>
> 
> <<set $gameTime = ($gameTime + 1) % setup.dayPart.length>> 
> 
> <<if setup.datePart[$gameDate] is "Sunday">>
>     <<set $gameDate = 0>>  //  Start again from Monday
>   <<else>>
>     <<set $gameDate = ($gameDate + 1) % setup.datePart.length>>  //Increase game date
>   <<endif>>

To display the day and time period on the sidebar I’m using the following code in a StoryCaption passage:

> Date: <<=setup.datePart[$gameDate]>>
> Time: <<=setup.dayPart[$gameTime]>>

When I run the game to test it shows the following error message on the sidebar:

> Error: <<=>>: bad evaluation: Cannot read properties of undefined (reading 'undefined')
> 
>
> <<=setup.datePart[$gameDate]>>
>

Any help is greatly appreciated. Sorry if this is posted in the wrong place. I’m new here. Thanks

1 Like

Hi!

Which version of SugarCube are you using?

Because <<endif>> has been deprecated for SugarCube 2. You should be using <</if>> instead.

Also, StoryInit only loads once, when you open the file/project. If you have code that should be used anywhere else, like to advance time/day, you may want to use a <<widget>> Docs

EDIT: also you may find these custom macro handy:

1 Like

Hey, thanks for the reply… I’m using sugarcube .2 so I fixed the <</if>> part. I understand that I need a macro for changing the date/ time and I have some ideas. My main concern is that I can’t get it to populate the day/ time on the sidebar So I’m curious what is preventing this from happening.

I copied your code with the corrected closing if tags and it is working for me.
Undefined usually means a variable isn’t set somewhere. Have you tried using debug mode?

I hope you meant the StoryInit passage, because you can only have one. Twine 2 shouldn’t let you have more than one, unless you’re misspelling them.

Also. If StoryInIt is the actual name of the passage you’re using, and not simply a typo here, then that’s your problem.

Identifiers and names in SugarCube are both case-sensitive and unforgiving of extra whitespace. Thus, the correct StoryInit is not the same as StoryInIt (second i is uppercase) or StoryInit (trailing space) as just two examples.

You may want to verify the spelling of your passage.

Wow that’s such a silly oversight on my part. Thank you so much. I do have only one passage but I capitalized the second “I”. I was going crazy.