Skip to main content

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

Reset values when seeing a card

A topic by mzungushao created Aug 30, 2024 Views: 177 Replies: 2
Viewing posts 1 to 2
(+2)

Hello! First of all, pardon my etiquette, it's my first time posting on a forum. It's also my first time actually being able to "program" something. I'm so grateful for Decker. I've tried many tools, and at last, one that clicks for me! Thank you. I'm going through tough times and Decker gives me something to focus on. It feels great to create something.

Now for my question. I have a card with many buttons that add values to specific fields. The script is also toggling canvas in the right order. Everytime I test my game and go back to that card, I have to manually reset the fields to zero, as well as untoggle the canvas that are now visible. I was wondering how to avoid that. 

I tried to make a card script for the fields:

on view do
    display.text: if val > 0
        display.text:0 

But when I do that, the button that's supposed to add 1 to the field stops working.

Developer(+1)

Welcome! I'm delighted to hear that you're having a positive experience expressing yourself with Decker, and I'm glad we can offer you some solace and distraction from whatever you're going through. I originally created Decker while going through a difficult time of my own.

It looks like the script fragment you provide doesn't have the correct Lil syntax, which might cause problems elsewhere. The "on ... do" and "if" keywords always need to be paired with "end", like so:

on view do
 display.text:if val > 0
  display.text:0 
 end
end

I'm also not sure where "val" is coming from in this example. For the sake of my explanation, I'll start from scratch. Let's say that we have a card that looks something like this, with two fields and a canvas:


If we wanted to clear these every time the user reloads the deck, there's one option that doesn't involve any code: you can configure these widgets as "Volatile" (See the Widgets -> Volatile menu item). A volatile field "forgets" its text contents when a deck is saved, and a volatile canvas likewise loses its image contents. You can also manually discard the contents of all volatile widgets in a deck at any time by using File -> Purge Volatiles from the menu. This approach isn't very flexible, though; there isn't a way to simply hide a canvas instead of automatically clearing it, for example. Still, this might be useful for some scenarios.

You could give the card a "view[]" event handler script (see Card -> Script... in the menu) to reset the widgets, like so:

on view do
 c.show:"none"
 f1.text:0
 f2.text:0
end

But view[] isn't exclusively called when a card is first visited- it also activates any time you leave the editing tools (which may be fine), or when modal dialogs are dismissed. It's really intended more as a way to refresh the contents of the card than to initialize it.

You could tweak the above event handler to respond to the event "init" instead:

on init do
 c.show:"none"
 f1.text:0
 f2.text:0
end

Decker doesn't ever automatically call "init[]", but you can trigger it explicitly from a script. Let's say that you had a button on the first card of your deck that resets the game, and our init[] function and the widgets we wish to reset live on a card named "Bournemoth" (see Card -> Properties in the menu to set a card's name). The button's script could say:

on click do
 Bournemoth.event["init"]
end

Which would call the "init[]" event on the card Bournemoth (no matter where the original button resides) and the init[] handler on Bournemoth will reset it to an initial state long before our visit.

Does that explanation make sense?

(+2)

It makes sense and beyond eheh! Thank you, happy to learn. As to where the "val" is coming from, I took it from The Decker Tour deck, Example 2. Starting with 0 programming skills, I tend to heavily rely on bits and pieces of codes here and there.