Page 1 of 1
c++ error on arrays and pointers.
Posted: Sun Nov 21, 2004 3:19 am
by SARIN
im making a real time strategy and i have a problem. are u able to set an array as a pointer for an object? im setting it up like this;
//unit meshes
IAnimatedMesh* guymesh = smgr->getMesh("guy.3ds");
//unit arrays
IAnimatedMeshSceneNode* unita[60] = 0;
then i add an object
unita[5] = smgr->addAnimatedMeshSceneNode(guymesh);
i have found that the error is on the addanimatedmeshscenenode.
and its not a regular compiler error. the application, while starting, gives a windows error window that says "error on irrlicht.dll"
plz help. i just want to no if i can use an array in this way. if not, can u tell me any alternatives? (for RTS, dealing with units)
thx
Posted: Sun Nov 21, 2004 3:48 am
by Spintz
as far as C++ goes, that shouldn't be a problem. Are you sure the mesh is loading properly?
Posted: Sun Nov 21, 2004 5:18 am
by SARIN
ah, found my prob.
my code was actually
unita[numunita] = smgr->addAnimatedMeshSceneNode(rakermesh);
and numunita was an int. am i able to use a variable inside the pointer?
Posted: Sun Nov 21, 2004 5:43 am
by DarkWhoppy
This is similar to IrrSED... I use an array to kep track of models and stuff... but then I tried using a 'list' from core::list<> and using scenenodes as the template... but that didn't work either.
If anyone has info on this, please reply.
Posted: Sun Nov 21, 2004 9:46 am
by T101
unita[numunita] = smgr->addAnimatedMeshSceneNode(rakermesh);
and numunita was an int. am i able to use a variable inside the pointer?
Yes you can use a variable for that.
But you need to make sure your variable has a correct value. You need to initialise it to 0 before you first use it - and you need to check if it does not become too big.
Also, your array initialisation looks odd.
AFAIK you cannot initialise an array's contents with that syntax. At best, the first entry (unita[0]) would be initialised to 0, the rest remain garbage.
Proper array initialisation is of the format:
IAnimatedMeshSceneNode* unita[60] ={0,0,0,0,0,0,0,0
... etc };
Posted: Sun Nov 21, 2004 6:20 pm
by SARIN
no, still didnt work. here is my actual code then, from declaration to implementation (oooo, it rymes lol)
#include "Sicim.h"
//variables
int numunita = 0,numunitb,numunitc,numunitd,newunita;
//Keys
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if(event.EventType == EET_KEY_INPUT_EVENT)
{
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return true;
}
if(event.EventType == EET_MOUSE_INPUT_EVENT)
{
bool mouse[EMIE_LMOUSE_PRESSED_DOWN];
return true;
}
return false;
}
};
//main loop
int main()
{
MyEventReceiver receiver;
//the irrlicht device
IrrlichtDevice *device =
createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 16,
false, false, false, &receiver);
device->setWindowCaption(L"The Legend of Sicim");
//main stuff
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
ICameraSceneNode* cam = smgr->addCameraSceneNode(0,vector3df(0,100,0), vector3df(0,0,0));
//unit meshes
IAnimatedMesh* rakermesh = smgr->getMesh("raker.3ds");
//unit arrays
IAnimatedMeshSceneNode* unita[60] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//level
IAnimatedMesh* arena = smgr->getMesh("Arena.3ds");
arena->getMesh(0)->setMaterialFlag(EMF_LIGHTING, true);
//triangle selector
ISceneNode* oct = smgr->addOctTreeSceneNode(arena->getMesh(0));
ITriangleSelector* tris = smgr->createOctTreeTriangleSelector(arena->getMesh(0),oct,10);
oct->setTriangleSelector(tris);
tris->drop();
//loop
while(device->run())
{
driver->beginScene(true, true, SColor(0,0,0,0));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
if(keys[KEY_ESCAPE]) device->closeDevice();
if(keys[KEY_UP])
{
vector3df campos = cam->getPosition();
campos.Z=campos.Z+1;
cam->setPosition(campos);
}
if(keys[KEY_DOWN])
{
vector3df campos = cam->getPosition();
campos.Z=campos.Z-1;
cam->setPosition(campos);
}
if(keys[KEY_LEFT])
{
vector3df campos = cam->getPosition();
campos.X=campos.X-1;
cam->setPosition(campos);
}
if(keys[KEY_RIGHT])
{
vector3df campos = cam->getPosition();
campos.X=campos.X+1;
cam->setPosition(campos);
}
if(mouse[EMIE_LMOUSE_PRESSED_DOWN])
{
newunita=1;
}
if(newunita=1) {
unita[numunita] = smgr->addAnimatedMeshSceneNode(rakermesh);
aabbox3d<f32> box = unita[numunita]->getBoundingBox();
vector3df radius = box.MaxEdge - box.getCenter();
ISceneNodeAnimatorCollisionResponse* anim = smgr->createCollisionResponseAnimator(
tris, unita[numunita], radius,
vector3df(0,0,0), 0.0f,
vector3df(0,0,0));
unita[numunita]->addAnimator(anim);
newunita=0;
++numunita;
}
}
device->drop();
return 0;
}
and, here is my small header (ya, i no, pathetic)
#include <irrlicht.h>
#include <wchar.h>
#include <stdio.h>
#include <math.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#pragma comment(lib, "Irrlicht.lib")
bool keys[KEY_KEY_CODES_COUNT];
bool mouse[EMIE_LMOUSE_PRESSED_DOWN];
this is after i tried wat u said t101 (i may not have done it right still)
Posted: Mon Nov 22, 2004 1:53 am
by Spintz
I think I see your problem.
should be...
The problem you're having is when the mouse button is down, you're calling that code, but you aren't checking that the value of numunita, which is quickly getting over the value of 60, which is the size of you're array of IAnimatedMeshSceneNode*'s.
Posted: Mon Nov 22, 2004 2:39 am
by SARIN
THANKS!
finally, it works.
i worship all who replied (especially u spintz)
someone shoot me. i cant believe i didnt notice that.