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 / I think the game isn't Sync'ing

Author
Message
vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 14th Dec 2011 21:00 Edited at: 14th Dec 2011 21:02
Hey guys!

Im making a game with a friend just for fun.

Its a Tower Defense game, where you need to protect a place from the enemys adding towers and stuff.

Ok what im doing now it's the AI for the enemys, so im kinda making a path so the enemies can try to reach the player's tower.

I got the enemy1 struct:



The bools for paths are just a thing that i tested and they didnt work so forget it.

Now the Functions of the enemy:

Load:


Spawn:


Move:


Ok so here's the main function :



Well i dont see what's wrong but what really happens is that the enemy stays on the x:0 and y:0 ... and no movement.

I already did it once and it worked but i dont remmember what i changed :S


Hope you guys can help me

PS: If you need more info just ask
Oh and i forgot that i know its not sync'ing because i had a X and Y displayers and they are getting the new ones above the oldest..

CREDITS:
GetFreeSpriteID() Function was provided by Hassan (i think so, was a long time ago)

C++ Medium 3.5/5
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
14
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 15th Dec 2011 05:09 Edited at: 15th Dec 2011 05:12
try these:
-in LoadEnemy, take one more parameter which is the sprite id, and in there, save it in "id" element of your struct
-remove the "int id" parameters from the other functions because the struct already knows it, use the element copy
-try dbCLS ( ) at the beginning of the loop
-after making LoadEnemy save the id, don't pass to it like "enemy1.LoadEnemy ( enemy1.id, .... );", because id is not even initialized yet, and even if it was, that doesn't make sense, give a real id, and while we're at it, it really should be called "Load" instead
-image_id = GetFreeSpriteID();, read that again ^^, you will probably want a new "GetFreeImageID();", and when you "Load", you will probably do "enemy1.Load ( GetFreeSpriteID ( ), ... );"

vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 15th Dec 2011 15:20 Edited at: 15th Dec 2011 15:21
well, the problem is, i want to use the same image for various enemies... i have no idea how to do that since im gonna create a id for the enemy1...

The point is that i want to use the enemy1 struct for all the enemies with that image, the id can be random but assigned at the spawn and deleted when it dies.

jUST One thing: I want to create another enemy with the same image, how to do it?
EDIT:


C++ Medium 3.5/5
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
14
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 15th Dec 2011 17:47
you could use the "static" keyword:


note that you can also generate a random sprite id and save it in Load function so you won't have to pass any IDs to it at all, this could be better

vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 15th Dec 2011 18:58
well the actual problem is:

How can i make a serious AI so in the right time a right function is activated? ...

I think for the enemy1 i can make a for loop so with 1 move function they all move independently but the actual problem is when they die i will make a deleteSprite function but the struct function knows already a value for the SpriteID.

This seems harder than i thought :S

C++ Medium 3.5/5
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
14
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 15th Dec 2011 20:02
you will probably not declare enemies one by 1 in your code, and you wont have independent variables for each, the thing is using a vector, here's the whole thing:

create your enemies in a loop, by an enemy i mean a copy of an "Enemy1" struct, and add each one of these to a vector using vecName.push_back ( enemy1 );

in the game loop, loop through the vector elements and update each one of them the way you want (you should probably have Enemy1::Update ( ... ); that should be implementing AI or something))

sprites that are not called by dbSprite every loop are not drawn right? alright then all it needs to destroy an enemy is to get it out of the vector, yes, when it's out, it will no longer be updated every loop and thus is destroyed (this only applies to your approach, different implementations requires different actions), so basically, you should also have this in the "updating" loop: check if your bullet(s) or whatever weapon is colliding with the enemy, if so, vecName.erase ( vecName.begin ( ) + i ) (names are obvious right?)

you can also use vectors to have multiple bullets shooting at the same time, it's the exact same way, have a struct representing a bullet, add it in vector to update so it's being shot, remove it when it hits something, update bullets vector in loop

basically that can solve most of your game mechanics you should just pick the suitable struct/class representation of the object, have a vector of it, and you're done!

thinking of writing a sample template with simple polymorphism/inheritance showing how powerful they can be to deal with such things

vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 15th Dec 2011 21:12
Well, i dont know how to work with vectors :S

But that way is like an array of enemies1 right? like a bunch and each one are represented for the vector name.

The Update function is a great idea for my game =D

Another thing, could you make a example with my code?

PS: If you could please go to msn so we can talk better, and sorry for the time you are taking :S

C++ Medium 3.5/5
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
14
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 16th Dec 2011 08:06 Edited at: 16th Dec 2011 08:07
you don't need to fully understand vectors, here's all you need:
vector<Enemy1 /*TYPE*/> enemyV; /*NAME*/
this is probably a global variable

to add enemies:
enemyV.push_back ( enemy1 /*your copy(object) of Enemy1 struct*/ );

to remove an enemy:
enemyV.erase ( enemyV.begin ( ) + i ); //i is the index

that's how you update:

and if you want collision detection, efficiently (along with the update):


vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 17th Dec 2011 16:32
ok so first:

#include <vector>

Then inside the main function:


then what to do?

I dont get is how can i know whats the enemy thats alive or being hit ... thats my problem, the indentification. =D

Sorry for all the trouble hassan ..

C++ Medium 3.5/5
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
14
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 17th Dec 2011 16:47 Edited at: 17th Dec 2011 16:48
i explained it pretty well, you pull it out of the vector with vector::erase and it's considered as dead, you add it in with vector::push_back and it's alive and running, that's all about it
you didn't push_back your enemy above
now next, you will more or less copy-pase the code from my previous post, nothing too complicated
don't forget the dbCLS ( ) at the beginning of the loop, and don't forget "using std::vector" or "using namespace std"

vitinho444
13
Years of Service
User Offline
Joined: 12th Oct 2010
Location:
Posted: 17th Dec 2011 17:34 Edited at: 17th Dec 2011 17:44
hey hassan

So.. i did everything you said..

The struct of the enemy1 should stay like this?:

so it can be an array right?

but how i put that things of the index and stuff.. ? (i know what it is but dont know where to put)

Im terrible sorry for making you lose your time


@EDIT
Hassan sorry for being stupid but i just found that if i do:

Enemy1 enemy2; it works as another enemy (im so stupid..) Real sorry man

Thanks for everything you are doing, you are the best really..

C++ Medium 3.5/5
VB6 Advanced: 4/5
VB.NET Advanced: 4/5
Hassan
14
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 17th Dec 2011 17:44 Edited at: 17th Dec 2011 17:53
i don't get what you're trying to ask
here check this out:


90% of that was copy-pasted, fixed some tiny mistakes (like the vector declaration in your previous post..)
now note that since you're not using pointers, changing(using) enemy1 (the OBJECT not the TYPE) after calling enemyV.push_back will not be of any use, you must change it through the vector, since this is just stupid, you will have each Enemy1 (TYPE not OBJECT) manage itself by itself, so, have 3 functions, like Init, Update and Destroy or Cleanup are basically all you need, and those 3 are the only things that can alter an "enemy", you can have functions like "DealDamage (float dmg)" so that for example, in a previous post i made, i showed you some simple bullet mechanic, you could use it so that when a bullet hit and enemy, don't erase it, call DealDamage instead, and then check if it's alive with another function like "bool IsAlive ( void ) const" and erase when this returns false
if that's all complicated forget about it and just stick to the basic idea and do the rest yourself

also, check this for a possible Update/Load functions bodies:


Login to post a reply

Server time is: 2024-04-26 04:53:31
Your offset time is: 2024-04-26 04:53:31