diff --git a/README.md b/README.md index 07edce6..f6ca10d 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,7 @@ A guide for the usage of the [alt:V C++ Module SDK](https://github.com/altmp/cpp 4. [Creating your own resource class](creating-resource.md) 5. [Handling events](handling-events.md) 6. [MValues](mvalues.md) -7. [Refs](refs.md) -8. [Final steps](final-steps.md) +7. [Final steps](final-steps.md) ## Contribution diff --git a/articles/creating-resource.md b/articles/creating-resource.md index 069b78b..21afdbc 100644 --- a/articles/creating-resource.md +++ b/articles/creating-resource.md @@ -77,12 +77,12 @@ public: // Also useful for handling timers } - void OnCreateBaseObject(alt::Ref object) override + void OnCreateBaseObject(alt::IBaseObject* object) override { // Here you can e.g. add the base object to some list for validating if a base object still exists } - void OnRemoveBaseObject(alt::Ref object) override + void OnRemoveBaseObject(alt::IBaseObject* object) override { // Here you can e.g. remove the base object from some aforementioned list } diff --git a/articles/handling-events.md b/articles/handling-events.md index 70b6375..f8fd4a8 100644 --- a/articles/handling-events.md +++ b/articles/handling-events.md @@ -31,7 +31,7 @@ alt::ICore::Instance().SubscribeEvent(alt::CEvent::Type::PLAYER_CONNECT, [](cons auto event = static_cast(ev); // Gets the player that connected to the server - alt::Ref player = event->GetTarget(); + alt::IPlayer* player = event->GetTarget(); // Gets the connect reason alt::StringView reason = event->GetReason(); @@ -71,7 +71,7 @@ bool MyResource::OnEvent(const alt::CEvent* ev) auto event = static_cast(ev); // Gets the player that connected to the server - alt::Ref player = event->GetTarget(); + alt::IPlayer* player = event->GetTarget(); // Gets the connect reason alt::StringView reason = event->GetReason(); diff --git a/articles/mvalues.md b/articles/mvalues.md index ed16d4e..40ba186 100644 --- a/articles/mvalues.md +++ b/articles/mvalues.md @@ -65,8 +65,3 @@ if(type == alt::IMValue::Type::INT) Another example can be found in the [article about event handling](handling-events.md#how-to-emit-events). > All the methods to create MValues can be found [here](https://github.com/altmp/cpp-sdk/blob/dev/ICore.h#L61). - -## Refs - -Now you also know what an MValue is and how to use it. -The next article will explain [what a Ref is and how to use it](refs.md). diff --git a/articles/refs.md b/articles/refs.md deleted file mode 100644 index fc7dbf4..0000000 --- a/articles/refs.md +++ /dev/null @@ -1,66 +0,0 @@ -# Refs - -This article will explain what Refs are and how to use them. - -## What are Refs - -If you are familiar with modern C++ you probably know what **smart pointers** are, and how to use them. -A `Ref` is similiar to the popular `shared_ptr` container from the standard library. -It stores a heap-allocated object and controls its lifetime and memory by using refcounting. -If there are no more instances of the `Ref` exist, the stored object will be freed automatically. - -They are however different, as the `shared_ptr` stores the refcount on the `shared_ptr` container, while the -`Ref` in the SDK stores the refcount on the stored object inside the `Ref`. That is also why any object stored in a `Ref` must -inherit from [`alt::CRefCountable`](https://github.com/altmp/cpp-sdk/blob/master/CRefCountable.h). - -## How to use Refs - -To create a Ref you can simply use the `Ref` class. Once you create a ref with the pointer of your object you want to store, -make sure to NOT delete that pointer manually, the `Ref` is now responsible for managing the memory and lifetime of that object. - -The `Ref` class uses a template argument to specify which type of object is stored inside of it. - -The object stored inside the `Ref` can be accessed by using the `Get()` method. In most cases this is not necessary though, -as the `->` operator of the `Ref` is overloaded to access methods and properties of the object inside it without having to call `Get()`. - -A simple example of how to use a `Ref`: -```c++ -// To store our class in a Ref, -// we need to inherit from 'alt::CRefCountable' -class MyClass : public alt::CRefCountable -{ - int id; -public: - MyClass(int _id) : id(_id) {} - - int GetID() { return id; } -}; - -// We create a ref storing the instance of our class -// The ref now takes care of managing the object's memory -// Once the ref and each of its copies has been destroyed, -// the memory for the object stored will be freed. -alt::Ref myRef(new MyClass(1)); -``` -```c++ -// We receive a new copy of the ref here, -// so the ref count will be increased by 1, -// and the object can not be freed, -// as long as this copy exists. -int GetRefId(alt::Ref ref) -{ - // We use the overloaded '->' operator, - // to access the method on the stored object directly, - // without needing to call 'Get()' - return ref->GetID(); -} - -// We call this function that takes our ref and copies it, -// and then calls a method on it -int id = GetRefId(myRef); -``` - -The SDK uses `Refs` in many places where instances of objects are passed, usually you do not need to worry about the refcounting of the `Ref` etc. -You can safely store a copy of the `Ref` in your own `std::vector` or similiar, without having to think about the lifetime of the object. - -> To see all available methods on `Refs`, refer to the [header file in the SDK](https://github.com/altmp/cpp-sdk/blob/master/Ref.h). diff --git a/articles/toc.yml b/articles/toc.yml index 5b3c999..86f2eb3 100644 --- a/articles/toc.yml +++ b/articles/toc.yml @@ -10,8 +10,6 @@ href: handling-events.md - name: MValues href: mvalues.md -- name: Refs - href: refs.md - name: Modules for reference href: modules-for-reference.md - name: Client