Lanterns: improving the Multilights extension!

Can you send your code? I want to mess around with it and see if something works.

1 Like

sure also it starts to move when I go backwards or move a certain distance

3 Likes

sorry if I’m bothering anyone but I can’t find the link

2 Likes

You don’t need the whole link to search for an extension, but here’s the full link if you need it:

2 Likes

I added this and it works! Sorry I forgot about you!

2 Likes

Recently, on stream, @richard showed off the new shader implementation. It’s basically half of this extension, the rendering part, but implemented in the c++ layer of Makecode instead of the JavaScript layer. That means that when I get this extension updated to use that, it will be much, much faster. @richard is this functionality documented anywhere or should I just watch the stream again?

5 Likes

not documented anywhere, but it’s just two functions. to add it to your extension, add shader to the dependency entry in your extension’s pxt.json:

{
    "name": "Untitled",
    "description": "",
    "dependencies": {
        "device": "*",
        "shader": "*"
    },
    "files": [
        "main.blocks",
        "main.ts",
        "README.md",
        "assets.json"
    ],
    "preferredEditor": "tsprj"
}

and these are the two functions:

helpers.mapImage()
helpers.mergeImage()

helpers.mapImage is similar to mapRect, except that it takes in an image instead of a rectangle when remapping the colors in the target image

helpers.mergeImage takes the minimum of each pixel in two images

3 Likes


Half way done with the circle light implementation. The lag in that screenshot is mostly due to the flashlight, which I haven’t even looked at yet. I don’t know if shiver will work well or not. Currently I have it so it just generates the image once and saves it, but the old buffer system still exists, so I could use that to add the shiver. Then again, as Richard suggested, you could just have an animation that plays instead of actually using random shiver. Since I already have buffers in my JavaScript Extras extension, it shouldn’t be too hard to drop these two new functions in there. I’ll do that soon if I remember.

4 Likes

As usual, I have stumbled down a slightly bigger rabbit hole than I intended to. As it turns out, the draw position of the center of a sprite is not sprite.x - camera.drawOffsetX as the original extension is written. It is actually sprite.left + (mySprite.width >> 1) - camera.drawOffsetX. This… is annoying, but I only really have to write that code a few times. I had to find the actual sprite rendering code to figure that out. This error is the reason that putting a lightsource on a sprite with odd image side lengths results in the lightsource shifting around as the sprite moves, which was giving me soooo much trouble. There was also a slight annoyance in @richard’s shader implementation… get well soon Richard so I don’t feel bad about bugging you! :heart_hands:

5 Likes

Found a very old issue where toggling light on, then off, then pressing menu, then exiting the menu, and then toggling light back on would result in another Renderable being created, which would cause double the lag and shade the screen twice. This is just because of some odd push/pop scene handlers. Should be fixed when I finally release this.

@richard is there a way to tie something to the scene it’s in? I want to be able to tell if a certain sprite is in the current scene without searching through all the sprites. I can easily make a system for this myself, but I’m just wondering. (This is so that I can selectively render only the light sources attached to sprites in the current scene)

3 Likes

Surprise! More than 6 levels of darkness! I added support for more than 6 levels of darkness!

9 Likes

best way is to just keep track of what scene was active when the sprite was created. that’s why it’s a good idea to put all the state for your extension on a stack that you can push/pop as scenes are pushed/popped and store any sprites you’re tracking on that object. this is what i always do in my extensions (i’ve even made an extension just for making this boilerplate pushing/popping easier)

if you absolutely must do this for some reason, you can check the allSprites array which is stored on a scene:

if (game.currentScene().allSprites.indexOf(mySprite) !== -1) {
    
}

…but that won’t be the most efficient lookup in games with a lot of sprites/renderables/etc.

worth noting that the allSprites array is somewhat deceptively named; it doesn’t just contain sprites but also all other things that have a z index (like renderables)

4 Likes

After finding out the hard way that sprite IDs are tied to the scene, which… makes sense… I will attempt to do this instead. I’m gonna try to copy the mini menu code.

Ok well that was easy. Time to fix up the flashlight code now!

2 Likes

can’t wait to try out the improved extension :slightly_smiling_face:

1 Like

For this it works well until you get close to the top wall or bottom wall where it starts to move.

1 Like

That… makes sense… I think…
Anyways, here you are:

1 Like

Ok, I’ve updated the extension with the new circle light source code, but flashlights are still not updated. Just thought I would release it now as it may be a while before I get to the flashlight code (my high school does Trimesters and it’s crunch time!)

Maybe… don’t update existing projects just yet? I really like to extensively search for bugs before I release things and while I have done that, I don’t think I’ve testing enough, just imo. Idk, it’s probably safe, but just hold off until I release the full thing. You can certainly make new games with the new implementation, because a game breaking that you’ve worked on for 15 minutes isn’t nearly as bad.

If you encounter any issues, please let me know! If you have any issue with the extension at all, even issues I didn’t cause, now is the time, because the extension is very fresh in my mind and I understand how literally every part of it works now.

1 Like

I don’t know if this is just a “my project” thing but i’ve notice when i turn off the lighting then back on some light sources appear duplicated. It happens in my game Hidden Shades and i’ll try and replicate the glitch and show it to you.

2 Likes