spx icon indicating copy to clipboard operation
spx copied to clipboard

Define auto-binding

Open nighca opened this issue 1 year ago • 0 comments

Audo-binding is a mechanism / feature:

In a project with sprites (named as Sp1, Sp2, Sp3) & sounds (named as sd1, sd2), if you define vars in main.spx as below:

var (
	Sp1 Sp1    // with type of some sprite
	Sp2 *Sp2   // with type of pointer to some sprite
	Sp3 Sprite // with type `Sprite`
	sd1 Sound  // with type `Sound`
	sd2 *Sound // with type of pointer to `Sound`
)

Note that the variable name must exactly match the sprite/sound name.

The corresponding sprite instances and sound info will be bound to the variables automatically, without need to assign value manually.

Then, for example, you can access the values like this:

onStart => {
	Sp1.say "123"
	play sd2
}

The feature is implemented in spx code:

https://github.com/goplus/spx/blob/845f2c0e2e748d7f6acc9d3a29b6bbc4cb2cbe01/game.go#L255-L274

Related:

  • https://github.com/goplus/spx/issues/330
  • https://github.com/goplus/builder/issues/752
  • https://github.com/goplus/spx/issues/316#issuecomment-2362719724

nighca avatar Nov 08 '24 07:11 nighca