Howdy, Stranger!

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

Pathfinding in Twine

edited May 2015 in Help! with 2.0
Hi,

I was wondering if someone can give me some pointers on how to do pathfinding for NPCs in Twine. Since passages do not necessarily correspond to locales, I don't see an easy way of doing this.

I imagine that there are some info on this already, so pointers would be great.
Thanks.

PS) I am new here, so I should remember to mention that I am using the standard format (Harlowe?).

Comments

  • Can you be more specific, please? I think it will help. I KNOW it will help me, anyway.

    —Sage.
  • edited May 2015
    I am trying to figure out if there's an easy way to do locations based on arrays, and I found Dan Cox's tutorial on making a dungeon crawler that seems to do just that

    ()

    He uses a grid to figure out where the player is and where he can go (which solves the issue for NPCs moving around too). However, he is using a different format than Harlowe (SugarCube). I am trying to do a 2d array in Harlowe, but with no luck. What's wrong here:

    (set:$mapArray to
    [[0,0,0,0],
    [0,1,1,0],
    [0,0,1,0],
    [0,1,1,0],
    [0,0,0,0],
    [0,0,0,0]]
    )

    I get an "unexpected token"-error.

    or maybe I should simply switch to the other story format now? Can anyone supply a link that details the differences between formats?


  • edited May 2015
    Is this Twine 2 or Twine 1.4.2? Assuming you mean SugarCube is the other format, in addition to the Twine 2 Guide these links might help.

    Harlowe Resource: http://twine2.neocities.org/
    SugarCube: http://www.motoslave.net/sugarcube/docs/macros.html

    If you are using Twine 2.0 then setting an array in Harlowe looks something like this:
    (set: $mapArray to (array: 0,0,0,0))
    

    Two-dimensional arrays need a bit more coding but I hope that clarifies the syntax for Harlowe.

    ----

    Here's a link that might help with regard to Pathfinding in 1.4.2 with SugarCube, would need a bit of modification to work with 2.0 but at least the example code is there for you to poke around:

    Pathfinding Widget for Maze-Games [Sugarcube]

  • Thank you, and yes, I meant SugarCube.

    As a total newbie, it's a little confusing with the different formats. I will read the provided guides, but maybe someone could tell me what the main reasons are for using one over the other?
  • Oh, and I am using Twine 2.0 - unless there's a really good reason not to. I am just starting out, so I am a bit shaky on what's what.

    Basically, I am trying to learn the syntax of T2 and thought it would be interesting to do an oldschool text adventure first just to get to know the system.
  • edited May 2015
    Different story formats provide different macro types and some stories can do some things better than others. Different people like different story formats.

    But as to whether there's a good reason to use Twine 2, honestly if you are using SugarCube then you might like Twine 1.4.2 better.

    The main strength of Twine 2 imo is Harlowe. While you can run SugarCube in Twine 2, the experience is not as smooth as working with SugarCube in 1.4.2, as I think 1.4.2 has a better interface than Twine 2 (some sacrifices were made to make Twine 2 browser based).
  • Dominia wrote: »
    If you are using Twine 2.0 then setting an array in Harlowe looks something like this:

    (set: $mapArray to (array: 0,0,0,0))

    Two-dimensional arrays need a bit more coding but I hope that clarifies the syntax for Harlowe.

    This is what I am looking for. Could you do an example with a 2d array?


  • edited May 2015
    wingsofwax wrote: »
    Oh, and I am using Twine 2.0 - unless there's a really good reason not to. I am just starting out, so I am a bit shaky on what's what.
    2.0 and Harlowe are perfectly fine for what you want to do. SugarCube *might* be more of a fit but it really depends on what you are doing with the game.

    The link for Twine 2 Guide should help as well as L's resource guide for Harlowe 2.0. I keep both of those up in tabs in my browser at all times when I am working with 2.0 and Harlowe. You'll get the hang of it pretty quickly, just takes some practice and there are lots of people here willing to help.

    As for selecting a format, info is here that might help: How to Choose a Story Format

    P.S. I love old school text adventures. :D

  • Claretta wrote: »
    Different story formats provide different macro types and some stories can do some things better than others. Different people like different story formats.

    But as to whether there's a good reason to use Twine 2, honestly if you are using SugarCube then you might like Twine 1.4.2 better.

    The main strength of Twine 2 imo is Harlowe. While you can run SugarCube in Twine 2, the experience is not as smooth as working with SugarCube in 1.4.2, as I think 1.4.2 has a better interface than Twine 2 (some sacrifices were made to make Twine 2 browser based).

    Thanks! I'll be sticking to T2 and Harlowe then.


  • edited May 2015
    2d array in Harlowe would look something like this:
    (set: $mapArray to (a:))
    (set: $mapEntry to $mapArray.push((a: 0,0,0,0), (a: 0,1,1,0)))
    

    Just an FYI, (a:) is shorthand for (array:).

    Also: Additional examples by @greyelf here:

    Can you use multidimensional arrays in Harlowe?
  • edited May 2015
    wingsofwax wrote: »
    Claretta wrote: »
    Different story formats provide different macro types and some stories can do some things better than others. Different people like different story formats.

    But as to whether there's a good reason to use Twine 2, honestly if you are using SugarCube then you might like Twine 1.4.2 better.

    The main strength of Twine 2 imo is Harlowe. While you can run SugarCube in Twine 2, the experience is not as smooth as working with SugarCube in 1.4.2, as I think 1.4.2 has a better interface than Twine 2 (some sacrifices were made to make Twine 2 browser based).

    Thanks! I'll be sticking to T2 and Harlowe then.


    Well to clarify, there's nothing actually wrong with SugarCube in Twine 2, it's just some Twine 2 things that some people don't like, like tying the story saves to your browser cache, and the lack of full screen passage editing.

    IMO, these are big enough reasons to use 1.4.2 over 2 if you can, so for SugarCube which supports both, I prefer 1.4.2.

    Other people really like Harlowe so use Twine 2 despite these issues.


    All this isn't to hate on Twine 2, it's just to point out that the choice is not "2 is automatically better because it's a bigger number than 1". Some people like Harlowe because it does what they want, others prefer 1.4.2. Both versions of Twine have good things going for them.
  • edited May 2015
    Dominia wrote: »
    2d array in Harlowe would look something like this:
    (set: $mapArray to (a:))
    (set: $mapEntry to $mapArray.push((a: 0,0,0,0), (a: 0,1,1,0)))
    

    Just an FYI, (a:) is shorthand for (array:).

    Thank you, I'll try that out.

    Oh, one more thing: Do you know the syntax for getting data out of the array too?


  • wingsofwax wrote: »
    Oh, one more thing: Do you know the syntax for getting data out of the array too?
    (set: $mapArray to (a:))
    (set: $mapEntry to $mapArray.push((a: 0,0,0,0), (a: 0,1,1,0), (a: 0,0,1,0), (a: 0,1,1,0), (a: 0,0,0,0), (a: 0,0,0,0)))
    

    length of mapArray:
    (print: $mapArray.length)
    

    all entries in mapArray:
    (print: $mapArray)
    

    first entry in mapArray:
    (print: $mapArray's 1st)
    

    first element of first entry in mapArray:
    (print: $mapArray's 1st's 1st)
    

    print mapArray as a grid:
    (print: $mapArray's 1st)
    (print: $mapArray's 2nd)
    (print: $mapArray's 3rd)
    (print: $mapArray's 4th)
    (print: $mapArray's 5th)
    (print: $mapArray's 6th)
    

    If you want to set values into a variable you would simply use (set:) instead of print.

    NOTE: Not sure why commas are being printed. Seems like they shouldn't.
  • Thank you Dominia, you've been very helpful. I will try playing around with what you've described, but could there be other more traditional ways of getting data out of arrays - like "Xth" instead of "5th", for example?



  • edited May 2015
    wingsofwax wrote: »
    I will try playing around with what you've described, but could there be other more traditional ways of getting data out of arrays - like "Xth" instead of "5th", for example?
    If it were normal [] based indexing then I'd be comfortable helping out but I am not too familiar with Harlowe's use of possessive operators in array syntax since I just started using 2.0 and Harlowe about a week ago. I'm sure if you just keep the thread bumped, someone with more experience will jump in and help. :)
  • wingsofwax wrote: »
    ... but could there be other more traditional ways of getting data out of arrays - like "Xth" instead of "5th", for example?
    You currently have one option for using variable to access array element and that is Harlowe's (subarray:) macro:
    (set: $mapArray to (a:))
    (set: $mapEntry to $mapArray.push((a: 0,0,0,0), (a: 1,1,1,1), (a: 2,2,2,2)))
    
    (set: $index to 2)
    (set: $item to (subarray: $mapArray, $index, $index))
    
    item: (print: $item)
    items first: (print: $item's 1st)
    

    Harlowe's overview / commit log did mention something about being able to use variables directly in a future version of Harlowe, so you may want to check the beta version of Harlowe to see if it was implemented. From memory it was something like the following:
    (print: $mapArray's ($index))
    
  • I'm using the most recent version and Harlowe, no experience in coding, and I'm trying to wrap my brain around a lot of stuff that ... most of it is going over my head, but I'm working on that.

    My question is more about displaying the map. For some reason, if I use the (print:) function to show the whole map, it shows commas and all.

    In the video from Dan Cox (using Sugarcube) use the <for> to set up the map (replacing 0 with #, and 1 with -, etc), which I think in Twine2 and Harlowe would be the (replace:) function. But I don't know if that would do anything about the commas showing up.

    Not sure if that explains what I'd like to know. Am I right about the for and replace? Is there any more information from that video that has a parallel in Harlowe, but isn't the same? How do i get rid of those commas in the map print?

    And, ya know, any other knowledge that you'd like to pass unto me. :)
  • SugarCube's <<for>> macro allows you to do something again and again until a condition is meet.

    There is no direct equivalent in Harlowe but you can simulate it using a $variable and the (live:) macro:
    WARNING: If not careful doing the following could cause the Reader's web-browser to slow down or become unresponsive.
    (set: $counter to 0)
    (live: 500ms)[What you want to happen goes here!
    (if: $counter >= 10)[(stop:)]
    (set: $counter to it + 1)]
    

    Harlowe's (replace:) macro is using to replace the contents of a named hook with something else.
  • Hi everybody,

    another question concerning this topic. I have a one-dimensional array and I want a variable to move randomly up and down the array.

    I'm looking for the command that let's me add or substract a position in the array. Moving from the 5th to the 4th position for example.

    I'm using Twine 2.0 and Harlow.

    Thanks

  • So this is what I wrote so far:

    Passage Name: INI
    (set: $mapArray to (a:))
    (set: $mapEntry to $mapArray.push((a:0),(a:1),(a:2),(a:3),(a:4),(a:5),(a:6),(a:7),(a:8)))
    
    (set: $X to $mapArray's 1st)
    (set: $Y to $mapArray's 5th)
    

    X is you. Y is the enemy. Every time you make a move up or down the array, Y makes a random move up or down the array.


    Passage Name: UP
    (set: $X to $mapArray's ($pos + 1))
    (if: (either: 0,1) is 1)[(set: $Y to $mapArray's ($pos +1))]
    (else:)[(set: $Y to $mapArray's ($pos - 1))]
    

    When I execute it, it doesn't change the position though and I get no errors. What am I missing?
  • Your example does not state what value you have assigned to the $pos variable but besides that the UP passage code will either:

    a. assign the same value to $X and $Y, that value being whatever is stored in the $pos element of the $mapArray array.

    b. assign $X the value stored in the $pos element of the $mapArray array and assign $Y the value stored in the $pos -1 element which if equal to -1 will be the value stored in the last element of the array.

    eg. If $pos is 1 then $X will equal $mapArray's 1st and $Y will equal either the same as $X or it will equal $mapArray's (0) => $mapArray's 8th
Sign In or Register to comment.