Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / A bullet vector not working

Author
Message
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 20th Jan 2011 21:22
Hi all,

OK its just another bump in the road and it contains "vector".

Can someone tell me what is wrong with this code:


This is what i am trying to do is:

Make a struct of bul containing object number and its counter life.
Make a vector of bul so i can have multiple bul and dynamically arrange them as i want.
Update each bul so that its life is -- and if 0 delete the object and the element.

Here's the problem


ALLAH IS THE GREATEST
May he forgive me
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 20th Jan 2011 23:37 Edited at: 20th Jan 2011 23:39
There are a lot of problems here:


Should probably be:



And:

You need to actually add something to the array.

Also, in your updatebullet function-you need to use the -> operator when accessing members of pointers to classes/structs.

So you'd probably want something like:


In addition to this-I think your program would crash as you never set the pointers in the array to point to anything. I assume that when you call push_back you'd want to put something like


or something. And then you'd have to delete them when you're finished.

Finally, when using erase with vectors, I always do it like this



You might also want to have the bullets have a bool that contains whether they're alive or dead, and then instead of constantly resizing the array and deleting and adding stuff, you can just "reset" that bullet to have a different id and new lifespan rather than creating a new bullet

EDIT: Just realized


That might make your program crash-since the array goes from 0 - (arraysize -1)...so you should make the <= to a <
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 21st Jan 2011 00:19
@puppyofkosh

Thnx for the input.But i have another issue with this code.


This function(i have declared it) is not returning a proper value for my objects , any ideas.

ALLAH IS THE GREATEST
May he forgive me
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 21st Jan 2011 00:28 Edited at: 21st Jan 2011 00:29
Oh and i did what you told me but now i have different kind of error at compile.:


libcpmtd.lib(xdebug.obj) : warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
Main.obj : error LNK2019: unresolved external symbol __CrtDbgReportW referenced in function "public: struct bullet * & __thiscall std::vector<struct bullet *,class std::allocator<struct bullet *> >::operator[](unsigned int)" (??A?$vector@PAUbullet@@V?$allocator@PAUbullet@@@std@@@std@@QAEAAPAUbullet@@I@Z)
libcpmtd.lib(stdthrow.obj) : error LNK2001: unresolved external symbol __CrtDbgReportW
libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new(unsigned int,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z)
libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __free_dbg referenced in function "void __cdecl operator delete(void *,struct std::_DebugHeapTag_t const &,char *,int)" (??3@YAXPAXABU_DebugHeapTag_t@std@@PADH@Z)
Debug\Project PAF vs Aliens.exe : fatal error LNK1120: 3 unresolved externals
Build log was saved at "file://f:\arif projects\Project PAF vs Aliens\Project PAF vs Aliens\Debug\BuildLog.htm"
Project PAF vs Aliens - 5 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


Here is my rewritten code:


struct bullet
{
int id;
int life;
};
vector<bullet*> bul;

void makebullet(int obj,int limb, int lif)
{
int i=bul.size() + 201;
bul.push_back(new bullet);
dbMakeObjectCube(i,10.0);
float x,y,z,ax,ay,az;
x=dbObjectPositionX(i);
y=dbObjectPositionY(i);
z=dbObjectPositionZ(i);
ax=dbObjectAngleX(i);
ay=dbObjectAngleY(i);
az=dbObjectAngleZ(i);
dbPositionObject(i,x,y,z);
dbRotateObject(i,ax,ay,az);
}

void updatebullet()
{
for ( int i=0 ; i<bul.size();i++)
{
if ( dbObjectExist(bul[i]->id))
{
dbMoveObject(bul[i]->id,1);
bul[i]->life--;
if (bul[i]->life<0)
{
dbDeleteObject(bul[i]->id);
delete bul[i];
bul.erase(bul.begin()+i);

}
}
}
}


ALLAH IS THE GREATEST
May he forgive me
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 21st Jan 2011 00:29 Edited at: 21st Jan 2011 00:31
What i tried the "code" and nothing happend???

Oh well back to the problem now.

ALLAH IS THE GREATEST
May he forgive me
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 21st Jan 2011 00:59 Edited at: 21st Jan 2011 01:00

This is trying to delete the whole bullet vector(which you shouldn't/can't even do since you didn't use new in the first place), you want to delete what an item in that vector is pointing to.


Same thing. You need to specify which item/thing in the bullet array you're trying to access. There are a couple more instances of this same problem.

Also, when you call "makebullet", you don't seem to save the id and life info into the bullet struct you just made for that purpose.

I'd really look up documentation for the vector class as someone fixing your code again and again won't do much good.

I'm not sure if the linker errors are caused by these other problems, I would just fix them and see what happens though.
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 21st Jan 2011 07:17
Oh no the last post was about the code snippet box . It didn't work for some reason.

But those errors are generating and the objid() is not returning a good value.

ALLAH IS THE GREATEST
May he forgive me
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 21st Jan 2011 07:27
Can any one tell what is this saying?



It looks to me like it wants some other library.

This is the error i received when i compiled the above code with the line "delete bul[i]" was deleted . I think its improving a little bit.

Does anyone understand what i am trying to do.If this isn't the right way then plz tell me.

ALLAH IS THE GREATEST
May he forgive me
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 21st Jan 2011 07:30 Edited at: 21st Jan 2011 07:30
from visual studio menu, project -> <projectname> properties -> configuration properties -> C/C++ -> code generation -> change /MTd to /MT

Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 21st Jan 2011 09:25
Well i have tried the code but nothing is being created.I don't know what to do.

Can someone plz post a similar code for creating bullets(increasing and decreasing number of bullets) which is easy to understand and is efficient.

Some how i managed to get this working in darkbasic pro using the add to stack() command but i want this in darkGDK.

ALLAH IS THE GREATEST
May he forgive me
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 21st Jan 2011 14:13
The STD <vector> and <list> libraries are the best way to go. I found it much easier to wrap them up in an class.

Warning! May contain Nuts!
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 21st Jan 2011 14:44
Well ok i don't want to use a struct or a class . I want two vectors name bid and blife (b=bullet) .

How can i implement this.

Oh i need this fast , two days before my project.

ALLAH IS THE GREATEST
May he forgive me
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 21st Jan 2011 18:22
Umm a tutorial would be superb

ALLAH IS THE GREATEST
May he forgive me
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 21st Jan 2011 18:33
There's a link attached to this forum post to download:

http://forum.thegamecreators.com/?m=forum_view&t=180340&b=6

Although the bullet class uses simply an array to set it up ti does give an example of bullet handling and also collision detection using Sparkys Collision.



Warning! May contain Nuts!
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 23rd Jan 2011 12:10
Ok guys i have made the code that makes the bullet and it actually shows but after some time the program stops and hangs there (even my computer).....plzzzzzzzzz guys i have only 9 hours to go before i must pack my computer for the project exhibition.I will pray for you guys , plzzzzzz help me.

Heres the code



ALLAH IS THE GREATEST
May he forgive me
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 23rd Jan 2011 16:42
HELLOOOOOOOOO??????????????

Guys plzzzzzzzzzzz help me , i have got only 4 hours before pack up.
PLllllllllllllllllllZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

See my post above .Plzzzzzzzzzzzzzzzzzzz

ALLAH IS THE GREATEST
May he forgive me
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 23rd Jan 2011 17:04 Edited at: 23rd Jan 2011 17:27
maybe vector size going too big? how are you using the functions?
also, you need an "y--" after the deletion so all bullets are updated

Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 23rd Jan 2011 18:04
just by


Can you alter the code a little bit . now the same code is not even showing a single one and if i press space key it just freezes the program.

ALLAH IS THE GREATEST
May he forgive me
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 23rd Jan 2011 19:06 Edited at: 23rd Jan 2011 19:08
if (dbSpaceKey())
makebullet(player);

if the FPS is too hight, let's say 500, you will generate 500 bullets each second, which can be very heavy on both CPU and GPU, on the CPU because the vector grows too large, so the bullet update is slower and the vector can be too big that it can't handle itself
on the GPU because you have an object for each bullet, imagine 500 objects per second, each object has 8 or 36 vertices (depending on the way GDK creates them), if they are 8, so it's 500*8 per second, after some time that gotta be large

you probably don't want to keep shooting while space is down, or at least have some delay, so, for one bullet per click:



and for a timer:



another possibility which is the last i can think of is, findfreeobject() not ending, so like, you click space, it attempts to shoot a bullet but when it calls findfreeobject(), it goes into an infinite loop, check the function whether it hangs or not (using breakpoints is great for this purpose)

Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 23rd Jan 2011 19:46
Well i think so something is wrong with this:

But have you runned my code yet??

I think something is wrong with it.

ALLAH IS THE GREATEST
May he forgive me
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 23rd Jan 2011 21:04
Helloooo???????

ALLAH IS THE GREATEST
May he forgive me
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 23rd Jan 2011 22:35
PLzzzz guys help here plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz?

ALLAH IS THE GREATEST
May he forgive me
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 23rd Jan 2011 22:45
There's not much to go by with the bits of code you've put up here.

Warning! May contain Nuts!
Neotron
14
Years of Service
User Offline
Joined: 21st Jul 2010
Location: I am in an underworld , making a E.Army
Posted: 23rd Jan 2011 23:04
Hi guys , thanks for anyone who tried to help.I have found a way to do this without any errors (TGC+Internet ROCKSSSS).

Any way here's how i did it.(just as the above one but replace the functions).Oh and after tomorrow i am gonna write a tutorial on this INSHALLAH.



SOLVED

ALLAH IS THE GREATEST
May he forgive me

Login to post a reply

Server time is: 2024-11-19 14:32:19
Your offset time is: 2024-11-19 14:32:19