openplanet-nl / issues

Issue tracker for Openplanet.
10 stars 0 forks source link

Something with `editor.PluginMapType.MacroblockInstanceItemsResults` or `CGameCtnEditorScriptAnchoredObject` going wrong. #438

Open XertroV opened 6 months ago

XertroV commented 6 months ago

If I try and get a pointer to editor.PluginMapType.MacroblockInstanceItemsResults[0] (via a method that works for everything else, using Dev::SetOffsetNod) I get back gibberish 0x74206B63696C436B (kClick t). Accessing this obviously crashes the game.

To get something populated here, you can use the following snippet.

auto editor = cast<CGameCtnEditorFree>(GetApp().Editor);
auto mbInst = editor.PluginMapType.GetLatestMacroblockInstance();
if (mbInst !is null) {
    UI::Text("mb inst ptr" + Text::FormatPointer(Dev_GetPointerForNod(mbInst)));
    UI::Text("mb inst name" + mbInst.Id.GetName());
    UI::Text("mb inst order" + tostring(mbInst.Order));
    UI::Text("mb inst ref count" + tostring(Reflection::GetRefCount(mbInst)));
    if (UI::Button("Explore MacroblockInstance")) {
        ExploreNod("MacroblockInstance", mbInst);
    }
} else {
    UI::Text("No MacroblockInstance returned.");
}

if (UI::Button("Try creating mb inst")) {
    auto mbStr = "Stadium\\Macroblocks\\LightSculpture\\Spring\\FlowerWhiteSmall.Macroblock.Gbx";
    auto model = editor.PluginMapType.GetMacroblockModelFromFilePath(mbStr);
    if (model is null) {
        print("Failed to get MacroblockModel from file path.");
    } else {
        if (!editor.PluginMapType.PlaceMacroblock(model, int3(10, 10, 10), CGameEditorPluginMap::ECardinalDirections::North)) {
            warn("Failed to place Macroblock.");
        }
        auto mbInst = editor.PluginMapType.CreateMacroblockInstance(model, nat3(10, 10, 10), CGameEditorPluginMap::ECardinalDirections::North, CGameEditorPluginMap::EMapElemColor::Default, false);
        if (mbInst !is null) {
            print("inst not null");
            print("inst ptr: " + Text::FormatPointer(Dev_GetPointerForNod(mbInst)));
            print("inst name: " + mbInst.Id.GetName());
            print("inst order: " + mbInst.Order);
            print("inst ref count: " + Reflection::GetRefCount(mbInst));
            editor.PluginMapType.ComputeItemsForMacroblockInstance(mbInst);
            print("nb items: " + editor.PluginMapType.MacroblockInstanceItemsResults.Length);
            if (editor.PluginMapType.MacroblockInstanceItemsResults.Length > 0) {
                // do stuff with the item results here
                // print("items[0] ptr: " + Text::FormatPointer(Dev_GetPointerForNod(editor.PluginMapType.MacroblockInstanceItemsResults[0])));
                // accessing this will obvs crash the game b/c the pointer is not valid
                //print("items[0].IdName: " + editor.PluginMapType.MacroblockInstanceItemsResults[0].IdName);
                //print("items[0].Position: " + editor.PluginMapType.MacroblockInstanceItemsResults[0].Position.ToString());
                //print("items[0].ItemModel exists: " + (editor.PluginMapType.MacroblockInstanceItemsResults[0].ItemModel !is null));
            }
        } else {
            print("Failed to create MacroblockInstance.");
        }
    }
}