Topic: Programming question

Hello!
I've started f** with c++(cuz I'm noob in it and espeshially in maratis engine)
I have a question

Here is my OnBeginFunction

....
MOText * TextEntity= NULL;
....
void MyGame::onBeginScene(void)
{
    MGame::onBeginScene(); // call the default MGame begin scene
    
    MEngine * engine = MEngine::getInstance(); // get the engine instance
    MLevel * level = engine->getLevel(); // get the current level
    
    
    // get GUI scene and objects
    menu = level->getSceneByName("menu");
    //mouseEntity = menu->getEntityByName("Mouse");
    //barEntity = menu->getEntityByName("Bar");
    TextEntity = menu->getTextByName("Text0");
}

it compiles well but
when I launch game, Maratis crashes and when I del

TextEntity = menu->getTextByName("Text0");

it works well, so what's wrong?
---------------------------------
p.s. Can we put an engine log to console, to find where is a mistake, and if it exists, just disable plugin(as it made with lua)

Last edited by hog (2014-05-17 18:50:35)

Re: Programming question

Hi,

from what I can see, I'll say that there is no scene named "menu" and getSceneByName return NULL,
so you should always add this by security :

menu = level->getSceneByName("menu");
if(menu)
{
    //mouseEntity = menu->getEntityByName("Mouse");
    //barEntity = menu->getEntityByName("Bar");
    TextEntity = menu->getTextByName("Text0");
}
else
{
    MLOG_WARNING("no scene called menu found");
}

For logs, you can use "MLOG_ERROR" or "MLOG_WARNING" (like above)
it will be written in a file called "maratis_log.txt" at the root of Maratis,
or launch Maratis from a command line and you'll see the logs in realtime.

The crash cannot be avoided if there is a bad access memory because it's native code and not a scripting langage.
In Maratis a lot of pointers are returned NULL when the object is not found.

Re: Programming question

Thanks for your reply, anael
it's something strange, I did have scene "menu"
http://s1.hostingkartinok.com/uploads/images/2014/05/49fd1928871a8a7b38c40c57d56ff33c.png
and it crashes with your code too, in maratis_log there is only

Log tracked at level :6
Info     Render version : 3.3.0     in main     in d:\maratis_svn\trunk\dev\maratis\editor\main.cpp

when I add any code after

menu = level->getSceneByName("menu");

  (even MLOG_WARNING) it crashes

Last edited by hog (2014-05-18 11:04:07)

Re: Programming question

What version are you using ? The windows version you downloaded from Maratis website ?
What compiler are you using ?

check this :
- Maratis.exe is compiled with microsoft compiler, a plugin used with this exe need to be compiled with the same compiler
  (mingw won't work with this exe)
- be sure you are linking to the same MCore.dll and MEngine.dll used by Maratis.exe
- try to compile your plugin, Game.dll, in Release mode (not Debug)

What happen when you compile this example ?
http://www.maratis3d.org/?p=500

Re: Programming question

The version is 3.21 beta win32 (downoloaded from http://www.maratis3d.org/?page_id=57)
My system is win7 x64
I use VS2010 Express with SP1
The libraries and includes for project are the same ver (in SDK folder from maratis archive)
in Release mode, all does the same.
And all the tutorial and demo plugins work well
I can send you my project and whole the code for better view

It crashes even with it(MyGame.cpp):

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// MyGame.cpp
// 
// Code : Anael Seghezzi
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "MyGame.h"

// constructor
MyGame::MyGame(void):
MGame()
{}

// destructor
MyGame::~MyGame(void)
{}

//MOText * TextEntity= NULL;
// custom begin scene
void MyGame::onBeginScene(void)
{
    MGame::onBeginScene(); // call the default MGame begin scene
    
    MEngine * engine = MEngine::getInstance(); // get the engine instance
    MLevel * level = engine->getLevel(); // get the current level


    MLOG_ERROR("here it is");

}

may be I miss some basic functions to render or smth like this?

Last edited by hog (2014-05-18 18:59:41)

Re: Programming question

Must be something wrong in the other files,
if you can show the full project code I'll have a look.

Re: Programming question

http://yadi.sk/d/n8xRjmUGQhDUv

Re: Programming question

i don't see anything wrong in the code.

Did you compile the plugin in 32bit ?

Re: Programming question

Yes, and that's only 32 bit compilation in vs by default
Have you tested this plugin(with maratis editor)?

Last edited by hog (2014-05-19 12:12:31)

Re: Programming question

yes I tested it, but on mac,
I just noticed "MLOG_ERROR" was not printing "here it is" in the console I don't know why,
it can be replaced by "printf("here it is");" but I don't think it's what causes a crash in your machine.

The code is the same as this : http://www.maratis3d.org/?p=500
What I don't understand is when you compile the tutorial you said it was working.

Re: Programming question

Check again you are really in release mode :
http://stackoverflow.com/questions/3689 … press-2010

12

Re: Programming question

http://s7.hostingkartinok.com/uploads/images/2014/05/b11afcbdefdfb4708f2df6c39bab6f55.png
I'm really in release mode.
"Simple Game plugin" and "Gui demo" were compiled in debug, and worked perfectly

13

Re: Programming question

That's really strange, but I copied MyGame.cpp from here
http://www.maratis3d.org/code/simplePlugin/MyGame.cpp
renamed scene's objects and added some code:

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// MyGame.cpp
// 
// Code : Anael Seghezzi
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "MyGame.h"


// constructor
MyGame::MyGame(void):
MGame()
{}

// destructor
MyGame::~MyGame(void)
{}

MScene * MenuScene = NULL;
MOText * MenuText = NULL;

void MyGame::onBeginScene(void)
{
    MGame::onBeginScene(); // call the default MGame begin scene
    
    MEngine * engine = MEngine::getInstance(); // get the engine instance
    MLevel * level = engine->getLevel(); // get the current level
    printf("use");
    MenuScene=level->getSceneByName("menu");
    MenuText=MenuScene->getTextByName("menuText");
    
}

void MyGame::update(void)
{
    MEngine * engine = MEngine::getInstance(); // get the engine instance
    MSystemContext * system = engine->getSystemContext(); // get system context
    MInputContext * input = engine->getInputContext();// get input context
    if(MenuScene){
        if(input->onKeyDown("MOUSE_BUTTON1")){
            printf("yeah!");
        }
    }

    MGame::update();
}

And it works!!!
Great thanks, anael, for your support
MLOG_ERROR and MLOG_WARRNING still don't work, but printf("..."); works as I want.

Re: Programming question

: ) good

for MLOG_ERROR and MLOG_WARNING, is it just not printing the message in the console like I experienced or is it making the plugin crash ?

15

Re: Programming question

It crashes plugin
hmm, I had another question
How can I track mouse click on text?(By engine) Do we have speshial function, which tracks clicks?
http://s1.hostingkartinok.com/uploads/images/2014/05/d491156cdb0bcb1d27cde28ef3b8fe49.png
as 2nd variant I can take 4 ponts and if mouse is inside, highlight text/play sound, etc.(but it's not really "interesting" -
the great solution is to make behavior, which does luascript on click)
or I should use 3d to 2d points conversion?

Last edited by hog (2014-05-20 12:38:58)

Re: Programming question

In this case you can use the bounding box of the text and send a ray :

unsigned int width = 0;
unsigned int height = 0;
system->getScreenSize(&width, &height);

float mouseX = input->getAxis("MOUSE_X");
float mouseY = input->getAxis("MOUSE_Y");

// ray
MVector3 origin = camera->getTransformedPosition();
MVector3 dest = camera->getUnProjectedPoint(MVector3(mouseX*width, (1.0f - mouseY)*height, 1.0f));

// bounding box
MBox3d * bbox = text->getBoundingBox();

if(isEdgeToBoxCollision(origin, dest, bbox->min, bbox->max))
    // intersection

17

Re: Programming question

cool, I coudn't even imagine - we can do almost everything with maratis )
I decided to create new behavior
That's one problem - my behavior doesn't take place in behavior list
Could you have a look?
MouseClick.h

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// MouseClick.h
// 
// Code : Anael Seghezzi
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef _MOUSE_CLICK_H
#define _MOUSE_CLICK_H

#include <MEngine.h>


class MouseClick : public MBehavior
{
public:

    // constructors / destructors
    MouseClick(MObject3d * parentObject);
    MouseClick(MouseClick & behavior, MObject3d * parentObject);
    ~MouseClick(void);

private:
    
    // custom variables

    const char* m_cameraName;
    
public:

    // destroy
    void destroy(void);

    // get new
    static MBehavior * getNew(MObject3d * parentObject);

    // get copy
    MBehavior * getCopy(MObject3d * parentObject);

    // name
    static const char * getStaticName(void){ return "MouseClick"; }
    const char * getName(void){ return getStaticName(); }

    // variables
    unsigned int getVariablesNumber(void);
    MVariable getVariable(unsigned int id);

    // events (virtuals from MBehavior class)
    void update(void);
    void onBeginScene(void);
    void runEvent(int param){}
};

#endif

MouseClick.cpp

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// MouseClick.cpp
// 
// Code : Anael Seghezzi
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "MouseClick.h"


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Init, this part is always similar, constructor, copy constructor etc
/////////////////////////////////////////////////////////////////////////////////////////////////////////

// constructor
MouseClick::MouseClick(MObject3d * parentObject):
MBehavior(parentObject),
m_cameraName(NULL)
{}

// copy constructor
MouseClick::MouseClick(MouseClick & behavior, MObject3d * parentObject):
MBehavior(parentObject),
m_cameraName(behavior.m_cameraName)
{}

// destructor
MouseClick::~MouseClick(void)
{}

// destroy function : always similar
void MouseClick::destroy(void)
{
    delete this;
}

// getNew function : always similar
MBehavior * MouseClick::getNew(MObject3d * parentObject)
{
    return new MouseClick(parentObject);
}

// getCopy function : always similar
MBehavior * MouseClick::getCopy(MObject3d * parentObject)
{
    return new MouseClick(*this, parentObject);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Variables, allow to access custom variable from script and from Maratis Editor
/////////////////////////////////////////////////////////////////////////////////////////////////////////

unsigned int MouseClick::getVariablesNumber(void){
    return 1;
}

MVariable MouseClick::getVariable(unsigned int id)
{
    switch(id)
    {
    default:
        return MVariable("NULL", NULL, M_VARIABLE_NULL);
    case 0:
        return MVariable("Camera name", &m_cameraName, M_VARIABLE_STRING);
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
/////////////////////////////////////////////////////////////////////////////////////////////////////////
MScene * MenuScene = NULL;
MOText * MenuText = NULL;
MLevel * MenuLevel=NULL;
MOCamera * MenuCamera=NULL;
MInputContext * input=NULL;
void MouseClick::onBeginScene(void)
{
     //MGame::onBeginScene(); 
    
    MEngine * engine = MEngine::getInstance(); // get the engine instance
    MLevel * level = engine->getLevel(); // get the current level
    MObject3d * parent = getParentObject();
    //printf("use");
    //MenuScene=level->getSceneByName("menu");
    MenuText=MenuScene->getTextByName(parent->getName());
    MenuLevel= engine->getLevel();
    MenuCamera=MenuScene->getCameraByName(m_cameraName);

    
}


// update function (called by MGame class by default)
void MouseClick::update(void)
{
    MEngine * engine = MEngine::getInstance();
    MGame * game = engine->getGame();
    unsigned int width = 0;
    unsigned int height = 0;
    MSystemContext * system = engine->getSystemContext();
        system->getScreenSize(&width, &height);


    if(! game->isRunning())
        return;

float mouseX = input->getAxis("MOUSE_X");
float mouseY = input->getAxis("MOUSE_Y");
MVector3 origin = MenuCamera->getTransformedPosition();
MVector3 dest = MenuCamera->getUnProjectedPoint(MVector3(mouseX*width, (1.0f - mouseY)*height, 1.0f));
MBox3d * bbox = MenuText->getBoundingBox();
    if(isEdgeToBoxCollision(origin, dest, bbox->min, bbox->max)){
        printf("it works!");
    }
    // get the associated parent object (who is using this behavior)
    //MObject3d * parent = getParentObject();

    // lets rotate the parent object around the z axis, using our custom "rotationSpeed" variable
    //parent->addAxisAngleRotation(MVector3(0, 0, 1), m_rotationSpeed);
    
}

And did you have any project now? Really interesting

Last edited by hog (2014-05-21 09:15:15)

Re: Programming question

Don't forget to declare your behavior in StartPlugin :

void StartPlugin(void)
{
    // get engine
    MEngine * engine = MEngine::getInstance();

    // add behaviors
    MBehaviorManager * behaviorManager = engine->getBehaviorManager();
    behaviorManager->addBehavior(MouseClick::getStaticName(), M_OBJECT3D, MouseClick::getNew); // here
}

19

Re: Programming question

thanks, I'll try it

20

Re: Programming question

can i add OnBeginScene to my behavior?
i've tried to add virtual void to mbehavior.h, and to mouseclick.h and to mybehavior.h, but after that use of mybehavior(from example) and mouseclick behaviors crash maratis

Re: Programming question

MBehavior don't have onBeginScene, it's only for MGame :
https://code.google.com/p/maratis/sourc … Behavior.h

But in your game's onBeginScene, you can call your native behaviors,
or you can call behavior->runEvent(someNumber) and implement "runEvent" in your behavior.

22

Re: Programming question

oooooooops, I did something wrong
1)How do we pick up behaviors? I did in this way, but does it mean that i take first behavior on object?

ClickBeh=MenuText->getBehavior(0);
ClickBeh->runEvent(0);


2)where do i write runEvent?
I declare it in header file in this way

void runEvent(int param);

and then in cpp file of beh:

void MouseClick::runEvent(int param=0){
...
}

can i do in this way?(maratis crashes(it's my classic phrase now :D )

Last edited by hog (2014-05-29 17:00:54)

Re: Programming question

1)How do we pick up behaviors? I did in this way, but does it mean that i take first behavior on object?

yes, and yes.
Just be sure there is a behavior before calling getBehavior(0), check how much behaviors are attached to the object by using getBehaviorsNumber().

2)where do i write runEvent?

yes, but I don't think you are allowed to set a default value for param in a virtual function : "int param=0"
do it this way :

void MouseClick::runEvent(int param) // use param as a code
{
    switch(param)
    {
        case 0:
        do_something();
        break;

        case 1001:
        scene_begin();
        break;

        // etc
    }
}

24

Re: Programming question

Huge help!

25

Re: Programming question

anael wrote:

In this case you can use the bounding box of the text and send a ray :

unsigned int width = 0;
unsigned int height = 0;
system->getScreenSize(&width, &height);

float mouseX = input->getAxis("MOUSE_X");
float mouseY = input->getAxis("MOUSE_Y");

// ray
MVector3 origin = camera->getTransformedPosition();
MVector3 dest = camera->getUnProjectedPoint(MVector3(mouseX*width, (1.0f - mouseY)*height, 1.0f));

// bounding box
MBox3d * bbox = text->getBoundingBox();

if(isEdgeToBoxCollision(origin, dest, bbox->min, bbox->max))
    // intersection

I've fixed crashes, but it doesn't work.
when I put my resolution instead of height/width it doesn't work too
Any ideas?
hmm, and when i use plugin and save scene with MouseClick behavior on object, the next time i open it, i take an empty scene ( have to back up), even if i didn't change Game.dll

Last edited by hog (2014-06-01 17:57:39)