Closed 0LGL0 closed 1 year ago
I would like to know if it is possible to call functions from components.
I barely get what you mean but yeah, I don't see the problem.
EnTT
doesn't really care about what you do with your components.
When you get them, you've no limits:
auto &component = registry.get<Type>(entity);
// do what you like with your component here
As for your question:
why is my function not working?
It's almost impossible to say what's wrong in your code without a repro. I'm sorry. That being said, it's very likely that EnTT
isn't your problem here.
@skypjack, then here's the output from Visual Studio: Build started...
1>------ Build started: Project: Hyko, Configuration: Debug x64 ------
1>D:\VisualStudio_Integrade\MSBuild\Microsoft\VC\v170\Microsoft.CppBuild.targets(502,5): warning MSB8004: Intermediate Directory does not end with a trailing slash. This build instance will add the slash as it is required to allow proper evaluation of the Intermediate Directory.
1>D:\VisualStudio_Integrade\MSBuild\Microsoft\VC\v170\Microsoft.CppBuild.targets(503,5): warning MSB8004: Output Directory does not end with a trailing slash. This build instance will add the slash as it is required to allow proper evaluation of the Output Directory.
1>Scene.cpp
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared\minwindef.h(130,1): warning C4005: 'APIENTRY': macro redefinition
1>D:\C++\Hyko_Engine\libs\GLAD\include\glad\glad.h(32,1): message : see previous definition of 'APIENTRY'
1>D:\C++\Hyko_Engine\Hyko\src\Engine\Scene\Scene.cpp(80,57): warning C4267: 'return': conversion from 'size_t' to 'int', possible loss of data
1>D:\C++\Hyko_Engine\Hyko\src\libSource\entt\entt.hpp(1801,32): error C2338: static_assert failed: 'Ill-formed request'
1>D:\C++\Hyko_Engine\Hyko\src\libSource\entt\entt.hpp(1904,114): message : see reference to function template instantiation 'auto entt::internal::uses_allocator_construction
And also I use C++ 17
I'm sorry but I'm not supposed to look into pages of VS errors and fix user code to be honest. If you've a minimal repro to run on godbolt (which also includes EnTT, see the link on the README), I can quickly give it a glance though.
Sorry, but in godbolt there is only entt 3.6.0, and I have version 3.11.0 and, accordingly, some functions that are in entt 3.11.0 are missing. But still, here is the minimum code: https://godbolt.org/z/e819z5br5 . However, I want to say right away that it may not be possible to run this code (at least here). Also, just in case, I will give you a link to the repository of this project on GitHub. https://github.com/0LGL0/Hyko_Engine.git
You can pick the trunk version on godbolt, it's the most recent one. 👍 See here. The problem is that I can't reproduce your error anyway. The code compiles fine. Although, at first glance, I bet on the fact that your types aren't movable nor fully copyable. For example:
struct TransformComponent {
// ...
TransformComponent() = default;
TransformComponent(glm::mat4 transform)
: Transform(transform){}
TransformComponent(const TransformComponent& copy)
: Transform(copy.Transform), translate(copy.translate), scale(copy.scale), rotAngle(copy.rotAngle) {}
// ...
};
As you can see, the type has neither the move constructor nor the move operator. Similarly, it has a copy constructor but still no copy operator. Try to fix it and recompile. If the problem persists, please, provide me with a minimal reproducible example that fails to compile.
here: https://godbolt.org/z/7Wrsz9xWj . However, if you still can't figure out what the problem is, I can provide you with a zip archive with the project (this project is fully open on GitHub), but there will be files for Visual Studio (2022). I hope this helps, because the same compilation errors were output to the godbolt console as in Visual Studio. I also created a move constructor and operators. And yes, errors of this type: "error: no matching constructor for initialization of 'Hyko::TransformComponent' " appeared after I added the move constructor and operators
You have an error at line 317:
copy.addComponent<comp>(copy);
should be:
copy.addComponent<comp>();
@Innokentiy-Alaytsev, Sorry, but I can't check it right now. Although I can see that this is an error, but perhaps this is not the only reason for compilation errors. But thanks for the help. I'll be able to check it out soon
It compiles with the error fixed. At least Clang 16.0 compiles it.
This is good news. But mobile godbolt didn't work, that's why I can't check it.
I'm sorry I accidentally put a dislike :|. I didn't want
Oh, don't worry, stay where you are, S.W.A.T team is on the way.
So, here we are with another non-repro?
Yes, everything works, thank you so much for your help. And I'm sorry for my inattention
@skypjack, i'm so sorry
By the way, can I make my visit function better?
What is it trying to achieve exactly @0LGL0 copying a component?
@skypjack, I'm doing all this for my 2D game engine. I want to copy the entities of the scene using "Ctrl+ C" and "Ctrl + V".
You can use the value
and push
from the base class of the storage then (the sparse set).
They return and accept a void pointer, that is an opaque pointer to the element assigned to the entity.
You can get the value from the source and push it on the destination.
That being said, your implementation is also fine, so 🤷♂️ as you prefer.
Well, I wouldn't say that my implementation is good, since it depends on me, that is, if I add a new component, then I will have to add a new check to the "visit" function, and I may forget to do it, and then suffer thinking why my code doesn't work. Also, I don't think that such an abundance of if statements will benefit my engine in terms of performance. But thanks for the help
It's a well encapsulated function. This kind of things rarely poses a problem to the dev because you can easily rework them later on without affecting the whole codebase. Therefore, if it gets the job done today and it's good enough for your current purposes, why not? 🤷♂️
Anyway, you can make it opaque and avoid the type list using value
and push
functions. 👍
That being said, what about the original issue? I still don't have a way to reproduce it and I'm tempted to close it as invalid.
Here is a link to the problematic code: https://godbolt.org/z/7Wrsz9xWj . However, the problem has already been solved, and I have released a commit with copying scene objects for my engine. The problem was my inattention, namely in line 317 (where it was necessary to write "copy.AddComponent
I would like to know if it is possible to call functions from components. If so, why is my function not working? Here is my function: https://gist.github.com/0LGL0/9a32dda2b822cbc045ed7153387045c7
My function is needed to copy one component passed to the template to a passed entity with the same component. Here is the "hasAllComponents" function: https://gist.github.com/0LGL0/a8597d58cba50e45330ada311153582c. This is the "GetComponent" function: https://gist.github.com/0LGL0/08e5e801701af65e19cfd7d53bbe3c04. An example of one of my components (they all contain the "clone" function): https://gist.github.com/0LGL0/03b4bd8f8bde99ad36312c9e429e63a3. And the function where I call the "copyComponent" function: https://gist.github.com/0LGL0/c8df1666e2fcdc864fa5678cb4c39525.
"visit" is my function, which simply iterates through all the components that I have and passes to the lambda function argument only those components that are in this entity