hi,
i try to make my own class and functions but i don’t know how to initialise scene, cache or others nodes within it so that when i use my function in the main.cpp, it use the nodes already set.
could someone tell me how to do that please ?
here is the function i’m trying to make.
void Module::addRandomModule(Scene* scene, ResourceCache* cache, int m_id)
{
const char* moduleX;
//XML
tinyxml2::XMLDocument modules;
modules.Parse("Data/Scripts/Modules.xml");
if(modules.LoadFile("Data/Scripts/Modules.xml") == tinyxml2::XML_NO_ERROR)
{
tinyxml2::XMLNode* root = modules.FirstChild();
tinyxml2::XMLElement* infos = root->FirstChildElement("infos");
infos->QueryIntAttribute("count", &modulesCount);
int randomLine = chooseRandomLine(modulesCount);
std::string module = ("module");
{
std::ostringstream ss;
ss<<randomLine;
std::string s(ss.str());
module.append(s);
}
moduleX = module.c_str();
tinyxml2::XMLElement* randomModule = root->FirstChildElement(moduleX);
randomModule->QueryIntAttribute("type", &m_type); //moduletype);
randomModule->QueryIntAttribute("exits", &m_exits);
m_path = randomModule->Attribute("path");
m_texturepath = randomModule->Attribute("texturepath");
}
std::string moduleName = ("");
{
std::ostringstream ss;
ss<<m_id<<moduleX;
std::string s(ss.str());
moduleName.append(s);
}
m_name = moduleName.c_str();
moduleNode=scene->CreateChild(m_name);
moduleNode->SetPosition(Vector3(0,0,0));
AnimatedModel* moduleObject=moduleNode->CreateComponent<AnimatedModel>();
moduleObject->SetModel(cache->GetResource<Model>(m_path));
moduleObject->SetMaterial(cache->GetResource<Material>(m_texturepath));
}
the class WIP :
[spoiler]module.h
[code]
#ifndef MODULE_H
#define MODULE_H
#include <Urho3D/Math/Vector3.h>
using namespace Urho3D;
class Module
{
private:
int m_id;
const char* m_name;
int m_type;
int m_exits;
const char* m_path;
const char* m_texturepath;
public:
Module(); //constructeur
~Module(); //destructeur
void addRandomModule(Scene* scene, ResourceCache* cache, int m_id);
int getType(void);
int getExits(void);
void setPosition(Vector3 modulePos);
};
#endif[/code]
module.cpp
[code]
#include <Urho3D/Engine/Application.h>
#include <Urho3D/Engine/Engine.h>
#include <Urho3D/Input/InputEvents.h>
#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Resource/XMLFile.h>
#include <Urho3D/IO/Log.h>
#include <Urho3D/UI/UI.h>
#include <Urho3D/UI/Text.h>
#include <Urho3D/UI/Font.h>
#include <Urho3D/UI/Button.h>
#include <Urho3D/UI/UIEvents.h>
#include <Urho3D/UI/Window.h>
#include <Urho3D/Scene/Scene.h>
#include <Urho3D/Scene/SceneEvents.h>
#include <Urho3D/Graphics/Graphics.h>
#include <Urho3D/Graphics/Camera.h>
#include <Urho3D/Graphics/Geometry.h>
#include <Urho3D/Graphics/Renderer.h>
#include <Urho3D/Graphics/DebugRenderer.h>
#include <Urho3D/Graphics/Octree.h>
#include <Urho3D/Graphics/Light.h>
#include <Urho3D/Graphics/Model.h>
#include <Urho3D/Graphics/StaticModel.h>
#include <Urho3D/Graphics/AnimatedModel.h>
#include <Urho3D/Graphics/Material.h>
#include <Urho3D/Graphics/Skybox.h>
#include <Urho3D/UI/Window.h>
#include <Urho3D/Math/Vector3.h>
#include
#include
#include
#include
#include <time.h>
//my stuff
#include “tinyxml2.h”
#include “fonctions.h”
#include “Module.h”
int modulesCount;
Module::Module() //constructeur qui initialise
{
int m_id; // = 0;
const char* m_name;
int m_type; // = 0;
int m_exits; // = 0;
const char* m_path; // = “”;
const char* m_texturepath; // = “”;
}
Module::~Module() //destructeur
{
}
void Module::addRandomModule(Scene* scene, ResourceCache* cache, int m_id)
{
const char* moduleX;
//XML
tinyxml2::XMLDocument modules;
modules.Parse("Data/Scripts/Modules.xml");
if(modules.LoadFile("Data/Scripts/Modules.xml") == tinyxml2::XML_NO_ERROR)
{
tinyxml2::XMLNode* root = modules.FirstChild();
tinyxml2::XMLElement* infos = root->FirstChildElement("infos");
infos->QueryIntAttribute("count", &modulesCount);
int randomLine = chooseRandomLine(modulesCount);
std::string module = ("module");
{
std::ostringstream ss;
ss<<randomLine;
std::string s(ss.str());
module.append(s);
}
moduleX = module.c_str();
tinyxml2::XMLElement* randomModule = root->FirstChildElement(moduleX);
randomModule->QueryIntAttribute("type", &m_type); //moduletype);
randomModule->QueryIntAttribute("exits", &m_exits);
m_path = randomModule->Attribute("path");
m_texturepath = randomModule->Attribute("texturepath");
}
std::string moduleName = ("");
{
std::ostringstream ss;
ss<<m_id<<moduleX;
std::string s(ss.str());
moduleName.append(s);
}
m_name = moduleName.c_str();
moduleNode=scene->CreateChild(m_name);
moduleNode->SetPosition(Vector3(0,0,0));
AnimatedModel* moduleObject=moduleNode->CreateComponent<AnimatedModel>();
moduleObject->SetModel(cache->GetResource<Model>(m_path));
moduleObject->SetMaterial(cache->GetResource<Material>(m_texturepath));
}
int Module::getType(void)
{
return m_type;
}
int Module::getExits(void)
{
return m_exits;
}
void Module::setPosition(Vector3 modulePos)
{
}[/code][/spoiler]