Added a few small networking related helper methods to Node. Made the rpc macros in Entity use them.

This commit is contained in:
Relintai 2022-08-19 00:36:24 +02:00
parent 1dc5d84196
commit 9d0c889583
3 changed files with 60 additions and 54 deletions

View File

@ -22,9 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include "core/containers/vector.h"
#include "core/object/object.h" #include "core/object/object.h"
#include "core/string/ustring.h" #include "core/string/ustring.h"
#include "core/containers/vector.h"
#include "core/io/networked_multiplayer_peer.h" #include "core/io/networked_multiplayer_peer.h"
@ -132,19 +132,19 @@ struct EntityStat {
// f.e. RPC(method, arg0, arg1, etc) // f.e. RPC(method, arg0, arg1, etc)
#define RPC(func, ...) \ #define RPC(func, ...) \
if (is_inside_tree() && get_tree()->has_network_peer() && get_tree()->is_network_server()) { \ if (is_network_server()) { \
rpc(#func, ##__VA_ARGS__); \ rpc(#func, ##__VA_ARGS__); \
} \ } \
func(__VA_ARGS__); func(__VA_ARGS__);
#define VRPC(func, ...) \ #define VRPC(func, ...) \
if (is_inside_tree() && get_tree()->has_network_peer() && get_tree()->is_network_server()) { \ if (is_network_server()) { \
vrpc(#func, ##__VA_ARGS__); \ vrpc(#func, ##__VA_ARGS__); \
} \ } \
func(__VA_ARGS__); func(__VA_ARGS__);
#define ORPC(func, ...) \ #define ORPC(func, ...) \
if (is_inside_tree() && get_tree()->has_network_peer()) { \ if (has_network_peer()) { \
if (get_tree()->is_network_server()) { \ if (get_tree()->is_network_server()) { \
if (get_network_master() != 1) { \ if (get_network_master() != 1) { \
rpc_id(get_network_master(), #func, ##__VA_ARGS__); \ rpc_id(get_network_master(), #func, ##__VA_ARGS__); \
@ -157,7 +157,7 @@ struct EntityStat {
} }
#define RPCS(func, ...) \ #define RPCS(func, ...) \
if (is_inside_tree() && get_tree()->has_network_peer()) { \ if (has_network_peer()) { \
if (get_tree()->is_network_server()) { \ if (get_tree()->is_network_server()) { \
func(__VA_ARGS__); \ func(__VA_ARGS__); \
} else { \ } else { \
@ -170,38 +170,38 @@ struct EntityStat {
//RPC Objects //RPC Objects
#define RPCOBJ(rpcfunc, rpc_var, normalfunc, normal_var) \ #define RPCOBJ(rpcfunc, rpc_var, normalfunc, normal_var) \
if (is_inside_tree() && get_tree()->has_network_peer()) { \ if (has_network_peer()) { \
rpc(#rpcfunc, rpc_var); \ rpc(#rpcfunc, rpc_var); \
} \ } \
normalfunc(normal_var); normalfunc(normal_var);
#define VRPCOBJ(rpcfunc, rpc_var, normalfunc, normal_var) \ #define VRPCOBJ(rpcfunc, rpc_var, normalfunc, normal_var) \
if (is_inside_tree() && get_tree()->has_network_peer()) { \ if (has_network_peer()) { \
vrpc(#rpcfunc, rpc_var); \ vrpc(#rpcfunc, rpc_var); \
} \ } \
normalfunc(normal_var); normalfunc(normal_var);
#define VRPCOBJ12(rpcfunc, rpc_var, normalfunc, normal_var1, normal_var2) \ #define VRPCOBJ12(rpcfunc, rpc_var, normalfunc, normal_var1, normal_var2) \
if (is_inside_tree() && get_tree()->has_network_peer() && get_tree()->is_network_server()) { \ if (is_network_server()) { \
vrpc(#rpcfunc, rpc_var); \ vrpc(#rpcfunc, rpc_var); \
} \ } \
normalfunc(normal_var1, normal_var2); normalfunc(normal_var1, normal_var2);
#define VRPCOBJP(rpcfunc, rpc_var1, rpc_var2, normalfunc, normal_var1, normal_var2) \ #define VRPCOBJP(rpcfunc, rpc_var1, rpc_var2, normalfunc, normal_var1, normal_var2) \
if (is_inside_tree() && get_tree()->has_network_peer() && get_tree()->is_network_server()) { \ if (is_network_server()) { \
vrpc(#rpcfunc, rpc_var1, rpc_var2); \ vrpc(#rpcfunc, rpc_var1, rpc_var2); \
} \ } \
normalfunc(normal_var1, normal_var2); normalfunc(normal_var1, normal_var2);
#define ORPCOBJ(rpcfunc, rpc_var, normalfunc, normal_var) \ #define ORPCOBJ(rpcfunc, rpc_var, normalfunc, normal_var) \
if (is_inside_tree() && get_tree()->has_network_peer()) { \ if (has_network_peer()) { \
if (get_tree()->is_network_server() && get_network_master() != 1) \ if (get_tree()->is_network_server() && get_network_master() != 1) \
rpc_id(get_network_master(), #rpcfunc, rpc_var); \ rpc_id(get_network_master(), #rpcfunc, rpc_var); \
} \ } \
normalfunc(normal_var); normalfunc(normal_var);
#define RPCSOBJ(rpcfunc, rpc_var, normalfunc, normal_var) \ #define RPCSOBJ(rpcfunc, rpc_var, normalfunc, normal_var) \
if (is_inside_tree() && get_tree()->has_network_peer()) { \ if (has_network_peer()) { \
if (get_tree()->is_network_server()) { \ if (get_tree()->is_network_server()) { \
normalfunc(normal_var); \ normalfunc(normal_var); \
} else { \ } else { \
@ -212,19 +212,12 @@ struct EntityStat {
} }
#define ORPCOBJP(rpcfunc, rpc_var1, rpc_var2, normalfunc, normal_var1, normal_var2) \ #define ORPCOBJP(rpcfunc, rpc_var1, rpc_var2, normalfunc, normal_var1, normal_var2) \
if (is_inside_tree() && get_tree()->has_network_peer()) { \ if (has_network_peer()) { \
if (get_tree()->is_network_server() && get_network_master() != 1) \ if (get_tree()->is_network_server() && get_network_master() != 1) \
rpc_id(get_network_master(), #rpcfunc, rpc_var1, rpc_var2); \ rpc_id(get_network_master(), #rpcfunc, rpc_var1, rpc_var2); \
} \ } \
normalfunc(normal_var1, normal_var2); normalfunc(normal_var1, normal_var2);
// f.e. RSET(rset("property", "value"), property, value)
#define RSET(rset_func, variable, value) \
if (is_inside_tree() && get_tree()->has_network_peer() && get_tree()->is_network_server()) { \
rset_func; \
} \
variable = value;
class Entity : public Node { class Entity : public Node {
GDCLASS(Entity, Node); GDCLASS(Entity, Node);

View File

@ -3289,6 +3289,10 @@ void Node::_bind_methods() {
ClassDB::bind_method(D_METHOD("_set_property_pinned", "property", "pinned"), &Node::set_property_pinned); ClassDB::bind_method(D_METHOD("_set_property_pinned", "property", "pinned"), &Node::set_property_pinned);
#endif #endif
ClassDB::bind_method(D_METHOD("has_network_peer"), &Node::has_network_peer);
ClassDB::bind_method(D_METHOD("is_network_server"), &Node::is_network_server);
ClassDB::bind_method(D_METHOD("is_network_client"), &Node::is_network_client);
ClassDB::bind_method(D_METHOD("sees_get", "index"), &Node::sees_get); ClassDB::bind_method(D_METHOD("sees_get", "index"), &Node::sees_get);
ClassDB::bind_method(D_METHOD("sees_remove_index", "index"), &Node::sees_remove_index); ClassDB::bind_method(D_METHOD("sees_remove_index", "index"), &Node::sees_remove_index);
ClassDB::bind_method(D_METHOD("sees_remove", "entity"), &Node::sees_remove); ClassDB::bind_method(D_METHOD("sees_remove", "entity"), &Node::sees_remove);

View File

@ -512,6 +512,15 @@ public:
bool is_displayed_folded() const; bool is_displayed_folded() const;
/* NETWORK */ /* NETWORK */
_FORCE_INLINE_ bool has_network_peer() {
return is_inside_tree() && get_tree()->has_network_peer();
}
_FORCE_INLINE_ bool is_network_server() {
return is_inside_tree() && get_tree()->has_network_peer() && get_tree()->is_network_server();
}
_FORCE_INLINE_ bool is_network_client() {
return is_inside_tree() && get_tree()->has_network_peer() && !get_tree()->is_network_server();
}
Node *sees_get(int p_index); Node *sees_get(int p_index);
void sees_remove_index(int p_index); void sees_remove_index(int p_index);