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 / Emptying an array real time efficiently

Author
Message
Quel
17
Years of Service
User Offline
Joined: 13th Mar 2009
Location:
Posted: 22nd Mar 2011 17:30
Is there a one command solution for changing all contents in an array to 0?

Because i need to reset it every frame, and going through it (it is a big array, 64*64) takes up a lot of loops thus slowing down my game.

I'm aware of the Empty Array command, but that for some silly reason isn't for emptying, but rather destroying an array for no future use. (why would anybody do that?)

I need the indexes intact, just giving back 0 when called.

Anybody?
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Mar 2011 17:38
IanM's command "Clear Array" should empty the contents of an array...

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 22nd Mar 2011 19:09
Yep, and it's fairly fast.

However, if your data can be arranged properly, you don't necessarily need to clear your array at all - you can 'version' it instead.

The way that works is you have a global version value that is incremented every time you would normally clear the array, and an extra version field within the array's UDT.

When writing to the array, you would update the array entry's version with the global version. When reading from the array, you compare the global version against the array entry's version, and if different pass back default values, or if the same, pass back the values in the array itself.

That's it.

There is a break-even point though, where the extra work reading/writing can eventually take longer than the crearing of the array, but you'll have to test for that

Jimmy
22
Years of Service
User Offline
Joined: 20th Aug 2003
Location: Back in the USA
Posted: 23rd Mar 2011 02:38
Also, if you don't have the Matrix1 plugins, you can always use UNDIM to delete the array, then use DIM again to re-init an empty array. That's old school, though!

NaGaFailMo.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 23rd Mar 2011 02:50
Quote: " but that for some silly reason isn't for emptying, but rather destroying an array for no future use. (why would anybody do that?)"

I find it useful.

I can't see why you'd need to reset all values to 0 every frame. I imagine there's a better way to control your data than whatever your current method is.

The Internet: Where men are men, women are men, and children are federal agents
Quel
17
Years of Service
User Offline
Joined: 13th Mar 2009
Location:
Posted: 23rd Mar 2011 10:37
I'm very sorry to say that, but...

GOD damn OF COURSE the typical "why would you do that?" kinda message has finally popped up...

I was wondering where it is, but i was right, it WAS going to appear, I'm so happy.

If there is a question, and you know an answer, you let the person know. Otherwise, if the question and its possible answers interest you, you stay quiet, and read the thread. If non, you stay away.

You DON'T come with the 'WHY WOULD YOU' question, it is stupid.

Without any intention to offend, but i must say, that those kind of questions, Dark Basic or not, ARE stupid, and useless. Don't do it anymore, everybody.

You may be curious, and ask... but the style 'i dont say something useful because why would you?!' is unacceptable.

I so wanted to say this once, but so far i only saw this going on with other's threads. What took you so long?

---

Anyways, isn't undim-ing and re-dim-ing a real slow method?

Phaelax, how and for what do you use 'Empty array'?
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 23rd Mar 2011 10:54
Quote: "Without any intention to offend, but i must say, that those kind of questions, Dark Basic or not, ARE stupid, and useless. Don't do it anymore, everybody."

Slight over reaction there Quel...

It's a perfectly valid question. It does seem like resetting an entire array every loop is unusual, perhaps he just was interested why you were doing it? I know I am.

The question "Why?" is not stupid or useless in most circumstances. It's how we learn. Perhaps you don't have a good reason for doing it but that's no reason to flame a respected "Silver Codemaster"

Please respect other forum users.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Mar 2011 15:24
@Quel,
Sometimes the fastest way to do something (in this case, clear an array), is to do something else instead that avoids the problem in the first place.

So sometimes, people ask what you are trying to do and why. Now they may not say it using those words, but really, who cares how they say it as long as they aren't being rude about it.

The thing you should have done at that point is to explain the what & why, and just move on, not throw a tantrum - you've probably lost yourself a knowledgeable helper there because we don't throw awards like 'Silver Codemaster' at just anyone.

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 23rd Mar 2011 16:43
Quote: "Because i need to reset it every frame, and going through it (it is a big array, 64*64) takes up a lot of loops thus slowing down my game."


Well, why not show us what your current routine looks like ? How much time is it actually using ?

(Quick hack)

So assuming you've something like this


Then depending what you're using it for you could express this as a 1d array also.



Which just means mult'ing the Y coordinate to write into as 2d structure.

Although, arrays have a bit of overhead, so a mem block or allocated chunk of memory could potentially be preferable.

(pseudo code)


TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 23rd Mar 2011 16:52
The question "why do it like that" is probably among the most useful questions out there actually... It helps show the people asking the question why you did it like that so they can learn, but can also point out flaws in the design of the person you're asking. I'm also curious to why you need to clear an entire array every loop, won't it be possible to somehow just overwrite the current content without resetting?

TheComet

Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 23rd Mar 2011 17:46
@ Quel:

Yeah, that's how we make code more efficient by asking ourselves "Is there a better way to do this.". There may be a better way you can't see but it can only be found if you tell everybody why you need to clear the array constantly.

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 23rd Mar 2011 18:39 Edited at: 23rd Mar 2011 20:32
Quote: "You DON'T come with the 'WHY WOULD YOU' question, it is stupid."


Yes I do and no it's not stupid. Many of the problems people have on this forum is because they think of one way of solving a problem, and then immediately close their mind to alternatives. Pixel perfect collision is a good example of this. 99% of the time you can get more accurate results thousands of times faster using a vector based system such as the one in box2d or a home-grown system, and yet people immediately think that pixel perfect collision is the only way to get accurate collision data.

Similarly, you've come here asking how to clear an array fast enough to be used every loop. I guarantee you that if you tell me what you are actually trying to acheive I can suggest a different method. Alternatively you can continue being narrow minded and getting angry at people trying to help you.

[b]
BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 27th Mar 2011 13:33 Edited at: 27th Mar 2011 13:42
If you will set up your array to have an 'active' or 'in use' array element, then all you will need to reset is that one element in each row of a 2-dimensional array to tell if the data is valid to use.

(or)
Use a variable as a pointer into the array, and reset the pointer to the top of the array each loop, then increment the pointer as you fill the array each time.

Asking what your reasoning is for zeroing the array each loop (not a good idea) really is a GOOD question to ask, so we can find the BEST way (or cheat) to get the job done.

Login to post a reply

Server time is: 2026-07-17 21:00:44
Your offset time is: 2026-07-17 21:00:44