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.

DarkBASIC Professional Discussion / dynamic arrays problem

Author
Message
zootan
16
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 31st Oct 2011 12:55
Hi , I need to make a list of objects(numbers) which share a common factor within the main loop of my program and then choose a random object from the list.
To explain further, my program checks distances between camera and objects ,i want to add all the object numbers of any object within a certain distance from the camera , this can change as the camera moves so the array needs to be created and ammended on the fly .
thanks for any help .
Braude Interactive
19
Years of Service
User Offline
Joined: 1st Aug 2006
Location: Sheffield, UK
Posted: 31st Oct 2011 13:55 Edited at: 31st Oct 2011 13:56
Not sure what you mean by 'a common factor'.

What you need to do isn't too tricky by how I understand it. In your loop fill the array with all the objects nearer than dist#.




If your question was about how to find out the distance I can help you with that too.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 31st Oct 2011 19:37
You can make it dynamic but adding/deleting elements would probably take too much time for what you want. The better way is to have a set array size of something like 100 and clear the array every time the user moves and a new check is done to fill the empty array. That way your game won't be bogged down by creating an array with 2000 objects and checking every millisecond to see if the objects are no longer in the view just because the user moves by a few pixels.

Braude Interactive
19
Years of Service
User Offline
Joined: 1st Aug 2006
Location: Sheffield, UK
Posted: 31st Oct 2011 19:51 Edited at: 31st Oct 2011 19:59
Someone showed me a really quick way of doing a distance check once using Vector3. I don't quite remember what it was, but it was useful.

Obviously you can just do it with pythagoras, but I remember (I think it was VanB) saying that using vector3 made it quicker.

But as I say, I don't really understand how it worked. I'll see if I can't find the post.

Edit:
Ah, it was my old forum account. I can't even log into that anymore! Anyway, sorry to give false credit, it was Fallout who helped me before (not VanB, they used to have such similar avatars I got them confused with Matt Rock as well) but anyway, here's the link:

http://forum.thegamecreators.com/?m=forum_view&t=92301&b=1

Check the third post down by fallout for a really simple bit of code to find the nearest object. He puts it in a nice tidy function for you as well.
zootan
16
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 31st Oct 2011 21:20 Edited at: 31st Oct 2011 21:30
hehe thanks but its not the actual distance part im stuck on, just how to make a dynamic array, how do i write it down so it contains only objects i chose add the list is only as long as there are things in it. Its a very simple thing , how do i say .. make array(x) where x is the total number of objects in the list . for some time the objects will only be 2 , but sometimes 200 depending on the dist .
i want my main loop to build the array as the program runs , not have a fixed array , but the method of just having a cleared array may work though its not exactly how i meant .
i know how to build a normal fixed array , i just dont know how to make it dynamic( as in it can change its size and number of elements as needed inside the main loop.
by common factor i mean some shared aspect, in this instance its when the distance is less than x .
i have the code running when a timer = 0 so it only triggers the dist part once every second or 2 .
thanks again
something like array() = array() to array() , i cant guess it.
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 31st Oct 2011 22:13
Do you mean something like this:



Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Nateholio
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: I\'ve Been Everywhere
Posted: 31st Oct 2011 22:17
Declare your data types for the array (if needed)


Then define an EMPTY array


Now to add an entry to the array


Processing array entries


Deleting an array entry (if index is already known)

If only data value is known


Emptying the contents of the entire array


In Development: K96 - Combat Simulation
Braude Interactive
19
Years of Service
User Offline
Joined: 1st Aug 2006
Location: Sheffield, UK
Posted: 1st Nov 2011 14:46
coouldn't you just flag the array location as active or inactive? That would save a lot of hassle fiddling about with deleting things.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 1st Nov 2011 15:27
Deleting from an array is relatively fast. Expanding an array is slow.

A better technique is to use an inuse/free-list. Everything at the front of the array is inuse, everything at the back of the array is free, and you keep track of the point where they meet.

(Typed in directly, not checked):


When you are done with the array, it's best to clear it down as with this very basic implementation the array will only ever grow larger.

zootan
16
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 1st Nov 2011 17:32 Edited at: 1st Nov 2011 17:36
Thanks for the replys guys!
im going to play about and see which one of u is right

btw this is for the medialess space-epic which is hidden away in the 2d section of the forum . All codes will be avalable to crit or improve in there , it just happened ater i asked a 2d question with vanB , now were colaberating on a medialess extravagansa. Check it out .http://forum.thegamecreators.com/?m=forum_view&t=190266&b=4&msg=2268769#m2268769

This problem is for the space traffic bit , i want all planets within xdist to become random spawn/destination points for the traffic .

Its...its aliiive!
spacekey to change planet .
so i want the yellow and green planets to be on the list , the blue planets taken off the list.
I will change the distance formula in my main project too , the vector way is much nicer ty.
zootan
16
Years of Service
User Offline
Joined: 12th Apr 2010
Location:
Posted: 1st Nov 2011 22:08
Quote: "coouldn't you just flag the array location as active or inactive? That would save a lot of hassle fiddling about with deleting things. "

no , when the destination or spawn point is chosen it is by the means of rnd(object numbers) so if the chosen object number has no flag my program will have an error or it will waste an opportunity to spawn , also dest and spawn are generated seperatly so the dest could be ok but then the spawn planet could be wrong (or viceverca)
this could be worked arround by making it reselect everytime the flag is not present but it would be more efficient if it could only choose between planets that are "right" .
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 2nd Nov 2011 08:33
Quote: "when the destination or spawn point is chosen it is by the means of rnd(object numbers) so if the chosen object number has no flag my program will have an error or it will waste an opportunity to spawn"


You could put your RND() command in a REPEAT/UNTIL loop to check for anything you want... be it a flag or planet class.



Braude Interactive
19
Years of Service
User Offline
Joined: 1st Aug 2006
Location: Sheffield, UK
Posted: 2nd Nov 2011 13:22
@ grog, yeah, that's how I'd have done it. In fact, how I do it, but mine's just to add new objects in a level loading sequence. I guess this is different, I just thought it was a similar thing.

Login to post a reply

Server time is: 2026-07-10 23:13:29
Your offset time is: 2026-07-10 23:13:29