Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+1)

Passing arguments with event calls from Contraptions?

Hi! I was going through the "Breakout Three Ways" tutorial, and decided I wanted to make my own game-like object. I have an array of tiles (Contraptions from a Prototype Tile) and when the user clicks one, I want it to pass the tile's label (kept in a hidden field) to the top level Card script. That top-level script looks like this:

on moving which do
 alert["Yes, this is the card top level."]
 alert[which]
end

So, the event should be "moving[]" which accepts an argument (`which`), right?

So, in my Prototype, I have this script attached to the invisible button that makes up the tile's face. The "which" field is hidden, keeping track of the label for the tile:

on click do
  card.parent.event.moving[which.text] 
end

This is based on the "Breakout" example, but none of the Card level event handlers take arguments, so I'm trying to supply the argument as I would with a regular function. I've also tried without the brackets and using literal strings or numbers instead of the text from the hidden "which" text field. And every time, all I get is zero, or null or empty (with event handler code above).

Can someone give me a hint on where I've gone wrong? This seems to be a reasonable way to call the event (other events seem to do it?). But if I can't get it to work, I'm going to have to do something stupid like set a flag on the Contraption and call the event and have the handler grovel through all the widgets to find out who's got the flag set.  I guess if it works, it's not stupid, but it seems inefficient since I know what tile is raising the event?

Thanks!

(1 edit)

In Lil, dot-indexing with an identifier and bracket-indexing with a string are equivalent:

card.parent.event.moving
card.parent.event["moving"]
card.parent["event"]["moving"]
card["parent"]["event"]["moving"]

This is a very convenient shorthand when functions take a single string as their argument, but I can see how it might leave you puzzled about passing extra arguments to the "event" method of deck-parts. You're looking for something like:

card.parent.event["moving" which.text]

Make sense?

(1 edit) (+2)

Okay... So I guess this is the relevant bit in the manual:


I thought that ".event" was just a dictionary, not actually a function (method?) that was taking event names(and optional arguments) as its arguments. Obviously, I need to read more carefully... Thanks very much for pointing me in the right direction!


With that resolved, I think I'm getting very close to the endgame here!

(+1)

Hi! Thanks again for your help! I got it working: https://oofoe.itch.io/puz15
(password "puz15" -- I just don't want it showing up on my profile yet because not finished.)

Another question, if you're willing... I'm trying to use the array language capabilities to figure out if there's an opening to move a tile into by vector adding to get the orthogonal neighbours:

np:2 window (p,p,p,p)+(1,0,0,1,-1,0,0,-1)

Then I get the contents of my tracking map (canvas that I write the actual tile positions into) at those positions to determine if one of the neighbouring spaces is empty (=0):

0=map@np  # Yields something like (0,1,0,0)

I know that a lot of array languages have some kind of operator to filter one array by a boolean other, so I could apply the result of the 0=map@np to np and arrive at just the coordinates of the empty space (or nil, if no empty space in neighbours). Currently, I'm grovelling through with an each loop -- is there a way to one-shot the operation like that?

Thanks!

In K, you'd solve this kind of problem with the "where" (&) operator:

 &0 1 0 0 1
1 4
 *&0 1 0 0 1
1

Lil doesn't have an independent monadic primitive for this operation; it's folded into the query language:

first extract value where 0=map@value from np

This is a little bulky when you're doing something simple.

When you only need a single result, you could consider forming a dictionary:

((0=map@np)dict np)[1]

It's also possible to perform an equivalent "masking" operation just with arithmetic:

m:0,0,0,1,0     # "one-hot" boolean vector
m*keys m        # (0,0,0,3,0)
sum m*keys m    # 3