-
Notifications
You must be signed in to change notification settings - Fork 481
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove behavior names, support multiple parameterized behaviors #22044
Conversation
if (typeof behavior === 'function') { | ||
behavior = { | ||
func: behavior, | ||
extraArgs: [], |
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.
Does this mean follow(cat)
will override follow(dog)
? What if I want to follow both?
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.
My plan is for the behavior getter block to return an object with both func
and extraArgs
properties. In your example they won't be considered equal because they'll have different extraArgs, so you can add both behaviors.
This bit of code just makes addBehavior
more convenient to use in helper library code if you're adding a behavior with no extra args.
for (var i = 0; i < sprite.behaviors.length; i++) { | ||
var myBehavior = sprite.behaviors[i]; | ||
if (myBehavior.func !== behavior.func) { | ||
console.log('funcs not equal'); |
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.
Extra?
return behavior; | ||
} | ||
|
||
function findBehavior(sprite, behavior) { |
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.
Now might a good time to start adding some basic test coverage of this behavior (hah). We don't need to test the whole file, but this method is a good starting place.
4cfa951
to
54b9487
Compare
54b9487
to
f4d6b65
Compare
2d423d2
to
2e4b0b1
Compare
I added a unit test, but had to pay the price of adding a terrible hack. I PTAL |
Hmm. Can we property export methods in |
I definitely need to put more thought into how we organize the helper library code. I imagine a pretty large chunk of it will be moved into common block snippets, and since that will mostly be written by levelbuilders I want to keep it pretty straightforward to write. I think the ideal solution would be to leave the library as it is, and replace the hackery in this PR with a webpack loader that allows me to import its globals into test code as if they were properly exported. Some of the options on this wiki page look promising: https://github.com/webpack/docs/wiki/shimming-modules Also, the globals need to be known to blockly so that it will work around them during codegen if you make a variable with the same name. Also, it turns out that babel does process the helper library because the rule doesn't explicitly exclude Line 67 in 4331cb4
That said, I think this is a problem for future Ram, so I'd like to merge this as-is. |
I'm removing the
name
property from behaviors now that method comparisons are working 馃帀However, I'm re-complicating things a bit to properly support parameterized behaviors such as
follow <target>
.Adding and removing a function defined in the global scope from a behaviors list is pretty straightforward, because functions equal themselves. However if you want to to have a behavior that varies based on a argument, we run into a problem:
Because the dynamically generated functions aren't equal to each other, we won't be able to find them later in a sprite's behavior list if we want to remove it or check for duplicates. If we go back to the old world of using behavior names, all
follow <target>
behaviors equal each other so you can't have multiple versions of one behavior on the same sprite.To handle this I'm storing behaviors as a an object with both a
func
andextraArgs
. In this world the follow behavior looks like:When called in the draw loop, everything in
extraArgs
gets appended to the argument list. Two behaviors are equal if they use the same function and all theirextraArgs
are equal.addBehavior
still accepts a function if you don't needextraArgs
.