mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
improve docs a tiny bit more and prepare for release 3.0.3
This commit is contained in:
parent
fd6feec382
commit
d83e1a5637
|
@ -8,7 +8,7 @@ Take this ``player`` struct in C++ in a header file:
|
||||||
.. literalinclude:: ../../../examples/source/usertype_advanced.cpp
|
.. literalinclude:: ../../../examples/source/usertype_advanced.cpp
|
||||||
:caption: player.hpp
|
:caption: player.hpp
|
||||||
:linenos:
|
:linenos:
|
||||||
:lines: 1-48
|
:lines: 8-51
|
||||||
|
|
||||||
It's a fairly minimal class, but we don't want to have to rewrite this with metatables in Lua. We want this to be part of Lua easily. The following is the Lua code that we'd like to have work properly:
|
It's a fairly minimal class, but we don't want to have to rewrite this with metatables in Lua. We want this to be part of Lua easily. The following is the Lua code that we'd like to have work properly:
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ It's a fairly minimal class, but we don't want to have to rewrite this with meta
|
||||||
:caption: player_script.lua
|
:caption: player_script.lua
|
||||||
:language: lua
|
:language: lua
|
||||||
:linenos:
|
:linenos:
|
||||||
:lines: 93-124
|
:lines: 97-127
|
||||||
|
|
||||||
To do this, you bind things using the ``new_usertype`` and method as shown below. These methods are on both :doc:`table<../api/table>` and :doc:`state(_view)<../api/state>`, but we're going to just use it on ``state``:
|
To do this, you bind things using the ``new_usertype`` and method as shown below. These methods are on both :doc:`table<../api/table>` and :doc:`state(_view)<../api/state>`, but we're going to just use it on ``state``:
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ To do this, you bind things using the ``new_usertype`` and method as shown below
|
||||||
:caption: main.cpp
|
:caption: main.cpp
|
||||||
:language: cpp
|
:language: cpp
|
||||||
:linenos:
|
:linenos:
|
||||||
:lines: 1-3,5,7-9,53,55-86,136-137,142
|
:lines: 1-3,5,7-9,53,55-85,135-136,143-
|
||||||
|
|
||||||
There is one more method used in the script that is not in C++ or defined on the C++ code to bind a usertype, called ``brake``. Even if a method does not exist in C++, you can add methods to the *class table* in Lua:
|
There is one more method used in the script that is not in C++ or defined on the C++ code to bind a usertype, called ``brake``. Even if a method does not exist in C++, you can add methods to the *class table* in Lua:
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ There is one more method used in the script that is not in C++ or defined on the
|
||||||
:caption: prelude_script.lua
|
:caption: prelude_script.lua
|
||||||
:language: lua
|
:language: lua
|
||||||
:linenos:
|
:linenos:
|
||||||
:lines: 90-93
|
:lines: 89-92
|
||||||
|
|
||||||
That script should run fine now, and you can observe and play around with the values. Even more stuff :doc:`you can do<../api/usertype>` is described elsewhere, like initializer functions (private constructors / destructors support), "static" functions callable with ``name.my_function( ... )``, and overloaded member functions. You can even bind global variables (even by reference with ``std::ref``) with ``sol::var``. There's a lot to try out!
|
That script should run fine now, and you can observe and play around with the values. Even more stuff :doc:`you can do<../api/usertype>` is described elsewhere, like initializer functions (private constructors / destructors support), "static" functions callable with ``name.my_function( ... )``, and overloaded member functions. You can even bind global variables (even by reference with ``std::ref``) with ``sol::var``. There's a lot to try out!
|
||||||
|
|
||||||
|
|
|
@ -65,24 +65,23 @@ int main() {
|
||||||
lua["p2"] = player(0);
|
lua["p2"] = player(0);
|
||||||
|
|
||||||
// make usertype metatable
|
// make usertype metatable
|
||||||
lua.new_usertype<player>("player",
|
sol::usertype<player> player_type = lua.new_usertype<player>("player",
|
||||||
|
|
||||||
// 3 constructors
|
// 3 constructors
|
||||||
sol::constructors<player(), player(int), player(int, int)>(),
|
sol::constructors<player(), player(int), player(int, int)>());
|
||||||
|
|
||||||
// typical member function that returns a variable
|
// typical member function that returns a variable
|
||||||
"shoot", &player::shoot,
|
player_type["shoot"] = &player::shoot;
|
||||||
// typical member function
|
// typical member function
|
||||||
"boost", &player::boost,
|
player_type["boost"] = &player::boost;
|
||||||
|
|
||||||
// gets or set the value using member variable syntax
|
// gets or set the value using member variable syntax
|
||||||
"hp", sol::property(&player::get_hp, &player::set_hp),
|
player_type["hp"] = sol::property(&player::get_hp, &player::set_hp);
|
||||||
|
|
||||||
// read and write variable
|
// read and write variable
|
||||||
"speed", &player::speed,
|
player_type["speed"] = &player::speed;
|
||||||
// can only read from, not write to
|
// can only read from, not write to
|
||||||
"bullets", sol::readonly(&player::bullets)
|
// .set(foo, bar) is the same as [foo] = bar;
|
||||||
);
|
player_type.set("bullets", sol::readonly(&player::bullets));
|
||||||
|
|
||||||
// You can also add members to the code, defined in Lua!
|
// You can also add members to the code, defined in Lua!
|
||||||
// This lets you have a high degree of flexibility in the code
|
// This lets you have a high degree of flexibility in the code
|
||||||
|
@ -138,5 +137,8 @@ p1:brake()
|
||||||
*/
|
*/
|
||||||
lua.script(prelude_script);
|
lua.script(prelude_script);
|
||||||
lua.script(player_script);
|
lua.script(player_script);
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,8 +20,8 @@
|
||||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
// This file was generated with a script.
|
// This file was generated with a script.
|
||||||
// Generated 2019-07-04 13:41:56.522945 UTC
|
// Generated 2019-07-04 15:28:54.820895 UTC
|
||||||
// This header was generated with sol v3.0.2 (revision e440d7a)
|
// This header was generated with sol v3.0.2 (revision fd6feec)
|
||||||
// https://github.com/ThePhD/sol2
|
// https://github.com/ThePhD/sol2
|
||||||
|
|
||||||
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP
|
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP
|
||||||
|
|
|
@ -20,8 +20,8 @@
|
||||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
// This file was generated with a script.
|
// This file was generated with a script.
|
||||||
// Generated 2019-07-04 13:41:56.267639 UTC
|
// Generated 2019-07-04 15:28:54.552614 UTC
|
||||||
// This header was generated with sol v3.0.2 (revision e440d7a)
|
// This header was generated with sol v3.0.2 (revision fd6feec)
|
||||||
// https://github.com/ThePhD/sol2
|
// https://github.com/ThePhD/sol2
|
||||||
|
|
||||||
#ifndef SOL_SINGLE_INCLUDE_HPP
|
#ifndef SOL_SINGLE_INCLUDE_HPP
|
||||||
|
|
Loading…
Reference in New Issue
Block a user