Skip to main content

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

Lil Programming Questions Sticky

A topic by Internet Janitor created Oct 28, 2022 Views: 33,600 Replies: 545
Viewing posts 141 to 150 of 150 · Previous page · First page
(+2)

Trying to decode images using app.params, but IMG3 images will not render correctly:


It seems random where the image will cut off, sometimes the display is nearly perfect except for a pixel or two. Could it be a limitation of URL parameters or a bug of some kind? Here's the script I'm using:

on view do
p:app.params
c.paste[image[p["img"]]] end
Developer(+2)

Base64 encoding as used in %%IMG datablocks in Decker uses digits, upper- and lower-case letters, and the symbols +, /, and =.

URL encoding leaves digits and letters unchanged, but reads + as a space and / is technically reserved. If I open web-decker with a URL like:

http://beyondloom.com/decker/tour.html?i=a1+/b2

app.params comes out as

{"i":"a1 /b2"}

If I instead encode that + and / with the appropriate percent-codes:

http://beyondloom.com/decker/tour.html?i=a1%2b%2fb2

I get the intended payload:

{"i":"a1+/b2"}

I think this explains the behavior you're seeing; your image-string is getting scrambled before you try to decode it.

Here's one way to encode those special characters- and other reserved chars, too- if you're using a Lil script to assemble the URL:

on url_escape x do
 a:"" split " !#$&'()*+,/:;=?@[]"
 b:3 window "%20%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D"
 rtext.string[rtext.replace[x a b]]
end

If you're exclusively pasting Decker image strings into URL parameters I guess it might also work to translate spaces back into + before decoding?

How's that?

(+2)

Exactly what I needed - thank you!

(5 edits) (+2)

Hello, I apologize that I am brand new to decker and lil! My goal has been to create a small AIM-style text chain for a bite-sized narrative thingy in Decker, and I figured using a table would be the best way to store all the usernames, however, trying to return to the card or another script asks me to discard the code with this error message (IGNORE THE PERIOD AT THE END OF THE chat.message DECLARATION as this was a decoy list for demonstration purposes)

my hello world

I am unsure what name parameter is needed, whether it is by the on view do function, whether me calling for the creation of a table is not necessary, or not defining what 'chat' is. I also am unsure what a name parameter is and how it's different from a string value.  I also am unsure if I need to put this table in a different script. It is currently in the home card script, but I don't know if it would instead go into the contraption instances? 

Goals for the text message game: display corresponding messages with usernames via stylized fields, have them drip feed out over different intervals of time depending on which message is next, have a small field players can type in and send something, which functionally causes an ending screen if done.

in general, I find the way the lil and decker documentation is written makes it hard to immediately intuit where I can use something written in the lil documentation in decker, such as a list, dictionary, or table. I think the fact I know what I want to do in terms of knowing how programming languages work but not getting used to the syntax nor the scripting interface of decker is just me making it harder on myself. I chalk it up to my brain and not a critique on the way these tutorials are written though.

Perhaps pre-existing guides on making IM or text message contraptions would also be helpful.

(+2)

Hello, welcome! I've seen several different text message situations in Decker games, with different behaviors and narrative purposes so it's hard to answer without knowing more about what you want for your own project. I'll start with clarifications and then go into some questions if that's okay?

I'm not seeing what in your script screenshot could be causing that error message specifically, so I suspect there's something else in the non-decoy version which is causing that.

It should just be a syntax error. Somewhere in your script Lil expected to find the name of something like a card, a widget, a variable or a function and what it found instead was a string (text enclosed in a pair of double-quotes). Again, I can't see the cause of this error in the decoy script as written.

The other thing you mentioned which I need to ask about is Contraptions, which the name for a reusable prototype made of bundled widgets and Lil script.  The pre-built ones made by members of the community (such as in this forum's Contraption Bazaar) are usually easy to use and modify. On the other hand, making Contraptions from scratch is difficult without familiarity with Decker and Lil.

Did you mean widgets (the basic forms of interactive objects in Decker)? Or if you are using Contraptions it would be helpful to know more about them since they might have their own scripted behavior already.

Okay! Now let's talk about what behavior you're aiming for.

I know this involves your actual planned series of screen names and messages and some stylized fields that they'll be displayed in.

But in terms of drip feed, do you mean:

  • messages appearing word-by-word or letter-by-letter?
  • sometimes adding a new message to a log of previous messages? 
  • updating a specific field to display only the most recent message? 
  • and do messages even vanish?
  • should the new message be displayed on a timer, or is there some other trigger you have in mind?

Knowing any of these specifics (or something else!) would make it possible to write guidance to build the specific version of text message interface that you want for your project.

Sorry to not have a one-size-fits all suggestion for you yet, but it seems like you have a specific vision in mind and I'd like to help you get there!

I'd like to make a button that operates similarly to using ctrl+z on a keyboard,  my initial thought is:

on click do danger.js["undo()"] end

However...

Directly calling internal Decker functions is not advised, as these do not necessarily represent a stable API; prefer passing Lil functions into JS to allow it to manipulate the Decker environment whenever possible.

I'm assuming this part of the reference is referring to what I'm trying to do here, but if that's the case, how would I go about doing it properly?

Developer(+2)

If Decker itself doesn't have APIs for doing something, and you're willing to constrain an application to only work in web-decker, calling internal functions via danger.js[] may be the only way to make it happen; I just reserve the right to change those internal functions in the future.

I'm a bit puzzled by the specific example you're raising, though. Clicking a button widget to fire a script takes place while in "interact" mode, whereas the internal "undo()" function in web-decker is part of the apparatus used in "widget" and drawing tool modes. Can you provide some broader context for what you're trying to do?

(+2)

Compared to decks like tour.html, Patternshop Cinnamon runs really poorly on my phone. In an attempt to make a more mobile-friendly version, I cut down any cosmetic widgets and significantly shrunk the deck size down. The menu bar is hidden by default to save on space, but I kept the toolbars visible. Given the lack of keyboard and menu bar, there's no way to undo or redo from within the deck - That's where my current road block lies.

(+3)

FYI base Patternshop (not Neo) has its own undo stack, outside of Decker’s undo, if you want to copy that code.

(+2)

Oh - and to connect with what IJ said - I wrote base Patternshop’s undo stack for exactly what he said: so it can undo inside of Interact mode.

Which makes me think, yeah, that’s probably why base and old Patternshop have on view methods… and why it can be slow.

Either way, I’m sure we can come up with optimizations to run better on mobile and allow Cinnanom to continue working the way you designed it.

(2 edits) (+3)

Also, IIRC the way old Patternshop works probably isn’t super optimal for your use case, because I believe it has on view methods. I vaguely remember this after looking at Cinnanom’s code. Neo does things slightly differently, which might be more optimal and less laggy for you.

Also, we could try packing your deck with my decker-xdc - it has a few optimizations for web decker that may help. I know it makes Wiggly Paint run a lot smoother on my laptop (Firefox, so no V8, and circa 2018-ish laptop). (decker-xdc packer doesn’t require WebXDC, it can do vanilla, too.)

(+3)

Anyways, I’d be willing to take a look and try some things out if you like - just let me know.

(+2)

That'd be fantastic, thank you. ^^ I did swap over to using Neo for Cinnamon in the recent update, and most operations are located in the script for the card itself.  There were some changes to Neo, which I documented here. One very noticeable slowdown occurs when moving and deleting layers, whereas I'm adding to the load by trying to refresh all of the patternshop contraptions using renderAll[] (Function to force an internal view[] event).

Deleted 48 days ago
Deleted 47 days ago
Deleted 45 days ago
Deleted 46 days ago
(5 edits) (+2)

Done

Update:

  • I fixed the two bugs that I made
  • There’s also a webxdc packed version in the repo so I can use it over DeltaChat for my own usage (it doesn’t synchronize, but that feature could be easily added)
(2 edits) (+2)

Also, if you want to wait until 1.72, it looks like IJ has incorporated similar performance boosts for image paste in base Decker so you don’t have to use my hacked version.

Let me know when you get a copy of the .deck and I’ll remove the repository from my codeberg page.

(+3)

I got it just now - thank you again for looking into this on my behalf. I can only assume IJ was also deep in thought about any optimizations that could be done.  

Nonetheless, your notes were insightful (There's probably some other things I can do to slim down the widgets...) and I'll be using your hack as a basis for 1.2 + My mobile cinnamon project (which might be evolving into a brand new subspecies of patternshop altogether). In fact, I already had your fonts downloaded and added to the new version before I saw your reply. :3

Developer (2 edits) (+3)

Woodring definitely deserves credit here. I took a somewhat different approach in my patches and added a few additional ideas of my own after some careful benchmarking, but Woodring's post made me realize I'd left significant low-hanging fruit in image.copy[] and image.paste[] (as well as a variety of places throughout Decker which rely on the same internal routines.) I think v1.72 will offer a significant boost to many existing projects.

Thanks to both of you for pushing the limits of the platform!

(+2)

Glad to be of service - thanks to you both as well

(+2)

Also, I just merged your image_paste changes in over the top of my lil.js fork and it gave me several more frames per second. It’s very nice.

Missooni, I can cut you another copy of Cinnamon packed in html with those changes. Just say the word.

(+2)

By all means! I'd be grateful. ^^

Is it possible to have the drag event on a canvas affect a certain area of that canvas? Say:

on drag pos rect[(1,1) (10,10)] do
 [script here]
end

or test if the pattern of a pixel in a canvas is a certain one to execute a script?

on drag pos do
 if pattern = 1
  [script here]
 end
end
(1 edit) (+2)

You can test whether the drag position is within a given rectangle like this:

if min (pos > 1,2),(pos < 10,20)
 alert["You clicked in the rectangle!"]
end

pos is an two-item list representing the X and Y coordinates of the pointer when the event happened. If we pretend there are separate x and y variables representing the individual coordinates, then pos > 1,2 is a shorter way to say (x > 1),(y > 2) - that is, you get a new two-element list where each item is 1 if the corresponding coordinate matches. In the same way, pos < 10,20 is like (x < 10),(y < 20). Put them together with “,” and you get a four element list where each item is 1 if the coordinate is inside that edge of the rectangle. min takes that four-element list and returns the minimum value: if any element is 0, it returns zero; it only returns 1 if every element is 1. That is, it only returns 1 if the point is completely within the rectangle.

It’s also possible to check the pattern in the canvas at a point, like so:

if me[pos] = colors.red
 alert["You clicked a red pixel"]
end

(where me inside a widget’s script refers to that widget, whatever its actual name is)

Does that help?

(4 edits) (+1)

Very much, thank you!

Though, now I have a new problem.

I got a canvas, a slider (Switch), and a field (Circle) with two images.

The Code for the canvas

on click pos do
 if me[pos] = colors.black
  Switch.value:Switch.value + 1
 end
end

And the Code for the slider

on change val do
 if Switch.value = 1
  canvas1.paste[Circle.images[1]]
 else
  canvas1.paste[Circle.images[0]]
 end
end

When a black pixel is clicked, it's supposed to change the value of Switch from 0 to 1 then paste the image of that value from Circle into canvas1. It does the second step, but not the third. And the slider works when clicking on it but not when changed by another widget.

Figured it out! Here's the code:

on drag pos do
val:Switch.value
 if (me[pos] = colors.black) & (Switch.value = 0)
  Switch.value:Switch.value + 1
 elseif (me[pos] = colors.black)
  Switch.value:Switch.value - 1
 end
 Switch.event["change" val] 
end
(+1)

Yeah, assigning to a widget’s .value does not actually trigger the widget’s on change handler. That is a good thing - for example, you might have a slider and a field, so the player can enter a number by quickly scrubbing along the slider, or typing a precise number in the field. Changes to the slider need to set the field text, changes to the field text need to set the slider value, so if setting .value automatically triggered on change, it would get caught in a loop: changing the text would set the slider, which would change the text, which would set the slider… The way it works now, each field can just set the other’s value to match its own without any mishaps.

For what it’s worth, your code could be simplified a little. Since both arms of the if test me[pos] = colors.black, we can lift that condition out:

val:Switch.value
if me[pos] = colors.black
 if (Switch.value = 0)
  Switch.value:Switch.value + 1
 else
  Switch.value:Switch.value - 1
 end
end
Switch.event["change" val] 

That inner if statement now just makes Switch.value into 1 if it was 0, and 0 if it was 1. It turns out Lil already has an operator that does exactly that: the ! operator.

val:Switch.value
if me[pos] = colors.black
 Switch.value:!Switch.value
end
Switch.event["change" val] 

Lastly, I see you’re sending the “change” event with the original value, even if the new value is changed. Normally the “change” event gets called with the new value, so that can get confusing if sometimes it gets the new value (from the user) and sometimes the old (from code like this). Unless you specifically need it to be the original value for some reason, it’s probably better to send the new value - then you don’t need to save the original value at all. Also, the “change” event is typically only sent when something actually changes:

if me[pos] = colors.black
 Switch.value:!Switch.value
 Switch.event["change" Switch.value] 
end
(+2)

Thank you for the optimization!

Unironically, I love it when, indeed, a program could be simplified further.

(+1)

im having a tad difficult time understanding contraptions.. can they be draggable?

my contraption in question is just made of one canvas; the canvas itself is draggable but (naturally) it doesnt move the rest of the contraption with it. what is a good approach to this?

(3 edits) (+2)

You can. This is how I’ve done it:

%%WGT0{"w":[{"name":"cammy1","type":"contraption","size":[100,30],"pos":[125,36],"def":"cammy","widgets":{"cammy_edit":{},"drag_handle":{},"resize_handle":{},"offset":{}}}],"d":{"cammy":{"name":"cammy","size":[100,30],"resizable":1,"margin":[16,15,16,15],"widgets":{"cammy_edit":{"type":"field","size":[70,30],"pos":[15,0],"script":"on change val do\n  if (\"\\n\" in val)\n    val: \"\" fuse on _ x do if (x~\"\\n\") \"\" else x end end @ val\n    show[val]\n    p: (list \"%v%s %n\") parse val\n    show[p]\n    t: p[0]\n    n: p[2]\n    valid: 0\n    if (\"field\" in t) |\n       (\"canvas\" in t) |\n       (\"slider\" in t) |\n       (\"button\" in t) |\n       (\"grid\" in t)    \n      w: card.parent.add[t]\n      w.pos: card.pos\n      w.size: card.size\n      valid: 1\n    else\n      w: card.parent.add[\"contraption\" t]\n      if !(w~nil)\n        w.pos: card.pos\n        w.size: card.size\n        valid: 1\n      end\n    end\n    \n    if valid\n      p: (list \"%v%s %j%s %n\") parse (n drop val)\n      k: p[0]\n      v: p[2]\n      n: n + p[4]\n      while !((k~nil) | (v~nil))\n        w[k]: v\n        p: (list \"%v%s %j%s %n\") parse (n drop val)\n        k: p[0]\n        v: p[2]\n        n: n + p[4]\n        show[p]\n      end\n      card.parent.remove[card]\n    end\n  end\nend"},"drag_handle":{"type":"canvas","size":[14,14],"pos":[0,0],"script":"on click pos do\n  rel_pointer: pointer.pos - card.pos\n  offset.text: (\"%J\" format (list (me.pos - rel_pointer)))   \n  cammy_edit.locked: 1\n  resize_handle.draggable: 0\nend\n\non drag pos do\n  card.pos: pointer.pos + (list (\"%J\" parse offset.text))\n  me.pos: (0,0)\n  cammy_edit.locked: 1\n  resize_handle.locked: 0\nend\n\non release pos do\n  card.pos: pointer.pos + (list (\"%J\" parse offset.text))\n  me.pos: (0,0)\n  cammy_edit.locked: 0\n  resize_handle.draggable: 1\nend","draggable":1,"scale":1},"resize_handle":{"type":"canvas","size":[14,14],"pos":[86,16],"script":"on click pos do\n  rel_pointer: pointer.pos - card.pos\n  offset.text: (\"%J\" format (list (me.pos - rel_pointer)))\n  cammy_edit.locked: 1\n  drag_handle.draggable: 0\nend\n\non drag pos do\n  offset: (list (\"%J\" parse offset.text))\n  rel_pointer: pointer.pos - card.pos\n  me.pos: rel_pointer + offset\n  card.size: me.pos + me.size\n  cammy_edit.size: me.pos + (-16,14)\n  cammy_edit.locked: 1\n  drag_handle.draggable: 0\nend\n\non release pos do\n  offset: (list (\"%J\" parse offset.text))\n  rel_pointer: pointer.pos - card.pos\n  me.pos: rel_pointer + offset\n  card.size: me.pos + me.size\n  cammy_edit.size: me.pos + (-16,14)\n  cammy_edit.locked: 0\n  drag_handle.draggable: 1\nend","draggable":1,"scale":1},"offset":{"type":"field","size":[40,14],"pos":[-50,-20],"value":"[-4,-5]"}}}}}

It does it by making canvases in the widget draggable and then updating the contraption it belongs to. It’s not particularly intuitive, which is why I copied the whole contraption so you can copy from it or modify it.

There might be better ways of doing it. I haven’t revisited that code in a while and it was one of the first contraptions I made when learning Decker and Lil.


Though, if the contraption is just composed of one widget, a canvas, does it need to be a contraption? Can you put the code in the canvas’s script, instead? Then, you could copy that canvas around if it needs to be replicated.

(+2)

One deck of mine consistently crashes when navigating from card to card using the arrow keys, and today I realized it's a syntax error that isn't being caught by the 'Your script contains errors' validator. The script with missing brackets is 'valid' and will initialize the brushes, but will crash the deck after a certain amount of navigate[] events when ran at a deck level. The second one, with added brackets to surround the brush function, seems to have fixed the issue I've been having, but I was surprised that both were able to pass the check.

roundtips:"%J" parse "[
%%IMG0AAcABzh8/v7+fDg=,
%%IMG0AAMAA0DgQA==
]"

brush[on roundB1 delta newLine pos do
roundtips[0].copy[].map[(1) dict plight[deck.card.image[pos]]]
end

brush[on roundB2 delta newLine pos do
roundtips[1].copy[].map[(1) dict plight[deck.card.image[pos]]]
end
roundtips:"%J" parse "[
%%IMG0AAcABzh8/v7+fDg=,
%%IMG0AAMAA0DgQA==
]"

brush[on roundB1 delta newLine pos do
roundtips[0].copy[].map[(1) dict plight[deck.card.image[pos]]]
end]

brush[on roundB2 delta newLine pos do
roundtips[1].copy[].map[(1) dict plight[deck.card.image[pos]]]
end]

Was it my fork of lil.js? I just fixed a bug that I noticed I had a divergence with IJ’s base and it might be related. If not, never mind me.

(+1)

Unrelated - no worries! I should've probably noted I've only seen this happen in Native Decker.

Developer(+2)

Lil's parser will currently tolerate source code ending without the full number of closing braces and/or parentheses; they're just implied.  This design is a potential foot-gun, but mildly handy when using the Listener like a calculator:


I was able to reproduce the crash you describe, but it's not specific to those script typos; it's a deeper race condition in Native Decker that just happens to become easier to reproduce when you have the toolbars open and custom brushes installed. Will fix.

(1 edit)
on run val do
 if (me.text = "hello") | (me.text = "Hello")
  log.text:log.text + "
Hello to you as well!"
 else
  log.text:log.text + "
I do not understand."
 end
 me.value:""
end

I thought this code would allow me to add text to log, instead of rewriting it, but it just writes "0." Is there a way to add line breaks in a string and new text in addition to the already existing one?

Edit, figured out the line break, but I still can't figure out the adding text function.

Developer(+2)

In Lil, the "+" operator is exclusively for addition. The "," operator joins strings into lists, and writing to the ".text" attribute of a field implicitly concatenates together the elements of a list. In a string literal, a backslash followed by the letter "n" is an escaped newline.

Putting this all together, you could write that fragment as:

on run do
 if (me.text = "hello") | (me.text = "Hello")
  log.text:log.text,"\nHello to you as well!"
 else
  log.text:log.text"\nI do not understand."
 end
 me.value:""
end

You could further simplify the conditional by factoring out the common parts of each subexpression:

on run do
 log.text:log.text,"\n",
 if (me.text = "hello") | (me.text = "Hello")
  "Hello to you as well!"
 else
  "I do not understand."
 end
 me.value:""
end

To make the initial comparison case-insensitive, you could use the "%l" formatting pattern to convert the input to lowercase:

on run do
 log.text:log.text,"\n",
 if "hello" = "%l" format me.text
  "Hello to you as well!"
 else
  "I do not understand."
 end
 me.value:""
end

Does that help?

(+2)

Yes! Very much. I was bashing my head over the "fuse" function and getting compounded text, lmao!

In other words, this helped me a lot and I am, once again, amazed at how simple the solution is.

Thank you!!!

So... I'm trying to use a grid cell's value to store variables. Here's the code:

on run do 
 if Data.cell(0,0)=1 & ("room" = "%l" format me.text)
  Log.text:Log.text,"\n\n","> ",me.text,"\n","You are in room 1."
 elseif Data.cell(1,0)=1 & ("room" = "%l" format me.text)
  Log.text:Log.text,"\n\n","> ",me.text,"\n","You are in room 2."
 elseif Data.cell(2,0)=1 & ("room" = "%l" format me.text)
  Log.text:Log.text,"\n\n","> ",me.text,"\n","You are in room 3."
 elseif Data.cell(3,0)=1 & ("room" = "%l" format me.text)
  Log.text:Log.text,"\n\n","> ",me.text,"\n","You are in room 4."
 else
  Log.text:Log.text,"\n\n","> ",me.text,"\n","I do not understand."
 end

And the Grid value is

Rooms
0
0
0
0

Which should produce "I don't understand" but it says, "You are in Room 1." No matter how I format the grid itself.

Am I doing something wrong?

(+1)

I think you’re a bit confused.

Looking things up in an array, or calling a function, uses square brackets rather than round brackets. So for example, if you want the grid to scroll until row 23 is visible, you’d do Data.scrollto[23], not Data.scrollto(23).

Data.cell tells you which cell is selected, it doesn’t give you the contents of the cell. If a cell is selected it returns its coordinates, otherwise it returns the pair -1,-1. Since it always returns a non-empty list, it’s always considered truthy. So an expression like:

if Data.cell(0,0)=1 & (whatever) alert["hi"] end

tests Data.cell (which is always true), evaluates the expression (0,0)=1 & (whatever) and discards the result, then evaluates alert["hi"].

I don’t think there’s an easy way to get directly from coordinates to a cell value, but you probably want to be looking up a specific column by name rather than position:

if Data.value[0].Rooms=1 & ("room" = "%l" format me.text)

Well, so much for using grids to hold values. Does this mean I have to flood the card with sliders and checkmark buttons?

(+1)

What kinds of values are you trying to store?

Just numbers.

(+2)

You can store a bunch of numbers in sliders and checkmark buttons, but you can also store them in a regular field with the .data attribute:

field1.data:
 (list 0,0,0,0,0),
 (list 0,0,0,5,0),
 (list 0,7,0,0,0),
 (list 0,0,0,0,0)

field1.data[2][1] # evaluates to 7
field1.data[1][3] # evaluates to 5
field1.data[4][4] # evaluates to 0

The actual text in the field looks like [[0,0,0,0,0],[0,0,0,5,0],[0,7,0,0,0],[0,0,0,0,0]]

(1 edit)

And I can use that data for if-statements? Like

if field.data[2][1] = 5
 (whatever)
end

And do I have to write "list" for each array or can I name those lists?

(+2)

You can use that data for if-statements, or anything else you want.

The list makes each row a separate list, so in field.data[2][1] the 2 picks one of the rows, and the 1 picks an item from within that list.

The way you should organise your numbers depends on how you want to use them. If you just want a list of numbers to look up, a simple flat list might be fine. If you want a two dimensional grid of numbers, you can use nested lists (as described above) or maybe a Canvas, or a chunk of the card’s background (as discussed previously). If you don’t need to store information about every single possible thing you could look up and just want to record the most important ones, you can use a dictionary:

treasures[3,5]:"mirror"
treasures[2,7]:"beads"
treasures[1,4]:"sword"

field1.data:treasures

# ... later ...

t:field1.data[playerpos]
if t alert["You found the %s!" format t] end

If you have a bunch of things with different values for the same properties - for example, every monster has an X coordinate, a Y coordinate, and an amount of health - then you probably want a grid after all.

(1 edit) (+1)

How do I copy from an image from a segment of a card and paste in a canvas via script?

I tried both of these programs in the canvas:

on click pos do
 me.copy[Map.image.paste[]]
end
on click pos do
 me.paste[Map.image.copy[]]
end

but neither work. I might be misunderstanding something.

Edit: got it to work! Here's the code:

on click pos do
 me.paste[Map.image.copy[]]
end

Hey, I'm trying to create a drum sequencer in a contraption. However I don't think contraptions can play sounds, I first tried playing a sound in the main prototype script and that didn't work so I just tried a button and still that didn't work. I test outside of the contraption just on the main card and that worked. I'm using play["snare'] . is this just a technical limitation with lil or an issue i'm having? I might be completely wrong because I'm actively trying to learn lil with little to no coding experience. If thats a fixable issue i'd love for that to be in the next decker release but if not are there any workarounds?

(+2)

Hi, I'm sorry I'm answering this on my phone and can't go into super detail so let me know and I can work up some better examples. Contraptions can definitely play sounds but I think they aren't able to see deck level resources (like sounds that have been added). So I think what you'd need to do is create the sound resource inside the contraption.

If you run in the listener 'deck.sounds.soundname.encoded' it'll give you a string that represents that sound. You can then in the contraption create a sound object in your code using sound["whatever the string is"] and use that as the input for the play function.

Let me know if this doesn't make sense and I will try to work up some proper examples and explain better!

(I have had a bit of experiences making music sequencer type things in Decker haha, I look forward to seeing what you come up with)

(1 edit) (+1)

Thanks millie, that makes somewhat of sense if you could later go into a little more detail that would amazing! I'm a little confused on how I create a sound resource for a contraption, I'll have a squiz at the docs but I'd greatly appreciate some more detail. no promises on when ill release it but I would put it up on my website so you can check soon. 


edit: I just looked at your janky sequencer, that is seriously impressive, mine is not going to be that in depth but yk its for the love of the game <3

(+1)

OK, so I'm doing a bit of experimentation, and from what I can tell calling play["soundname"] in a contraption does seem to play the corresponding sound in the deck actually! Could be worth checking your syntax, since you've got a single quote in your example instead of a double quote? But what you've got should work, I think! If it's not working, maybe there's an example deck you can share so we can have a look?

But nevertheless, I'd say this isn't ideal for a contraption since a contraption should be transferable between decks as a single unit, and the sound resources don't come with it if you paste it into a new deck. So I think it's probably better if the sounds are contained in the contraption.

Essentially what you can do is in the code for the contraption (either in the code for a button or in the whole of contraption code, depending where you need it, do something like

sound1:sound["the big text string goes here"]

only using the output from running deck.sounds.soundname.encoded in place of the string.

And then you just call like sound[sound1] when you want to play it.

I did up a quick demo contraption, with one button that'll look for a sound named "amen" in the deck resources and play it, and one button that contains the sound itself encoded as a string. If you have a sound in the deck named "amen" (I just copied it across from the example sounds.deck) then both buttons should work the same way. But if you don't have a sound like this, the top button won't do anything, but the bottom button should. It looks like my contraption is too big for the itch editor so I've uploaded a deck containing it here: https://zine.milliesquilly.com/surprises/sounddemo.html

Feel free to poke around the contraption code, see how it behaves when copied into another deck, etc.

(+1)

Thanks so much, I think it might be something with my code in the contraption or its just bugged somehow, I tried the embed technique and it didn't work so I tried making a new deck with a new contraption and even just play["snare"] (the sound was just in the deck sounds) worked so I think its somehow messed up. ill share the deck on my website if you want to have a squiz at my dismal code. my ultimate goal is to make it so you can easily change the sounds by adding sounds to the deck named like track1, track2, track3 so its easily customisable. The current code I have should calculate the time for a step based off the bpm and the using the sys.ms interface, then it should run a loop for the amount of times the loop slider is set to. then it should check when the step time in ms has passed, then steps and then will check each check box if its check and then play the sound. Its convoluted as hell but i'm trying my best T-T also ignore the prints in the listener its me trying so hard to debug. My original plan was to make each check box check its self when the play button is hit and then each button manages its own sound but I can't seem to get that to work. Here is the deck link https://wieke.neocities.org/drumtest and I appreciate your help so much!

Developer(+2)

It looks like your contraption has a button named "play". This will shadow (take precedence over) the definition of the built-in function "play[]". You also have a function play[] defined in the contraption-level script. If you clear up the first problem by e.g. renaming the play button to "_play" or "start_loop" or something else distinct then your listener logging prints, but as written it calls itself recursively instead of calling the built-in play[] function.

It is possible for a function which overrides a builtin to use "send" to invoke the "next-higher up definition", like so:

on play x do
 show["invoked wrapper!"]
 send play[x]
end

Sometimes this can be a handy way to bodge in extra behavior or logging without modifying existing scripts.

...but you can also sidestep this issue entirely by using distinct names for your widgets and functions.

Does that make sense?

(+1)

Thank you so much! I half understood, that makes sense it wasn't working especially since "play[]" is a function and I had a function named the same thing. I changed the button name and function name and it's now playing the sound however it is playing them all at the same time. It may have to do with the way the "while L > 0" statement is setup and how it interacts with the function, the listener shows the loop variable doing some funky stuff. also I believe that it plays all the sounds at the same time because if you set the loop slider to a high number, the sound gets very distorted like many iterations of the snare are being played at the same time. I really don't know why this is, it may be because of the way the code is. I updated the link with my new attempt (I don't think the sounds work with neocities so maybe save the html and run it in your browser or something), please take a poke around and see if you can spot anything, again thank you guys for your support and I hope one day ill be competent enough in lil to make some really cool gadgets and stuff!

(1 edit) (+2)

Here’s your problem:

if sys.ms-LST > T | sys.ms-LST = T

Lil always evaluates expressions right-to-left, which means this expression is understood like:

if sys.ms-(LST > (T | (sys.ms-(LST = T))))

…which probably isn’t what you wanted. You can fix it by adding brackets in the places you want:

if ((sys.ms-LST) > T) | ((sys.ms-LST) = T)

…or by leaning into the right-to-left pattern with something like this:

if !T<sys.ms-LST

After making that change, it plays the first four beats correctly.

(+1)

Thank you so much, I have the first track working!!!! thank you to all of you who helped, I really appreciate it. I have just 2 more questions, since lil doesn't do statements like ">=" the way I do a greater than or equal to statement is like how Screwtapello said by using brackets and the or statement " | "? My other question is how does the script in a button work? because I can only get it to run the script when I click it even with out the " on click do" function, so do buttons like only update to run the script when clicked or am I missing something? again thank you all and thank you IJ for making decker lol

Developer(+1)

Lil has three comparison operators: equal-to (=), greater-than (>), and less-than (<). The negation of these (the ! operator) is equivalent to not-equal-to, less-than-or-equal-to, and greater-than-or-equal-to, respectively:

 a = b  # a is equal to b
!a = b  # a is not equal to b
 a > b  # a is greater than b
!a > b  # a is less than or equal to b
 a < b  # a is less than b
!a < b  # a is greater than or equal to b

If you click on a button in interact mode, its script is evaluated and sent a "click[]" event, executing the code declared in "on click do ... end". Any code outside an "on ... end" event handler will execute any time ANY event is sent to that widget. Buttons normally only get click[] events, so technically you can give them a script like

go["Next"]

instead of

on click do
 go["Next"]
end

and it will behave the same in most cases, but the former version becomes a problem with other widget types, which are sent a wider variety of events in different situations.

If you mark any widget as "animated" it will be sent a "view[]" event 60 times per second. This offers one way of making widgets and contraptions that continuously update themselves or move around, if that's the sort of thing you're alluding to trying to accomplish.

Does that help at all?

Viewing posts 141 to 150 of 150 · Previous page · First page