FieldLayer Plugin Globals
-
Hello.
I am trying to create a FieldLayer plugin that would utilize global variables which would be used to effect the final values returned via Sample, for example an array to affect the final values. The purpose of using global variables is to keep the amount of overhead at a minimum for everytime Sample is called.
My issue comes in that the global variables are giving inconsistent results, including resetting to their default value. I've looked at the information in the sdk and the code examples and didn't find anything that explained why this would be happening, granted I might have completely missed something.
Here is a simplified version of my code using the HueShiftFieldLayer example. I know the code doesn't make sense for a proper plugin I'm just using it to highlight the problem I'm encountering.
#include "c4d.h" #include "c4d_symbols.h" #include "c4d_falloffplugin.h" #include "main.h" #include "c4d_fieldplugin.h" class HueShiftFieldLayer : public FieldLayerData { INSTANCEOF(HueShiftFieldLayer, FieldLayerData) public: static NodeData* Alloc() { iferr(NodeData * const result = NewObj(HueShiftFieldLayer)) { DebugStop(); return nullptr; } return result; } virtual maxon::Result<void> InitSampling(FieldLayer& layer, const FieldInfo& info, FieldShared& shared); virtual void FreeSampling(FieldLayer& layer, const FieldInfo& info, FieldShared& shared); virtual Bool IsEqual(const FieldLayer& layer, const FieldLayerData& comp) const; virtual maxon::Result<void> Sample(const FieldLayer& layer, const FieldInput& inputs, FieldOutputBlock& outputs, const FieldInfo& info) const; virtual void IncrementRun(); virtual Bool Message(GeListNode* node, Int32 type, void* t_data); Int32 incrementUp = 0; private: Float _offset = 0.0_f; }; Bool HueShiftFieldLayer::Message(GeListNode* node, Int32 type, void* t_data) { BaseDocument* doc = node->GetDocument(); if (!doc) return TRUE; BaseContainer *dataInstance = ((BaseList2D*)node)->GetDataInstance(); if (!dataInstance) return TRUE; IncrementRun(); // I would expect this number to keep incrementing up, instead it seems to almost randomly reset to 0 or continue from previous values ApplicationOutput("Count " + String::IntToString(incrementUp)); return TRUE; } void HueShiftFieldLayer::IncrementRun() { incrementUp++; } maxon::Result<void> HueShiftFieldLayer::InitSampling(FieldLayer& layer, const FieldInfo& info, FieldShared& shared) { return maxon::OK; } void HueShiftFieldLayer::FreeSampling(FieldLayer& layer, const FieldInfo& info, FieldShared& shared) { } Bool HueShiftFieldLayer::IsEqual(const FieldLayer& layer, const FieldLayerData& comp) const { return true; } maxon::Result<void> HueShiftFieldLayer::Sample(const FieldLayer& layer, const FieldInput& inputs, FieldOutputBlock& outputs, const FieldInfo& info) const { return maxon::OK; } Bool RegisterHueTest() { const Bool registerFieldLayerSuccess = RegisterFieldLayerPlugin(789456, "Hue Shift Layer"_s, "Changes the color hue."_s, "Hue Shift Layer"_s, FIELDLAYER_HASREMAP, HueShiftFieldLayer::Alloc, "Flhueshift"_s, nullptr, 0, nullptr); if (registerFieldLayerSuccess == false) return FALSE; else return registerFieldLayerSuccess; }
I tried doing the same global variable as above in a FieldObject plugin and I got the results that I would expect with my variable just incrementing up everytime Message is called.
Are global variables not able to be maintained inside of a FieldLayer plugin?
Any help would be greatly appreciated.
John Terenece
-
When using class-level variables as you are here, you should override CopyTo() and probably Read() and Write() as well. Cinema copies objects a lot without your knowing it, so if you don't implement CopyTo() the variables in the copy will revert to their default values, or worse, get some random value. Try that and see if it helps.
Steve
-
Thanks for the response, that seems to have solved the problem.