Greatly Improve Workflow With Lambda Functions | Godot Tutorial

  Рет қаралды 6,550

DevWorm

DevWorm

Күн бұрын

Пікірлер
@NickCoyour
@NickCoyour Ай бұрын
You are by far one of the best channels for Godot tutorials. You teach very well and very clearly. Love your videos.
@owenlloyd2528
@owenlloyd2528 Ай бұрын
Thank you, very interesting. At first thought I cannot see any way that a lambda/callable would improve readability or functionality in my current projects but I am sure that when I get into it things will pop out screaming 'use me!'. Very informative.
@robipro9254
@robipro9254 Ай бұрын
Very nice tutorial. I like using lambda functions when connecting signals. Instead of, for example, writing my_signal.connect(_on_my_signal.bind(a_value)) then create in the script func _on_my_signal(my_value), I can simply write my_signal.connect(func(a_value): what ever code I want). That way, I don't have to think about a name of a function (here _on_my_signal) and I don't have to cluster my script with multiple small functions linked to managing signals.
@desireelustre
@desireelustre Ай бұрын
This is another informative content. Thank you as always.
@dev-worm
@dev-worm Ай бұрын
thank you! I am so happy to hear such kind words!
@Claudehopper
@Claudehopper Ай бұрын
In my opinion, using Lambda functions for the example you gave with capping the health is a little more work than it's worth. I find it much easier to just clamp the variable. var health: int = 100: - set(value): - - health = clamp(value, 0, 100) This does it all for you with much less clunk and is easier to read. However, even if I don't think that's a good use for it, I already found value out of using Lambda functions and I thank you for this video!
@Mr_BetaMax
@Mr_BetaMax Ай бұрын
I didn't know GDScript supported this! Amazing!
@cheesy4810
@cheesy4810 Ай бұрын
That helped with a lot of questions I had. Great tutorial, keep going.
@dev-worm
@dev-worm Ай бұрын
thank you so much! I am glad it helped!
@schuepbachr
@schuepbachr Ай бұрын
Educational and aspirational. Thanks for the content! Now I need to get through initial setup and start implementing systems. (I feel like I could use a lambda to carry an armor system, with various stages of armor, and snapping it in line with the take damage process. Player takes damage, the value gets pushed through the player armor callable, then gets subtracted from current health, and tests dead or not.)
@lout160
@lout160 Ай бұрын
The tutorial I didn't know I needed. gotta go do some refactoring now.
@dev-worm
@dev-worm Ай бұрын
That’s awesome to hear! I hope it helps!
@JohnathanBlack-j3h
@JohnathanBlack-j3h 26 күн бұрын
Appreciate this video! Thank You!
@dev-worm
@dev-worm 26 күн бұрын
I am so happy to hear that! glad I could help! thank you!
@OriahVinree
@OriahVinree Ай бұрын
I live a simple life. I see a new DevWorm video, I click.
@dev-worm
@dev-worm Ай бұрын
haha!! thanks the support truly means the world!
@LincsOracle
@LincsOracle Ай бұрын
Great tutorial and I fully understand how to create an use a lambda function. However the I just struggle to see the benefit compared to just creating a couple of functions and call them directly as shown in the example below. Only difference I can see is I'm duplicating the print(new_health) statement in both functions. extends Node2D var player_health = 100 func add_health(phealth: int, amount: int) -> int: var new_health = phealth + amount print(new_health) return new_health func subtract_health(phealth: int, amount: int) -> int: var new_health = phealth - amount print(new_health) return new_health func _process(delta: float) -> void: if Input.is_action_just_pressed("ui_up"): player_health = add_health(player_health, 10) if Input.is_action_just_pressed("ui_down"): player_health = subtract_health(player_health, 5)
@fr4ctur71
@fr4ctur71 Ай бұрын
I have also been pondering the use of lambda functions. One thing I've realized is that I don't think you can pass a method normally as an argument. So the lambda and callables allow you to pass functions and logic to other scripts that don't have access to those methods. I've also found use for them specifically when building a load screen and something to monitor the loading of assets asynchronously. the main script could tell all the other things to start loading, and pass them a Lambda function as a callable that would essentially ping the main script or run whatever was needed to update the display. But it meant that each asset that was loading, didn't need access to the UI to update it or access to the main script to tell it to update the UI. I could be wrong about this though xD I'm very much still learning things
@dyegoeduardosantossilva3659
@dyegoeduardosantossilva3659 Ай бұрын
Using Callables variables is very much useful ( not sure about lambda functions though, i didnt really understand if they are the same thing ). You can have a piece of script that at one point calls a function to do something. If you ever have, like, two different things you could do at this point ( like subtract or add health ) you might just do an if statement and check for each function to call, right? Using callables, however, is much more simpler in some cases. Like, suppose you have an watery enemy you can shoot with different kinds of bullets.A regular bullet doesnt do anything to him damage-wise, instead it plays an animation on the enemy and changes its behavior. Now imagine you have a freeze bullet, that only freezes this enemy ( locks him in place, changes animation, yadda yadda yadda ), and THEN, a regular bullet would shatter the ice and therefore deal damage to the enemy. Now, on both occasions, the bullet is hitting the enemy on the same way, but the processing is different. You could have an If statement on the enemy function that handles the interaction, checking for if the enemy is frozen or not. OR you could have the function that handles the regular bullet interaction stored on a variable. So now, every time your enemy gets frozen, you can change which function is stored in said variable. That way your code stays the exact same, calling a function inside a variable, but you can change which function is stored inside depending of different interactions. This might have been really hard to understand ( in that case sorry ), but its one example that Im using myself on the game im currently working on. In any case, yeah, they can help you organize your code better on a lot of situations, specially if you have some nodes accessing functions of other nodes or things like that ( like the bullet telling the enemy that it got hit )
@SleepyLazyPanda
@SleepyLazyPanda 8 күн бұрын
thank y'all for sharing knowledge
@KartikNarayanan
@KartikNarayanan Ай бұрын
Great video. How are lambda functions/callables in terms of performance? Also, could you please do a similar video on _unhandled_input and how to use it in game
@dev-worm
@dev-worm Ай бұрын
supposably there isn't a noticeable difference in performance
@KurtBuhler
@KurtBuhler Ай бұрын
Great one!!
@mkaks22gamerserise16
@mkaks22gamerserise16 Ай бұрын
always support you
@dev-worm
@dev-worm Ай бұрын
thank you so much! that means the world!
@notaboutit3565
@notaboutit3565 Ай бұрын
Haven’t gotten a chance to mess with this yet, but can you @export the var that contains the lambda and have an inspector-definable argument? Idk if that makes sense or has a real use but
@dyegoeduardosantossilva3659
@dyegoeduardosantossilva3659 Ай бұрын
You cant do that, unfortunately. I would love to be able to do that but apparently its kinda of impossible to store that. I think functions are an adsress inside your ram memory, when you save stuff on export variables, they become actual text inside your .tscn file, so it would be impossible to store a function since every time you load the program ( in this case the game ) it goes to a different address in your ram. Although i might be completely wrong in this ( in why it doesnt work ). I do know for sure it doesnt, though.
@notaboutit3565
@notaboutit3565 Ай бұрын
@ thanks for the reply :) I appreciate the knowledge transfer for sure!
@dyegoeduardosantossilva3659
@dyegoeduardosantossilva3659 Ай бұрын
@@notaboutit3565 you're welcome man
@rionhunter
@rionhunter 23 күн бұрын
i'd never been able to get variable base lambdas to work. learning it was because I was missing .call all this time has me fist shaking. Otherwise, my go-to lambda usage has been limited almost entirely to mouse_entered.connect(func(): mouse_over = true) and the opposite for mouse_exited. but by the gods has that been powerful enough for me
@malojordan653
@malojordan653 Ай бұрын
Would you consider making a roguelike tutorial?
@alonsonneveld8137
@alonsonneveld8137 Ай бұрын
I have a small problem in Godot rn when I try to run my project in the editor it only works sometimes do you know how to fix this bug
@dev-worm
@dev-worm Ай бұрын
like it only runs sometimes? I haven't ever had the issue you are having but I have had a few engine bugs and usually I just either close and reopen the engine or delete Godot and redownload it!
@notaboutit3565
@notaboutit3565 Ай бұрын
Also, I know these are intended not to be global scope, but could you add them to the global pool in the project settings and would it be beneficial?
@Maffezzolo
@Maffezzolo Күн бұрын
Hi devworm, can you give me a light in how can i use normalmap on animatedsprites? This is really messing up with me😢
@635574
@635574 Ай бұрын
For things like health, setters and clamp() are doing all the work, no funcs or lambdas required. But I would use this for some simple things like that calculator example, not sure if it will be useful for the game I'm building now. I only know setters because of 16bit dev
@dev-worm
@dev-worm Ай бұрын
that is a very good point, and I can agree with that for the health example. But I do still think there are many instance though where callables could play a huge part in keeping things "orginized" within a script.
@mystiogan5891
@mystiogan5891 Ай бұрын
Thank you (╹◡╹)
@dev-worm
@dev-worm Ай бұрын
thank you and of course!! I hope it helped!
@ajmgdaj
@ajmgdaj Ай бұрын
tbf nothing needs a lmabda function. There are just places where they are extremely convenient and clear to read
@dev-worm
@dev-worm Ай бұрын
very true.
@bradleygross5340
@bradleygross5340 Ай бұрын
Can you curry with lambdas in Godot?
@GardenData61371
@GardenData61371 Ай бұрын
Prepare for unforseen consequences
@Caucasmap
@Caucasmap Ай бұрын
Hello friend, how can I zoom in and out of the map in my own grand strategy game and also move the map with the mouse?
@jeremiahaemile2008
@jeremiahaemile2008 Ай бұрын
back from hell (school ;) today is my rest
@dev-worm
@dev-worm Ай бұрын
that rest is important! how is school been going Jeremiah? Im guessing not too well from that comment lol!
@jeremiahaemile2008
@jeremiahaemile2008 Ай бұрын
@@dev-worm i have alot alot alot of weekly homeworks monthly test i have just finished the first month exams from two days and alot of unfinished lessons 🙂🙂
@jeremiahaemile2008
@jeremiahaemile2008 Ай бұрын
so easy right 😁😁😁
@SeanBotha
@SeanBotha Ай бұрын
GODOT CEO=people that say keep politics out of game engines are toxic!
@w00tyd00d
@w00tyd00d Ай бұрын
Hate to break it to you, but games have had politics in them for the past 40 years 😂 you just don't want games to include politics _you don't agree with,_ which is fine, but call it what it is.
@moonkis9103
@moonkis9103 Ай бұрын
You can use any function (4.4 will have static as callable as well) as a parameter, you just shifted cluttering the namespace with functions to variables. I watched the video and honestly came out a bit disappointed. The examples fails to mention the two biggest most useful aspects of lambdas: Lambdas are great *especially* for capturing local variables and passing it down func foo() -> void: var local_variable: int = 5 bar(func(): local_variable += 1) and currying en.wikipedia.org/wiki/Currying Would have been really nice if this delved into those aspects a bit more and could have provided some useful insight into when they can be good. I can take a real example I use where I have a combat function (turn-based) where I reset the original local z_index of whatever I'm tweening on the tweens finished signal without having to store that information anywhere but it's lambda-callback.
@moonkis9103
@moonkis9103 Ай бұрын
Snippet: func _handle_attack_action(event_: Event.AttackEvent) -> void: var position = event_.attacker.position var z_index = event_.attacker.z_index var reset_callback = func(): event_.attacker.z_index = z_index _tween = make_tween() _tween.set_ease(Tween.EASE_OUT) event_.attacker.z_index = 999 _tween.tween_property(event_.attacker, "position", event_.target.position, 0.125) _tween.tween_property(event_.attacker, "position", position, 0.125).finished.connect(reset_callback) This way the attacker will always be on-top of the defender, but then reset once the tween is finished to it's original z-index. This code isn't perfect, but it does display the power of capturing state to be modified at a later stage as part of say a callback.
Godot Rendering Update - Clay John - GodotCon 2024
46:03
Godot Engine
Рет қаралды 18 М.
Modular Upgrades Made Easy Using the Strategy Pattern
6:41
Bitlytic
Рет қаралды 87 М.
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН
The MUST Follow Roadmap For ALL Solo Developers
14:41
DevWorm
Рет қаралды 26 М.
Should you use Metadata in Godot?
7:18
Queble
Рет қаралды 15 М.
How to program in Godot - GDScript Tutorial
58:10
Brackeys
Рет қаралды 834 М.
Custom Resource are a MUST KNOW in Godot | Complete Tutorial
32:10
Introduction to Custom Resources in Godot!
8:43
Queble
Рет қаралды 7 М.
Google’s Quantum Chip: Did We Just Tap Into Parallel Universes?
9:34
Godot Scripts I add to Every Game
12:34
Aarimous
Рет қаралды 55 М.
You're missing out if you don't use these in Godot 4
10:49
MrElipteach
Рет қаралды 10 М.
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН