0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

HSP - module変数の扱い方

Posted at

hspではこのようにモジュールを書くのがおすすめ。

sample_module.hsp

dimtype hogehoge@global,5 //この変数をモジュール内のみで使用する。グローバルでなくてもいいがわかりやすさのために宣言的に@globalで書いてる。
#module sample_module x, y

//初期化関数
#defcfunc make_new_instance int _x, int _y
    newmod hogehoge@global, sample_module, _x, _y
    return stat
#modinit int _x, int _y, local i
    mref i, 2 //これがポイントでこの変数自身の番号が取得できる。
    set_xy i, _x, _y
    return i

//セッタ
#deffunc set_xy int p1, int _x, int _y
    _set_xy hogehoge@global(p1), _x, _y
    return
#modfunc _set_xy int _x, int _y
    x = _x
    y = _y
    return

//ゲッタ
#deffunc get_xy int p1, array a
    _get_xy hogehoge@global(p1), a
    return
#modfunc _get_xy array a
    a = x,y
    return

#global

a = make_new_instance(5,6) //モジュール変数自体が返ってくるのではなく、変数のIDが返ってくる
get_xy a, xy
mes strf("%d,%d",xy, xy.1)

set_xy a, 7,8
get_xy a, xy
mes strf("%d,%d",xy, xy.1)

deffunc と modfuncをセットで扱うことで、モジュール変数を番号で管理できる。
関数の定義に一手間かかるが、非常に柔軟。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?