File: usertype_basics.cpp

package info (click to toggle)
sol2 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,096 kB
  • sloc: cpp: 43,816; ansic: 1,018; python: 356; sh: 288; makefile: 202
file content (85 lines) | stat: -rw-r--r-- 1,992 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>

#include <iostream>

struct ship {
	int bullets = 20;
	int life = 100;

	bool shoot() {
		if (bullets > 0) {
			--bullets;
			// successfully shot
			return true;
		}
		// cannot shoot
		return false;
	}

	bool hurt(int by) {
		life -= by;
		// have we died?
		return life < 1;
	}
};


int main() {

	std::cout << "=== usertype basics ===" << std::endl;

	static const bool way_1 = true;

	sol::state lua;
	lua.open_libraries(sol::lib::base);

	if (way_1) {
		lua.new_usertype<ship>(
		     "ship", // the name of the class, as you want it
		             // to be used in lua List the member
		             // functions you wish to bind:
		             // "name_of_item",
		             // &class_name::function_or_variable
		     "shoot",
		     &ship::shoot,
		     "hurt",
		     &ship::hurt,
		     // bind variable types, too
		     "life",
		     &ship::life,
		     // names in lua don't have to be the same as C++,
		     // but it probably helps if they're kept the same,
		     // here we change it just to show its possible
		     "bullet_count",
		     &ship::bullets);
	}
	else {
		// set usertype explicitly, with the given name
		sol::usertype<ship> usertype_table
		     = lua.new_usertype<ship>("ship");
		usertype_table["shoot"] = &ship::shoot;
		usertype_table["hurt"] = &ship::hurt;
		usertype_table["life"] = &ship::life;
		usertype_table["bullet_count"] = &ship::bullets;
	}

	const auto& code = R"(
		fwoosh = ship.new()
		-- note the ":" that is there: this is mandatory for member function calls
		-- ":" means "pass self" in Lua
		local success = fwoosh:shoot()
		local is_dead = fwoosh:hurt(20)
		-- check if it works
		print(is_dead) -- the ship is not dead at this point
		print(fwoosh.life .. "life left") -- 80 life left
		print(fwoosh.bullet_count) -- 19
	)";


	lua.script(code);

	std::cout << std::endl;

	return 0;
}