Cradle icon indicating copy to clipboard operation
Cradle copied to clipboard

Add missing array extension methods for Sugar

Open daterre opened this issue 6 years ago • 6 comments

https://www.motoslave.net/sugarcube/2/docs/object-methods.html#array-arrayprotopluckmany

daterre avatar Jun 17 '18 11:06 daterre

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"}>>~~

daterre avatar Jul 29 '18 11:07 daterre

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());

daterre avatar Jul 29 '18 11:07 daterre

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.

Braza avatar Aug 05 '18 10:08 Braza

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>>

Braza avatar Aug 09 '18 14:08 Braza

Hey mate, anything I could help with?

Braza avatar Oct 19 '18 19:10 Braza

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.

daterre avatar Jan 20 '19 08:01 daterre