Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

How to make a global function?

A topic by Robot Face created May 11, 2024 Views: 156 Replies: 2
Viewing posts 1 to 2
(+1)

So, in the deck script, I have a function stat_to_mod:

on stat_to_mod x do
 floor (x-10)/2
end

Most widgets can use stat_to_mod[20] in their scripts and properly get the result 5, but for contraptions and widgets inside contraptions, it always returns 0 regardless of the argument. deck.stat_to_mod[20] also doesn't work (though It also doesn't work outside of contraptions so I didn't expect it to). The decker docs describe deck as a global constant, so I'm confused why this doesn't work.

I feel like I'm misunderstanding something important. Is there a way to achieve what I want to achieve?

Developer(+3)

Contraptions are independent, encapsulated units. Events generated inside a contraption do not automatically bubble up to a deck script in the way  events bubble up from widget -> card -> deck. Functions declared in a deck script (or for that matter a card or widget script) are not directly accessible as attributes. By designing contraptions not to specifically depend on the structure of the deck, they're more reusable.

You can, however, explicitly send an event to the deck with "deck.event[]". Given your deck-level declaration, from anywhere you have access to "deck" you can do something like

deck.event["stat_to_mod" 20]

Which will return "5". Using the same approach for sending events to cards or widgets can be extremely useful. For example, simulating a user's click on a button:

mybutton.event["click"]

Does that make sense?

(+1)

Yes, it does. In fact, I had even already tried to use deck.event, but forgot the quotation marks and assumed it didn't work. Thank you so much!