[Closed] C++ Pass objects as argument
I want to pass array of objects as argument and work on it’s items, but I got some exception and weird errors. This is my code:
#include <maxscript/maxscript.h>
#include <maxscript/macros/define_instantiation_functions.h>
def_visible_primitive(TestFunction, "TestFunction");
Value* TestFunction_cf(Value **arg_list, int count)
{
Array* nodesArray = (Array*)arg_list[0];
INodeTab* nodes = new INodeTab();
for (int i = 0; i < nodesArray->size; i++)
{
INode* node = (INode*)nodesArray->data[i];
nodes->AppendNode(node);
}
mprintf((*nodes)[0]->GetName());
return &ok;
}
Where is my mistake?
try this
#include <maxscript/maxscript.h>
#include <maxscript/macros/define_instantiation_functions.h>
def_visible_primitive(TestFunction, "TestFunction");
Value* TestFunction_cf(Value **arg_list, int count)
{
Array* nodesArray = NULL;
if (arg_list[0]->is_kind_of(class_tag(Array)))
nodesArray = (Array*)arg_list[0];
else
return &false_value;
INodeTab* nodes = new INodeTab();
for (int i = 0; i < nodesArray->size; i++)
{
INode* node = get_valid_node((MAXNode*)nodesArray->data[i], TestFunction);
nodes->AppendNode(&node);
}
mprintf((*nodes)[0]->GetName());
return &true_value;
}
I included <maxscript/maxwrapper/mxsobjects.h> and MaxNode is now valuable. But “get_valid_node” is still undefined, which library I should include?
First I should create static library from it, Right? Because It doesn’t have.
I saw the source code for the get_valid_node, I don’t want to raise error on deleted nodes, I just want to skip them. So I should create my own node check function:
#include <maxscript/maxscript.h>
#include <maxscript/maxwrapper/mxsobjects.h>
#include <maxscript/macros/define_instantiation_functions.h>
INode* MaxNodeToINode(Value* value)
{
INode* node = NULL;
if (is_node(value))
{
MAXNode* maxNode = (MAXNode*)value;
if (!deletion_check_test(maxNode)) node = maxNode->to_node();
}
return node;
}
INodeTab* MaxArrayToINodeTab(Value* value)
{
INodeTab* nodeTab = new INodeTab();
if (value->is_kind_of(class_tag(Array)))
{
Array* maxArray = NULL;
maxArray = (Array*)value;
for (int i = 0; i < maxArray->size; i++)
{
INode* node = MaxNodeToINode(maxArray->data[i]);
if (node != NULL) nodeTab->AppendNode(node);
}
}
return nodeTab;
}
def_visible_primitive(TestFunction, "TestFunction");
Value* TestFunction_cf(Value **arg_list, int count)
{
INodeTab* nodeTab = MaxArrayToINodeTab(arg_list[0]);
if (nodeTab->Count() == 0) return &false_value;
mprintf((*nodeTab)[0]->GetName());
return &true_value;
}
INodeTab* nodeTab = new INodeTab();
pretty bad style (and as coded it can leak memory), either pass it to the function as a reference or return the object it’s self. We use things like tabs and vectors so we don’t have use
int* array = new int[100];
I don’t get it, I just moved your code to a function. I should not create new instance of INodeTab? Would you please update entire code?
something like this…
void MaxArrayToINodeTab(Value* value, INodeTab& nodes)
{
if (value->is_kind_of(class_tag(Array)))
{
Array* maxArray = static_cast<Array*>(value);
for (int i = 0; i < maxArray->size; ++i)
{
INode* node = MaxNodeToINode(maxArray->data[i]);
if(node)
nodes.AppendNode(node);
}
}
}
def_visible_primitive(TestFunction, "TestFunction");
Value* TestFunction_cf(Value **arg_list, int count)
{
INodeTab nodeTab;
MaxArrayToINodeTab(arg_list[0], nodeTab);
if (nodeTab.Count() == 0)
return &false_value;
mprintf(nodeTab[0]->GetName());
return &true_value;
}