Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing array extension methods for Sugar #64

Open
daterre opened this issue Jun 17, 2018 · 6 comments
Open

Add missing array extension methods for Sugar #64

daterre opened this issue Jun 17, 2018 · 6 comments

Comments

@daterre
Copy link
Owner

daterre commented Jun 17, 2018

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

@daterre
Copy link
Owner Author

daterre commented Jul 29, 2018

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
Copy link
Owner Author

daterre commented Jul 29, 2018

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

@Braza
Copy link

Braza commented Aug 5, 2018

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

Braza commented Aug 9, 2018

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

Braza commented Oct 19, 2018

Hey mate, anything I could help with?

@daterre
Copy link
Owner Author

daterre commented Jan 20, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants