Cradle
Cradle copied to clipboard
Add missing array extension methods for Sugar
https://www.motoslave.net/sugarcube/2/docs/object-methods.html#array-arrayprotopluckmany
The new implementation adds to the Sugar story format two VarType services (the Cradle way of extending StoryVar) for array (List) and object (Dictionary). So now you do things like <<set $arr[3] = "milk">>
.
To overcome JavaScript to C# API incompatibility, extension methods were added that polyfill things like concat, pluck, etc. as well as some common JavaScript methods like toString().
The previous implementation that used arrayGet, objSet etc. has been removed - BREAKING CHANGE.
The only thing that remained is the requirement to instantiate variable of these types like this:
For array:
<<set $arr = array("cheese", "milk")>>
instead of ~~<<set $arr = ["cheese", "milk"]>>
~~
For object:
<<set $data = obj("captain", "Ahab", "whale", "Moby-Dick")>>
instead of ~~<<set $data = {"captain": "ahab", "whale" : "Moby-Dick"}>>
~~
To preview the use of these extensions before the develop branch is merged with master, download the following files and place them anywhere in your project: https://github.com/daterre/Cradle/blob/develop/.src/Cradle/StoryFormats/Sugar/StoryVarExtensions.cs https://github.com/daterre/Cradle/blob/develop/.src/Cradle/StoryFormats/Sugar/VarTypes/SugarDictionaryService.cs https://github.com/daterre/Cradle/blob/develop/.src/Cradle/StoryFormats/Sugar/VarTypes/SugarListService.cs
And then add these 2 lines somewhere before you call story.Begin:
StoryVar.RegisterTypeService<List<StoryVar>>(new SugarListService());
StoryVar.RegisterTypeService<Dictionary<string, StoryVar>>(new SugarDictionaryService());
Hi! Thanks for doing this! It did not work out fo the box, unfortunately. Should I also change js extensions accordingly? https://github.com/daterre/Cradle/blob/master/Editor/js/StoryFormats/Sugar/sugar.extensions.js_ Meanwhile I tested it by changing my imported story code. For some reason pluck many hangs the application. I'll debug soon.
StoryVarExtensions needed minor fix on line 254 (Max to Min). Likely the same issue for the other operations?
Now I'm stuck with lack of vars capturing support in my story, unfortunately, which is technically out of the scope for this arrays issue. I'll try to simplify my example and let you know.
Here's the example BTW, if you're aware of a better way to generate links from changing arrays so it works with Cradle, I'd appreciate that.
<<for $i to 0; $i lt length($discoveredLocations); $i++>>
<<capture $i>>
<<set $location = get($discoveredLocations,$i)>>
[[$location | $location] [$visitedLocations = concatUnique($visitedLocations, deleteAt($discoveredLocations,$i))]]
<br>
<</capture>>
<</for>>
Hey mate, anything I could help with?
Hey, sorry left you hanging, if you have any fixes/additions I'd really appreciate a pull request - this task is really hard to complete with the short time slots I have to work on Cradle.