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

Avoid panic when "auto-binding" failed #330

Closed
nighca opened this issue Sep 10, 2024 · 1 comment
Closed

Avoid panic when "auto-binding" failed #330

nighca opened this issue Sep 10, 2024 · 1 comment

Comments

@nighca
Copy link
Collaborator

nighca commented Sep 10, 2024

spx tries to do auto-binding for sprites & sounds when the game starts running:

spx/game.go

Lines 256 to 271 in 1b3a5bf

for i, n := 0, v.NumField(); i < n; i++ {
name, val := getFieldPtrOrAlloc(v, i)
switch fld := val.(type) {
case *Sound:
media, err := g.loadSound(name)
if err != nil {
panic(err)
}
*fld = media
case Spriter:
if err := g.loadSprite(fld, name, v); err != nil {
panic(err)
}
// p.sprs[name] = fld (has been set by loadSprite)
}
}

It offers convenience when the user need to access some sprite or sound instance. However it works only when the variable names matches the sprite or sound name.

Within the example project (Archive.zip), it is OK to declare variables like

var (
    NiuXiaoQi NiuXiaoQi
    explosion Sound
)

While it panics if we declare variables like

var (
    myNiuXiaoQi NiuXiaoQi
    currentSound Sound
)

Sometimes it is fair to define some variables to hold sprite or sound instance during the game, which are for specific purpose that we do want to name them with some other names. That should not cause panic.

This issue proposes to remove the panic behavior when loadSound / loadSprite failed because of name mismatch here.

qlli added a commit to qlli/spxfork that referenced this issue Sep 20, 2024
nighca pushed a commit that referenced this issue Sep 20, 2024
* fix: Avoid panic when "auto-binding" failed #330

* Remove unused parameter
@nighca
Copy link
Collaborator Author

nighca commented Sep 20, 2024

closed by #336

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant