Howdy, Stranger!

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

[Twine 2] [Sugarcube 1] Creating and Calling a Function passage. Possible?

edited March 2017 in Help! with 2.0
As I believe TheMadExile can attest, I have absolutely NO experience with JavaScript, so I was wanting to do this as a passage, because I can already see how the code should be shaped in Twine/Sugarcube.
I want to have some code that will check to see if certain variables are true (equipped inventory items in this case), and return a variable for the passage. This way, I can have events which will be able to check and describe equipped items, and possible react to them, without having a huge pile of If/Else statements clumping up EVERY SINGLE TIME I want to pull this off.

I wouldn't worry too much about a few extra If Trees, but since I will almost certainly add more equipable items as I work, it would be nice to have a single function somewhere to add them to, instead of having to go hunting through every passage that does a clothes check.

EDIT: Is there some perfectly simple method of doing this that I'm completely missing?

Comments

  • Next time please state the full version number of the story format you are using, as it can make a difference.

    1. You can use a passage to store commonly executed code but unfortunately a passage does not return a value, you can get around this by using a variable to store the result of that code instead.

    notes:
    . The following is an example of how to use a variable to return the result of code stored in a secondary passage, it is not an example of how to check the contents of an array.

    . SugarCube 1.x does not have temporary variables so the example with use the setup object to store the result in, this is to stop the result from being stored in History.

    1a. Use something like the following to call your Function Passage:
    <<set $list to ["apple", "banana", "cantilope"]>>
    
    /% initialise the variable being used to store the result. %/
    <<set setup.result to false>>
    
    /% Call the Function Passage. %/
    <<display "Check Some Condition">>
    
    The condition was <<print setup.result>>
    

    1b. An example Check Some Condition passage
    <<silently>>
    /% initialise the variable being used to store the result in case the calling passage forgot to. %/
    <<set setup.result to true>>
    
    /% Run some function code, in this case testing the contents of the $list variable. %/
    <<if $list.length is 3
    	and	$list.includes("banana")
    	and ! $list.includes("melon")>>
    
    	/% update the variable being used to store the result. %/
    	<<set setup.result to true>>
    <</if>>
    <</silently>>
    


    2. A much better way to do the above is to create a <<widget>> because they support the formal passing of parameters, although they also don't return a value so you will need to use the variable updating technique here as well.

    If you decide to use a <<widget>> instead I or another person can create you an example.
  • I would definitely suggest a widget, for exactly the reason mentioned by greyelf, you can pass arguments to them—even if you never do, having the ability is generally better than not.
  • edited March 2017
    Wwwwwwidgets.
    Actually, this is exactly what I was looking for. Thank you both!

    ...I'm going to go hide my head under a pillow now, for not spotting these before...

    Also, yes, sorry, I'll be more specific about the version numbers going forward.
Sign In or Register to comment.