Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Scripting One-liners

A topic by Internet Janitor created Jun 18, 2023 Views: 972 Replies: 7
Viewing posts 1 to 4
Developer(+4)

A little bit of scripting goes a long way in Decker. Here are some beginner-friendly ideas for adding interactivity to your decks with a single line of code!

Flipping a Coin

Make two or more cards and give them names. You can edit a card's name in the "Card -> Properties..." dialog. Then, make a button and fill in its "on click" function like so, substituting your card names for "heads" and "tails":



"random[]" will choose a random item from a list of items- in this case, our cards. If "go[]" is given a card, it will ask Decker to switch to that card. You could even place your thumb on the scale and make certain options more likely than others:


Opening a Hyperlink

If you provide "go[]" with a string that begins with "http://" or "https://", it will ask Decker to open a link to a web page. You can use this in a button script:



Alert, Alert!

The alert[] function displays a modal alert box. In the example below, I'm combining this with an "Invisible" button.



The Decker reference manual describes how you can give alert[] more parameters to pop up different kinds of modals.

Showing and Hiding

Draw a background for your card, then draw an image to place on top of part of that backrgound. You may want to use transparency mask mode ("View -> Transparency Mask") to make parts of your cover image opaque white Copy your "cover" image to the clipboard, switch to widget mode, and choose "Edit -> Paste as new Canvas". Position that canvas appropriately, remove its border, give it a name (in this case, "cover"), and make it transparent with "Widgets -> Show Transparent".

Now you can make a button to control whether or not this canvas is visible. The "toggle[]" function on widgets will flip the ".show" property of a widget between "none" and a specified alternate mode. In this case, "transparent":



Note: when a canvas is set to "Show None", it doesn't respond to "click" events. You can use toggle[] to hide buttons or other widgets this way, too!

Nixing Navigation

By default, in "Interact" mode, Decker allows the user to flip between cards with the cursor keys on their keyboard. To prevent this, we can override the default behavior of the "navigate" event by defining an empty "on navigate" function on a card (Card -> Script...):


(Would this be a "zero-liner"?)

If you want to disable keyboard navigation for an entire deck, define this empty function in the deck script instead by choosing "File -> Properties... -> Script...". You will still be able to use keyboard keys to flip between cards while using editing tools.

Questions? Have a neat use for one of these, or some one-liners of your own? Share it here!

(+1)

Hi! I've been building something in Decker (love it, having a great time) and I'm wondering if it would be possible to set up some kind of code where the deck navigates to a new card only if certain other cards have already been viewed?

Developer

Sure!

The main decision you'll need to make is how to represent whether or not the relevant cards have been viewed. One simple approach might be to put an invisible checkbox on those cards; say, name it "visited".

In the view[] event of the card, you could set the flag:

on view do
 visited.value:1
end

And then in some other script you could check the flag:

...
if thatCardYouVisited.widgets.visited.value
 # ... do one thing
else
 # ... do something else
end

You may also want to write a script somewhere to reset all the flags while you're testing your game.

There's more discussion of a similar kind of question in this thread.

Does that help?

Thank you very much for the response! Haven't managed to get this to work yet but I'm quite sure that's user error on my end, going to keep fiddling away. 

(+1)

Hello! Before anything else, I want to say how much I'm enjoying learning to use Decker---it's a real delight to make games in, and to just make fun UIs with. The reason I'm writing to you now, though, is that I've been struggling to override the default behavior in response to a link event. For example, there is a field I want to toggle the visibility of in response to a link event, so I wrote this in the script of another field that contains the link:

"on link x do

       examplefield.toggle[]

end"

but the deck still seems to be trying to navigate to a card named x instead.  Is there a different way I should be trying to override this behavior? Also, since I have your attention, is there a way to embed widgets within a rich text field? Thanks in advance for the help! Also sorry if these questions are strange or simple, I'm pretty new to coding and such.

Developer

What you're describing sounds like it ought to work. I would recommend making sure you have the script on the correct widget and checking carefully for typos. If it helps, here's a GIF of building the arrangement you describe from scratch:


It is not possible to embed widgets in rich text fields: they can only contain text (in multiple fonts), some of which can be hyperlinks, and inline images. With some fancy scripting and/or animated patterns it is possible to animate inline images within rich text. Here's an example with an "animated" field:


The script on the field:

on view do
 i:first extract arg where arg..type="image" from me.value
 i.paste[ref.copy[].rotate[sys.frame*.05]]
end

If you want to create the appearance of widgets within a rich text field, there are a variety of ways you might approach it. You could simply draw a card background that looks like interspersed text and widgets. (Using "Edit -> Copy Image" on widgets copies an image of them to the clipboard that you can then paste to the background and edit with drawing tools.) You could position widgets on top of a rich text field. You could even build Contraptions that contain other widgets (or images of them) stacked on top of a rich text field. I would recommend starting simple.

Thank you so much for your response! It really helped clarify what exactly my problem is. The field that contains the link I want to click to produce this behavior is embedded within a custom contraption, and for some reason that is causing some hiccup in my attempts to override the default behavior. So far I have tried adding this code to the script for the custom contraption and adding this code to the script for the embedded field in the prototype, but no luck yet. I'll keep trying though!

Found a hot-fix to my problem! For anyone curious, I just had to go into the prototype and override the link function in the embedded field (while being sure to point outside of the prototype with something like "deck.cards.exampleCard.widgets.exampleContraption.toggle[]"). In any case, I am having a blast making this game and am excited to share when it's in a more complete state.  Also looking forward to the Decker Summer Jam!