1 decade ago
by MikeL
Essentially instead of doing this in one entity:
playerState: {RUNNNING: 1, JUMPING: 2, HIDING: 3, SWIMMING: 4}
And this in another:
evilGuyState: {RUNNING: 1, CROUCHING: 2, HIDING: 3}
You declare them in your main.js:
new ig.Symbols("RUNNING JUMPING HIDING SWIMMING CROUCHING");
Then you can use them in ANY entity. No need to assign any numbers or variables or to any particular entity.
You could then use them for example in your player's update method as in:
if this.state == ig.Entity.RUNNING
//Do running stuff
else if this.state == ig.Entity.SWIMMING
//Do swimming stuff
And so forth.
The code and more information is here.
Thanks, I was looking for something exactly like this.
Hey Mike, great work as always. I just forked your repo and added one slight tweak that I found quite useful - bitwise safe state values. Now you can combine states if you'd like:
this.state = ig.Entity.RUNNING | ig.Entity.SHOOTING
// You're now running AND shooting
and to test for a given state or a combination of states:
if (this.state == ig.Entity.JUMPING) {
// Do jumping-only stuff
} else if (this.state & ig.Entity.RUNNING) {
// Do running stuff
} else if (this.state & (ig.Entity.SPAWNING | ig.Entity.DYING)) {
// Do stuff common to spawning and dying
}
Of course it still works as you have shown above as well. You can check it out on
github if you'd like. Thanks for the idea!
1 decade ago
by MikeL
Outstanding xdissent! Never thought of that. I love it. I'm assuming you've tested it and all and there's no glitches? If you want we can merge it to the trunk (? I'm not all that familar with Git) also so that people get to the modified Symbols plugin if they click my link above. Shoot me an e-mail if you like.
By the way, would this method cap the number of symbols? I've never used bitwise operators in javascript.
Good catch actually - it does cap the maximum number of symbols to 30. It would be 32 but we don&039;t use zero because it's not much help (always fails #&
and |
bitwise ops) and JS uses signed integers which use up one more bit, leaving 30. I considered throwing an error if too many symbols are defined. Perhaps that should be in there for safety. Other than the 32 bit overflow, I've tested it in every use case I can think of and it's been fine. Technically it's just assigning consecutive powers of 2 as state values, so there should be very little difference in practice.
I'll add that exception to my fork and then send you a pull request on Github. It should be safe to automatically merge into your branch from the Github UI. That way you don't have to sweat the nitty gritty git command line stuff.
Thanks again!
1 decade ago
by MikeL
Thanks xdissent. I was able to merge your commit. I wrote some more details about symbols' odd implementation and more food for thought
here (may need coffee for it)].
I'm not sure if github forwards everything that is written to my e-mail, because it didn't send me your last comment.
What I might do is set up a third parameter on the init which is a boolean with a default of true. True would mean that it uses the binary implementation that you devised. Setting false would resort to the original integer implementation and therefore remove the cap for those that need large numbers of symbols. I'll also update the readme when I get around to it.
When I use this plugin it often reverts to undefined
If I type this in my entity update(), I still get undefined from Impact.js line 54
if (this.state == undefined){
this.state = ig.Entity.RUNNING;
ig.log(this.state); // undefined
}
Nevermind, I didn't have RUNNING in my symbols list. It took me posting to the forum to check that.
Page 1 of 1
« first
« previous
next ›
last »