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.

Newcomers DBPro Corner / Controlling bullets, arrays?

Author
Message
BowserYo
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location:
Posted: 12th Sep 2009 06:03 Edited at: 12th Sep 2009 06:07
Hello all. I'm having trouble with shooting bullets right now, here is my problem:

When my player shoots a bullet, all goes well, it moves they way I want and stops when it hits a object. Btw, I use the "exclude object on" command when it hits an object, is that a good Idea?

My code is (in theory) is this:



Now my problem is, if I shoot more than one bullet it doesn't move the last one it created... I'm new to arrays and I don't understand how I would do this. Should I use arrays or is there a better way?

I'm not sure if the "for b = 1 to max_bullets" is the best way as I may have created other objects as well... I'm lost.

Thanks a lot!

ßõw§€r¥¤
z_man
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: Australia
Posted: 12th Sep 2009 07:44
Generally, you should avoid using actual objects with very fast moving projectiles (like bullets). Try using ray casting instead.

I just wrote something on a very similar note about 10 minutes ago:
http://forum.thegamecreators.com/?m=forum_view&t=157650&b=7&msg=1852261#m1852261

If you really want to use objects though, just say so and I'll try to help you out.

Z

Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 12th Sep 2009 14:09
z_man is right. For fast projectiles where you're not supposed to "see" the projectile, you should use ray casting.

However, to continue on your problem, if you're having a projectile that is supposed to be seen (like a rocket launcher) you have to use arrays. There's no other way around it because you usually need more information than just the object's rotation and position (like some sort of life, damage, acceleration, etc.)

First you create a custom type with all the data that you might need. An example would be


Then you create an array that will contain the data of all the rockets in the game

I chose size 0 because there is no rocket yet.

Upon creating a rocket, you add it to this array by using

or if you want to insert every new rocket at the end of the list:


After that, when you want to manipulate the rocket, you use a normal for-next loop.


And that should be all for controlling all your rockets.
The method I described has the disadvantage that adding and removing elements from an array is pretty slow. But it has the advantage that the only limit on the number of rockets in your game is the number of rockets you can load in your memory.

Sven B

BowserYo
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location:
Posted: 12th Sep 2009 17:15
Thank you guys for the help!

I've never used ray casting before. What if there's a line of enemies and you shoot once wouldn't that hit them all?

A quick question on arrays:

I just learned that they existed through an AI tutorial I took. He uses (in theory) "enemies=10 : for e = 1 to enemies", now does this select object 1 to enemies (or 10)? Or just arrays 1 to enemies...

I'm a bit confused on this since he also creates the player as make object cube 1,10. Wouldn't that conflict with "for e = 1 to enemies"?

He also does some type of off set when creating the objects:



Anyway, I'll look into ray casting. Thanks for the suggestions and help guys!

ßõw§€r¥¤
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 12th Sep 2009 19:28 Edited at: 12th Sep 2009 19:29
Quote: "now does this select object 1 to enemies (or 10)? Or just arrays 1 to enemies..."


A for loop isn't really "selecting", though you can see it that way in just a few cases. The code in for loops is executed a number of times that depend on the start and end value. A chosen variable will be increased by a step value (=1 by default) once the code has been executed.

A simple example would be


By "selecting" you might think it's done all at the same time, but it really isn't.

Because we can have such a variable that is increased every loop, we can easily reduce the number of lines used. Take the above example: without a for loop this would be



Quote: "I'm a bit confused on this since he also creates the player as make object cube 1,10. Wouldn't that conflict with "for e = 1 to enemies"?"


Quote: "He also does some type of off set when creating the objects"


Because the variable in the for loop (in his case this variable is e) ranges from 1 to enemies, it doesn't necessarily mean that his object numbers also range from 1 to enemies. In his case, they range from 11 to 10 + enemies. So he can easily solve this by just adding 10 to e when passing an object number.

Login to post a reply

Server time is: 2024-11-24 02:04:46
Your offset time is: 2024-11-24 02:04:46