Skip to content

Commit e0ffe71

Browse files
authored
remove mention of old fields, add more stuff
1 parent 0b8762a commit e0ffe71

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

tutorials/fields.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class $modify(PlayerObject) {
3636

3737
This code works even if you have multiple `PlayerObject`s, and the counter is initialized as 0 for each one, providing an elegant yet simple solution to the problem.
3838

39-
> :warning: If you are using old fields behavior, then **fields must be accessed through the `m_fields` member**. If you access them directly through `this`, you will get **undefined behaviour** (most likely a crash).
39+
Fields are declared just like normal member variables, but inside the special `Fields` struct. Even constructors and destructors work\*.
4040

41-
Fields are declared just like normal member variables, even constructors and destructors work\*.
41+
> :info: Fields are initialized only whenever they're first accessed, **not** when the modified class is originally created.
4242
4343
```cpp
4444
class $modify(PlayerObject) {
@@ -60,7 +60,20 @@ class $modify(PlayerObject) {
6060
};
6161
```
6262
63-
## Note about addresses
63+
# External Access
6464
65-
> :warning: If you are still using the old fields (which do not use the `Fields` struct), those ones are constructed and destructed in a different address than they exist normally (required for space optimization), so **if you have a class that depends on the address of `this` inside the constructor/destructor**, use `std::unique_ptr` to contain the said object. One such example would be Geode's events, since they are registered to a global map in their constructor.
65+
Fields can be accessed outside your modified class like usual, with the help of some casting.
6666
67+
> :warning: Do not use `typeinfo_cast` here, as it isn't actually an instance of `MyGameObject`.
68+
69+
```cpp
70+
class $modify(MyGameObject, GameObject) {
71+
struct Fields {
72+
int m_myField = 2;
73+
};
74+
};
75+
76+
GameObject* someObject = /*...*/;
77+
// `m_fields` is still required!
78+
static_cast<MyGameObject*>(someObject)->m_fields->m_myField = 12;
79+
```

0 commit comments

Comments
 (0)