diff --git a/docpages/advanced_reference/lambdas_and_locals.md b/docpages/advanced_reference/lambdas_and_locals.md index 20fba2b68c..ea41394066 100644 --- a/docpages/advanced_reference/lambdas_and_locals.md +++ b/docpages/advanced_reference/lambdas_and_locals.md @@ -2,11 +2,11 @@ If you are reading this page, you have likely been sent here by someone helping you diagnose why your bot is crashing or why seemingly invalid values are being passed into lambdas within your program that uses D++. -It is important to remember that when you put a lambda callback onto a function in D++, that this lambda will execute at some point in the **future**. As with all things in the future and as 80s Sci-Fi movies will tell you, when you reach the future things may well have changed! +It is important to remember that when you put a lambda callback onto a function in D++, that this lambda will execute at some point in the **future**. As with all things in the future and as 80s Sci-Fi movies will tell you, when you reach the future, things may well have changed! \image html delorean-time-travel.gif -To explain this situation and how it causes issues I'd like you to imagine the age old magic trick, where a magician sets a fine table full of cutlery, pots, pans and wine. He indicates to the audience that this is authentic, then with a whip of his wrist, he whips the tablecloth away, leaving the cutlery and other tableware in place (if he is any good as a magician!) +To explain this situation and how it causes issues, I'd like you to imagine the age-old magic trick, where a magician sets a fine table full of cutlery, pots, pans and wine. He indicates to the audience that this is authentic, then with a whip of his wrist, he whips the tablecloth away, leaving the cutlery and other tableware in place (if he is any good as a magician!) Now imagine the following code scenario. We will describe this code scenario as the magic trick above, in the steps below: @@ -31,7 +31,7 @@ In this scenario, the outer event, `on_message_create` is your tablecloth. The l * Best case scenario: you access invalid RAM no longer owned by your program by trying to write to `myvar`, and [your bot outright crashes horribly](https://www.youtube.com/watch?v=sm8qb2kP-fQ)! * Worse case scenario: you silently corrupt ram and end up spending days trying to track down a bug that subtly breaks your bot... -The situation I am trying to describe here is one of object and variable ownership. When you call a lambda, **always assume that every non-global reference outside of that lambda will be invalid when the lambda is called**! For any non-global variable always take a **copy** of the variable (not reference, or pointer). Global variables or those declared directly in `main()` are safe to pass as references. +The situation I am trying to describe here is one of object and variable ownership. When you call a lambda, **always assume that every non-global reference outside of that lambda will be invalid when the lambda is called**! For any non-global variable, always take a **copy** of the variable (not reference, or pointer). Global variables or those declared directly in `main()` are safe to pass as references. For example, if we were to fix the broken code above, we could rewrite it like this: @@ -45,7 +45,7 @@ bot.on_message_create([&bot](const dpp::message_create_t & event) { }); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Note, however that when you set `myvar` within the inner lambda, this does **not effect** the value of the var outside it. Lambdas should be considered self-contained silos, and as they execute in other threads should not be relied upon to set anything that exists **outside of that lambda**. +Note, however that when you set `myvar` within the inner lambda, this does **not affect** the value of the var outside it. Lambdas should be considered self-contained silos, and as they execute in other threads should not be relied upon to set anything that exists **outside of that lambda**. \warning Always avoid just using `[&]` in a lambda to access all in the scope above. It is unlikely that half of this scope will still even be valid by the time you get a look at it! @@ -53,4 +53,4 @@ Similarly, and important to note, your program **will not wait for bot.message_c If you do want to get variables out of your lambda, create a class, or call a separate function, and pass what you need into that function from the lambda **by value** or alternatively, you can use `std::bind` to bind a lambda directly to an object's method instead (this is great for modular bots). -If you are stuck, as this is a complex subject please do feel free to ask on the [official support server](https://discord.gg/dpp)! +If you are stuck, as this is a complex subject, please do feel free to ask on the [official support server](https://discord.gg/dpp)! diff --git a/docpages/example_code/cache_messages.cpp b/docpages/example_code/cache_messages.cpp index 8267971bb1..62c868fe05 100644 --- a/docpages/example_code/cache_messages.cpp +++ b/docpages/example_code/cache_messages.cpp @@ -11,7 +11,7 @@ int main() { bot.on_log(dpp::utility::cout_logger()); /* Message handler */ - bot.on_message_create([&](const dpp::message_create_t &event) { + bot.on_message_create([&message_cache](const dpp::message_create_t &event) { /* Make a permanent pointer using new, for each message to be cached */ dpp::message* m = new dpp::message(); diff --git a/docpages/example_code/context_menus.cpp b/docpages/example_code/context_menus.cpp index ff7a32a555..1ceee37d9b 100644 --- a/docpages/example_code/context_menus.cpp +++ b/docpages/example_code/context_menus.cpp @@ -8,7 +8,7 @@ int main() bot.on_log(dpp::utility::cout_logger()); /* Use the on_user_context_menu event to look for user context menu actions */ - bot.on_user_context_menu([&](const dpp::user_context_menu_t& event) { + bot.on_user_context_menu([](const dpp::user_context_menu_t& event) { /* check if the context menu name is High Five */ if (event.command.get_command_name() == "high five") { diff --git a/docpages/example_code/modal_dialog_interactions.cpp b/docpages/example_code/modal_dialog_interactions.cpp index 764581d468..74e1ec9f7b 100644 --- a/docpages/example_code/modal_dialog_interactions.cpp +++ b/docpages/example_code/modal_dialog_interactions.cpp @@ -45,10 +45,10 @@ int main(int argc, char const *argv[]) }); /* This event handles form submission for the modal dialog we create above */ - bot.on_form_submit([&](const dpp::form_submit_t & event) { + bot.on_form_submit([](const dpp::form_submit_t & event) { - /* For this simple example we know the first element of the first row ([0][0]) is value type string. - * In the real world it may not be safe to make such assumptions! + /* For this simple example, we know the first element of the first row ([0][0]) is value type string. + * In the real world, it may not be safe to make such assumptions! */ std::string v = std::get(event.components[0].components[0].value); @@ -59,7 +59,7 @@ int main(int argc, char const *argv[]) event.reply(m); }); - bot.on_ready([&](const dpp::ready_t & event) { + bot.on_ready([&bot](const dpp::ready_t & event) { if (dpp::run_once()) { /* Create a slash command and register it as a global command */ bot.global_command_create(dpp::slashcommand("dialog", "Make a modal dialog box", bot.me.id));