Skip to content

Commit

Permalink
Add Variant::as_object()
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Aug 16, 2024
1 parent 5cedf7b commit d4e24e2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ This extension exists to allow Godot creators to implement safe modding support,
- Call functions and attach signals like GDScript

```C++
static void add_coin(const Node2D& player) {
static void add_coin(const Node& player) {
static int coins = 0;
coins ++;
auto coinlabel = player.get_parent().get_node("Texts/CoinLabel");
Expand All @@ -55,18 +55,20 @@ static void add_coin(const Node2D& player) {
}

extern "C" Variant _on_body_entered(Variant arg) {
Node2D player_node = arg.as_node2d();
Node player_node = arg.as_node();
if (player_node.get_name() != "Player")
return {};

Node2D(".").queue_free(); // Remove the current coin!
Node(".").queue_free(); // Remove the current coin!
add_coin(player_node);
return {};
}
```
Script of a simple Coin pickup, with a counter that updates a label in the tree of the player. This script can be attached to the Coin just like GDScript.
You may also have a look at our [demo repository](https://github.com/libriscv/godot-sandbox-demo) for the Godot Sandbox. It's a tiny platformer that uses C++ and Rust.
### What can I do?
- You can implement a modding API for your game, to be used inside the sandbox. This API can then be used by other players to extend your game, in a safe manner. That is, they can send their mod to other people, including you, and they (and you) can assume that it is safe to try out the mod. The mod is *not supposed* to be able to do any harm. That is the whole point of this extension.
Expand Down
6 changes: 6 additions & 0 deletions program/cpp/api/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ struct Object {
uint64_t m_address;
};

inline Object Variant::as_object() const {
if (get_type() == Variant::OBJECT)
return Object{uintptr_t(v.i)};
throw std::bad_cast();
}

template <typename... Args>
inline Variant Object::call(const std::string &method, Args... args) {
Variant argv[] = {args...};
Expand Down
3 changes: 2 additions & 1 deletion program/cpp/api/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct is_string
template<class T>
struct is_stdstring : public std::is_same<T, std::basic_string<char>> {};

struct Node; struct Node2D; struct Node3D;
struct Object; struct Node; struct Node2D; struct Node3D;
#include "vector.hpp"

struct Variant
Expand Down Expand Up @@ -136,6 +136,7 @@ struct Variant
operator std::string_view() const; // View for STRING and PACKED_BYTE_ARRAY
operator std::span<uint8_t>() const; // Modifiable span for PACKED_BYTE_ARRAY

Object as_object() const;
Node as_node() const;
Node2D as_node2d() const;
Node3D as_node3d() const;
Expand Down

0 comments on commit d4e24e2

Please sign in to comment.