Skip to content
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

Emitting an event with a value of 0 produces a GameEvent instead #2418

Closed
mattjennings opened this issue Jul 17, 2022 · 4 comments · Fixed by #2422
Closed

Emitting an event with a value of 0 produces a GameEvent instead #2418

mattjennings opened this issue Jul 17, 2022 · 4 comments · Fixed by #2422
Labels
bug This issue describes undesirable, incorrect, or unexpected behavior

Comments

@mattjennings
Copy link
Contributor

When emitting a custom event on an object, if the value is 0 it gets overwritten by a GameEvent due to this line:

if (!event) {
event = new GameEvent();
}

Steps to Reproduce

Emit an event with a value of 0:

actor.emit('something', 0)

actor.on('something', (value) => {
    console.log(value) // GameEvent
})

actor.emit('something', 1)

actor.on('something', (value) => {
    console.log(value) // 1
})

Expected Result

Value to be persisted

Actual Result

Value gets overwriten with GameEvent

Environment

  • Excalibur versions: 0.27

Current Workaround

Create a custom GameEvent instance and emit that instead. Perhaps that is the intention, that every event should be an instance of GameEvent? If so, should emit throw an error if it receives something other than undefined or a GameEvent?

@eonarheim
Copy link
Member

@mattjennings Thanks for the bug!

My spidey sense says this is likely bad falsey checking and providing a default GameEvent

@eonarheim eonarheim added the bug This issue describes undesirable, incorrect, or unexpected behavior label Jul 17, 2022
@mattjennings
Copy link
Contributor Author

@eonarheim yup that's what's happening here. I started working on a PR to fix this by only forcing to GameEvent if null or undefined - however I noticed the type definition for emit on EventDispatcher is:

public emit(eventName: string, event: GameEvent<T>) {}

while on Class it's:

public emit(eventName: string, eventObject: any) {}

so while I can update EventDispatcher to preserve non-GameEvents, it goes against the expected type. Should Class be updated to take GameEvent? If so, what should emit do when it receives something else?

@eonarheim
Copy link
Member

Awesome, thanks for picking this up!

My intention is to remove the GameEvent<T> requirement eventually, it's been artificially constricting (requiring people to build custom events for simple things) and hasn't been very useful in general...

I'd support widening the type in the PR to be looser on EventDispatcher.emit(), maybe this?

// This might need some additional thought if it winds up causing typing issues elsewhere...
public emit(eventName: string, event: GameEvent<T> | any) {}

Otherwise I'm okay with a tactical change to just address the bad falsey check, and we can continue thinking/discussing the types on EventDispatcher

@mattjennings
Copy link
Contributor Author

Good to know, I agree that it is a bit cumbersom to have to make custom events for simple values.

Doing GameEvent<T> | any effectively just casts it as any, unfortunately:

CleanShot 2022-07-17 at 13 36 49@2x

I will just address the falsey check for now then!

eonarheim pushed a commit that referenced this issue Jul 17, 2022
Closes #2418

## Changes:

- Fixed `EventDispatcher.emit` converting falsy values to `ex.GameEvent`. It will only convert `undefined` or `null` values now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue describes undesirable, incorrect, or unexpected behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants