Comments (4)
I'll have a look at what my current documentation says and write a proper response to this later, as right now I don't have enough time, but you may want to already check out https://tgui.eu/tutorials/latest-stable/signals/#lambda-captures section to get a better understanding of the lambda capture and the reason behind the access violation.
from tgui.
I'll have a look at what my current documentation says and write a proper response to this later, as right now I don't have enough time, but you may want to already check out https://tgui.eu/tutorials/latest-stable/signals/#lambda-captures section to get a better understanding of the lambda capture and the reason behind the access violation.
I've read that page but rereading it now, I think I misread some of it, though if I'm reading it correctly now, I'm not sure why the code I quoted above would work now:
In a more advanced situation you can combine capture by value and capture by reference by specifying the individual variables (with or without an ampersand in front of the name to choose by reference vs. by value). All three functions below are identical.
If I'm understanding this correctly, then I can just do [&variable/class]
to access a specific thing, such as Reader
or loadFolderDialog
. The part I'm not sure I understand is why I need to pass the FileDialog
pointer as an input when I already have the [&]
or else I get a crash. Is this because I'm dynamically creating the FileDialog
in the onClick
event for a button? If so, why? Since the FileDialog
still exists when I click the open button in it, and the close
event fires after that point, should loadFolderDialog
not still be in scope and thus not need me to pass a reference to it?
Also, adding a bit that mentions the potential issues that the signals pages goes over but in a context more relevant to this specific widget, with some short examples, and a link to that section of the signals page would definitely help make things a lot clearer.
from tgui.
I wrote the "Lambda captures" section in the Signals tutorial because I knew that many people might not be familiar enough with lambdas. I'm open to suggestions on how to improve the information on that page.
One issue with writing better examples is that [&]
vs [=]
only matters in complex cases. Simple examples can't demonstrate "correct" behavior because they aren't complex enough, and also because the correct option depends on what you want to do in the lambda (which I can't predict). What I can do to improve the situation though is to make sure all examples use [=]
unless otherwise required. That way copying the example code can't lead to crashes and only to cases where variables outside the lambda aren't being altered (where it is easier to explain that a reference is needed).
I don't think I can add explanations to every single place where lambdas are used. The main reason I use lambdas in examples is to keep them short, so when just demonstrating what parameters a callback can have there shouldn't be a lot of code.
Something as detailed as Mozilla's MDN should not be expected. Writing good documentation takes a lot of time, so it's unlikely that there will be detailed guides on how to use TGUI. If there are specific parts in the documentation that can use some more clarification then I'm however willing to take suggestions or you can even send pull requests for them.
Since the FileDialog still exists when I click the open button in it
The capture [&]
is equivalent to writing [&Reader,&loadFolderDialog]
while [&,loadFolderDialog]
is equivalent to [&Reader,loadFolderDialog]
. The difference between the two is whether you take a reference to loadFolderDialog or whether the object is copied.
When taking a reference, what isn't important is whether the file dialog itself still exists, but whether the loadFolderDialog
variable itself still exists. If you are creating the file dialog in a onClick
event then I'm assuming your loadFolderDialog
is a variable that is local to that onClick callback. As soon as the variable goes out of scope, the reference to it becomes invalid and attempting to use it will result in a crash, even if the file dialog still exists in the gui.
This is why you need to copy the loadFolderDialog object in the lambda, so that it continues to exist even after loadFolderDialog
went out of scope. Because loadFolderDialog is a pointer, you are only copying a pointer and thus the object in the lambda is still the same file dialog.
from tgui.
I wrote the "Lambda captures" section in the Signals tutorial because I knew that many people might not be familiar enough with lambdas. I'm open to suggestions on how to improve the information on that page.
One issue with writing better examples is that
[&]
vs[=]
only matters in complex cases. Simple examples can't demonstrate "correct" behavior because they aren't complex enough, and also because the correct option depends on what you want to do in the lambda (which I can't predict). What I can do to improve the situation though is to make sure all examples use[=]
unless otherwise required. That way copying the example code can't lead to crashes and only to cases where variables outside the lambda aren't being altered (where it is easier to explain that a reference is needed).I don't think I can add explanations to every single place where lambdas are used. The main reason I use lambdas in examples is to keep them short, so when just demonstrating what parameters a callback can have there shouldn't be a lot of code.
That might be a good compromise. Though I do think it might be a good idea to add a section about potential pitfalls to the lambas section of the signals page. I can't speak for everyone but even though I managed to get my code working, it wasn't immediately obvious why the change I'd made worked so I'd imagine I'm not the only one to have run into an issue like this.
Something as detailed as Mozilla's MDN should not be expected. Writing good documentation takes a lot of time, so it's unlikely that there will be detailed guides on how to use TGUI. If there are specific parts in the documentation that can use some more clarification then I'm however willing to take suggestions or you can even send pull requests for them.
Yeah, I can understand that. I meant MDN as more being the ideal than what someone should realistically shoot for though anyways, especially for a small project like this that seems to have only one dev.
Since the FileDialog still exists when I click the open button in it
The capture
[&]
is equivalent to writing[&Reader,&loadFolderDialog]
while[&,loadFolderDialog]
is equivalent to[&Reader,loadFolderDialog]
. The difference between the two is whether you take a reference to loadFolderDialog or whether the object is copied.When taking a reference, what isn't important is whether the file dialog itself still exists, but whether the
loadFolderDialog
variable itself still exists. If you are creating the file dialog in aonClick
event then I'm assuming yourloadFolderDialog
is a variable that is local to that onClick callback. As soon as the variable goes out of scope, the reference to it becomes invalid and attempting to use it will result in a crash, even if the file dialog still exists in the gui.This is why you need to copy the loadFolderDialog object in the lambda, so that it continues to exist even after
loadFolderDialog
went out of scope. Because loadFolderDialog is a pointer, you are only copying a pointer and thus the object in the lambda is still the same file dialog.
This helped clarify things a lot, thanks. Judging by this, I should probably change it from [&, loadFolderDialog]
to [&Reader, loadFolderDialog]
then since those are the only two things I actually need any access to there. No need to be overly general and risk accidentally accessing something from the outside scope that I didn't actually want to access.
from tgui.
Related Issues (20)
- Autocomplete support for EditBox HOT 2
- Can not input CJK words HOT 9
- Black background shown when changing window size HOT 4
- How to customize the window's title bar and frame HOT 2
- How to implement a polygon window HOT 2
- Hover style does not disappear when mouse moves out of the window HOT 6
- Add a 2d chart or rendering 3d data for TGUI? HOT 1
- Chinese support? HOT 5
- Focus Issues HOT 1
- [ANNOUNCE] The TGUI now distributed via MSYS2 on Windows HOT 1
- CMake fails to install pkg-config files HOT 2
- On macOS, Input Method Editors(IME) behavior seems incorrect. HOT 6
- How to use SDL2/3 backend with cmake `FetchContent` HOT 2
- [Question] How to compile my first example of TGUI ? HOT 13
- [Bug] Dead radio button text HOT 3
- [Bug] Tabs state after disabled tab click HOT 2
- Is there a C binding? HOT 12
- Raylib backend? HOT 15
- Cannot delete widgets inside of the gui-builder HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tgui.