Hey, I’m trying to do a life system for my game and try to use it globally. My issue is that, when I try to use this.registry.events.on it will just register the registry event once (Using a register.set with true and false statements). Is there a way to make the registry event constantly check if it’s true or false or is there a better way to acomplish a consant scene comunication?
I think the events fire only when there is a change, so if you reset the registry to true when it’s already true, for example, it won’t fire a change event. Perhaps something like this would work though?
this.registry.set('playerDead', true);
this.registry.events.on('changedata-playerDead', (parent, value) => {
if (value === true) {
// do something
}
});
It kinda worked? I separated the events tho, making them work but only once. I do have a section on the other scene to set the registry to true whenever the player gets hit.
create(){
this.registry.events.on('changedata-gotHit', (parent, value) => {
if (value === true) {
if(!this.onePer){
this.onePer = true
this.hp -= 1
console.log("got Hit")
this.registry.set('gotHit', false);
}
}
});
this.registry.events.on('changedata-gotHit', (parent, value) => {
if (value === false) {
if(this.onePer){
this.onePer = true
this.hp += 0
console.log("NoHit")
}
};
});
}
The logic confuses me. You can write that code block like this:
create(){
this.registry.events.on('changedata-gotHit', (parent, value) => {
if(value) {
if(!this.onePer) {
this.onePer = true;
this.hp -= 1;
console.log("got Hit");
this.registry.set('gotHit', false);
}
} else {
if(this.onePer){
console.log("NoHit");
}
}
});
}
These two lines seem like they do nothing, so I wouldn’t include them:
this.onePer = true
this.hp += 0
Instead of storing a “gotHit” variable, can’t you just key off the HP to do your HUD update? Like this?
Inside create:
this.registry.set('hp', 10);
this.registry.events.on('changedata-hp', (parent, value) => {
if(value < 0) {
value = 0;
}
if(value) {
// Update HUD
} else {
// Player died
}
});
Inside a collider, when the player gets hit, put:
let hp = this.registry.get('hp');
this.registry.set('hp', (hp - 1));
Despite the name, the changedata events fire for any value setting after creation (so “update data”), but you can compare the value and previousValue arguments to see if anything changed.
I agree that for data you should maybe store hp instead. gotHit seems more like an event.
A registry event handler added with on() should keep firing for every value update until it’s removed — see registry data exchange.
I have tested what you showed me and it worked, but I want to see if I understood this.
Whenever I want to update a registry in another scene I need to get it first then change it, doing so it will update the registry event, right? Also, can the let be replaced with a this. ?
You don’t HAVE to get it per se. That’s not what makes it update. It’s just that in this case, I don’t know another way to do it. I feel like in your collision function, you should decrement the hp like that.There are use cases where you would just change it to something else though. setting it with set triggers the event.
As far as using this, you can. It will work. let, const, var, this are all valid ways to assign variables. To me, it’s a temporary value, so let made sense. If you’re asking as a general purpose rule, the registry acts like a Singleton, which is like a global data store, so from a software design perspective, cloning the data probably doesn’t make sense since the purpose of the registry is to separate concerns.
Yeah, I can just make a hp = this.hpCheck so I can check inside the update of the other scene how health you have and updating everything else that’s linked to the this.gameOver stuff. Thanks!