-
-
Notifications
You must be signed in to change notification settings - Fork 899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Removed parameter Component.updateTree({callOwnUpdate}) #1224
Conversation
I can't say I'm a big fan of this solution, having overlapping functionality on different methods becomes confusing I think. The current solution isn't nice either, but I hope that we can come up with something else than this. I would actually rather expose |
Alright, I renamed |
This reverts commit 9124872.
Turns out this won't work:
|
I don't think I follow, why can't |
I mean, we could just copy-paste the code from |
Ok, scratch that. There is a way, and it's even a very nice way, but it necessitates a breaking change. So, the trick is to replace FlameGame extends Component with Game with FlameGame extends Game with Component So, this is a big conceptual change. But it's a nice change: it's much easier to think of It would also be a breaking change, because in order to be able to use I wouldn't recommend such change just for the sake of being able to call the method |
If we are having a breaking change here, I think it would be nicer to just add
We want to keep priority on the constructor here, since that is consistent from |
Honestly, I prefer the way we have right now, I do agreed that it has problems, but I think I prefer those problems that any other alternative so far. We could mitigate those problems by having some section on our docs deeply explaining what callOwnUpdate is and why it exists. |
PositionComponent won't be affected here: class PositionComponent extends Component {
PositionComponent({..., int? priority}) {
changePriorityWithoutResorting(priority); // or we could make a simple setter in Component
}
} So the only users that would be affected are those that inherit from Component directly and call its constructor explicitly -- which, in my opinion, is a small minority. That being said, I'm wary in general about creating methods in |
Quite a lot of people use
We don't guarantee that, but we are in control of it. If we want to make breaking changes on that level we should of course be wary. But I think I'm leaning more to Erick's solution than the last one I suggested too, that we just keep it the way it currently is and document |
@spydon I hope we can at least agree on the fact that our current class hierarchy is ungood. We have And one of those warts is the fact that both Game and Component have the The problem then is that this hack propagates into the rest of the Component hierarchy. And that's bad, because it makes the original code smell endemic, and much harder to fix in the future.
Well, except for #1224, obviously. I mean, just like
I do not see how this solves the original concern that the parameter cannot be respected by the implementations at all. And if the documentation says "ok you have this parameter, but feel free to ignore it" -- then what kind of parameter is it? That being said, there are other methods for dealing with the |
#1224 is this PR, I guess you meant another issue/PR?
Absolutely, if you have more ideas, put them on the table. :) |
Maybe the best would be to just duplicate the lines from @override
@mustCallSuper
void update(double dt) {
super.update(dt);
_cameraWrapper.update(dt);
if (parent == null) {
updateTree(dt);
}
}
@override
void updateTree(double dt) {
children.updateComponentList();
if (parent != null) {
update(dt);
}
children.forEach((c) => c.updateTree(dt));
} |
^ Yes, this should work. And, I mean, it's not such a big code chunk anyways... Another possibility would be to add private variable class FlameGame {
bool _preventUpdate = false;
void update(double dt) {
if (_preventUpdate) return;
_cameraWrapper.update(dt);
if (parent == null) {
_preventUpdate = true;
super.updateTree(dt);
_preventUpdate = false;
}
}
} |
The variable way would call an overridden version of |
The idea here is that first the user calls It might sound complicated, but think of it as passing the parameter |
Sure, I understand how it works, but imagine this scenario: class MyGame extends FlameGame {
@override
void update(double dt) {
super.update(dt);
print(1);
}
} When that is added directly to the game widget |
I see, makes sense. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a minor comment, otherwise lgtm!
Description
Component.updateTree()
method will no longer have the optional parametercallOwnUpdate
.Game
class now has method.updateTree()
, which by default redirects toGame.update()
, but alternative implementations may be provided by the derived class.FlameGame.update()
now works according to howComponent.update()
is expected to work, not howGame.update()
is expected to work. If you needGame.update()
functionality forFlameGame
, callFlameGame.updateTree()
.Checklist
///
) and updated/added examples indoc/examples
.flutter format
and theflutter analyze
does not report any problems.Draft
status, by clicking on theReady for review
button in this PR.Breaking Change
Related Issues
Closes #1219