Forgottenserver: How to use the new `get` method of custom attributes?

Created on 28 Oct 2019  路  4Comments  路  Source: otland/forgottenserver

Greetings to all, I have a problem regarding the use of custom attributes
Well, it's about I want to use the following functions:
ItemAttributes::CustomAttribute::get but I can't find how to use it!

in fact, try to look for an example in the whole source, but apparently there is none.

What I have tried is to use it as follows:
int64_t value = item->getCustomAttribute("exampleKey")->get<int64_t>();
but it doesn't let me use the get method, because object isconst ItemAttributes::CustomAttribute, try changing the method to work with const ItemAttributes::CustomAttribute but it causes an error to get the result.
Screenshot_1

question

All 4 comments

int64_t value = const_cast<const Item*>(item)->getCustomAttribute("exampleKey")->get<int64_t>();

Next time head to otland support board, thats where questions belongs.

Thanks for answering, but your solution does not work, the getCustomAttribute method is an explicit function of Item * and not of const Item *, I hope you were wrong.

By the way, create a second list of get functions that are compatible with const ItemAttributes :: CustomAttribute* and it works fine, only when a value exists, if the value does not exist, will the server crashe when trying to get it.

apparently the if (value.type () == typeid (int64_t)) { return boost :: get <int64_t> (value); } it doesn't work well, because it doesn't matter if there is an integer in the variant or not, just try to transform it and POFF the server crashes.

I am sure the problem is in this part: if (value.type() == typeid(int64_t)) {, The same for the other types of values.

It never reaches this part: return emptyInt;

Thanks!

Yeah my bad, wrong place.

const ItemAttributes::CustomAttribute* attr = item->getCustomAttribute("attr");
if (attr) {
    int64_t val = const_cast<ItemAttributes::CustomAttribute*>(attr)->get<int64_t>();
}

It works correctly, now that I see it, I was just failing to check the attribute.
Forgot that it is now a pointer and if it has no value, returned nullptr.
Very Thank Nekiro!

:: Solved ::

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Olrios picture Olrios  路  4Comments

dudantas picture dudantas  路  4Comments

Imperians picture Imperians  路  4Comments

conde2 picture conde2  路  6Comments

souzajunior picture souzajunior  路  4Comments