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 / 'For' loop question

Author
Message
Aldur
17
Years of Service
User Offline
Joined: 8th Oct 2007
Location: Melbourne, Australia
Posted: 8th Aug 2009 17:23
Hey guys,
Having another problem, which I know will have a simple fix but it's late and I just can't seem to work it out.
I have a group of plains as billboards which I need to face the camera at all times, the code I have at the moment works, but only for one loop.
How can I get the for loop to continuously rotate these objects towards the camera?

Here is my current code:


Thanks,
Jamie.
Juggalo Memnoch
16
Years of Service
User Offline
Joined: 22nd Apr 2008
Location:
Posted: 8th Aug 2009 17:39
All you have to do is just put the For Loop in your games Main Loop
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 8th Aug 2009 20:57
you also don't need the extra int i; line. You can just do:


Use Google first... it's not rocket surgery!
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 8th Aug 2009 21:10
well, just a suggestion: don't use alot of 'for' loops in your main loop, because they slow the game.

heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 8th Aug 2009 21:35
I'm curious, if you don't use for loops to update objects then what do you use Hassan?

Use Google first... it's not rocket surgery!
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 8th Aug 2009 22:10
i said dont use "alot of" for loops

heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 8th Aug 2009 22:46
Ahh ok, my bad

Use Google first... it's not rocket surgery!
Funkbrotha10
15
Years of Service
User Offline
Joined: 2nd May 2009
Location:
Posted: 8th Aug 2009 22:51
yeah try to avoid funky nested loops. instead use 'if' statments inside of your main loop. an 'if' inside of a loop also acts as a loop because it executed so quickly

it was once said that a million monkeys at a million keyboards could re-produce the works of shakespear...
Now thanks to the internet we know that that is not true
Aldur
17
Years of Service
User Offline
Joined: 8th Oct 2007
Location: Melbourne, Australia
Posted: 9th Aug 2009 03:53
Thanks for the quick replies,
I already had the for loop inside my games main loop yet it only executes once.
I am going to play around a bit with variables to see if I can get it to repeat each game loop.
Anymore suggestions?
Thanks again all!
Jamie.
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 9th Aug 2009 05:08
Quote: "don't use alot of 'for' loops in your main loop, because they slow the game."


Yes, instead he should use the magic way to updating a list of entities, besides, if his loop is of a literal size then the compiler may unwind it depending on its length and/or if you enable that optimization. Also they don't inherently slow anything down much more than any other statement, and it's obvious that doing anything a lot of times = slowdown so what's your point?

Quote: "yeah try to avoid funky nested loops."


Why? Certain things are best done with nested loops, like... drawing something to an image, doing that with 1 loop just makes your code hard to read and most likely slower if you want to actually know the current x/y location.

Quote: "instead use 'if' statments inside of your main loop"


How do you hope to update 90 billboard objects using an if statement?

Quote: "an 'if' inside of a loop also acts as a loop because it executed so quickly"


Then incrementing an integer acts 'as a' loop because it too is fast. Assuming you fill the conditional part of the for loop, then you can do everything an if statement can do anyway, but with more features! And at no extra speed loss as they do the same thing if you just have: for ( ; poo > 5; ){} vs if ( poo > 5 ).

Amnzero
15
Years of Service
User Offline
Joined: 1st Aug 2009
Location:
Posted: 9th Aug 2009 05:18 Edited at: 9th Aug 2009 05:21
Psudo:


if(enemy == Amnzero) runAway();
Amnzero->WebSite = L"http://neovance.com/";
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 9th Aug 2009 06:34
Thank you Dark Coder, it's good to see that I wasn't the only one questioning some of the previous posts

Use Google first... it's not rocket surgery!
Amnzero
15
Years of Service
User Offline
Joined: 1st Aug 2009
Location:
Posted: 9th Aug 2009 11:29
Lol @ Heyufool. I don't even pay attention anymore. A little while ago I read a thread where it actually took 6 people and 20 posts to figure out how to make a block of code execute if the mouse position was inside a cordinate range...I just chuckle and shake my head a lot these days.

if(enemy == Amnzero) runAway();
Amnzero->WebSite = L"http://neovance.com/";
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 9th Aug 2009 13:54
Updating baddies using a for loop is not a problem since you probably wont have more than 100 or 200, but if you use for loops alot and each one of them is like "for (int i = 0; i <= 1000;i++)", the game will be really slow since it will execute thousands of lines in every single loop.

about using if instead of for, it also works and without slowing the game much, here's the point:

while ( loopGDK() )
{
for (int i = 0; i < 20000; i++)
{
dbPointObject(i, 0,0,0);
}
dbSync();
}

that will point objects 20000 times in every loop.

int i = 0;
while (loopGDK())
{
if (i < 20000)
{
dbPointObject(i, 0,0,0);
i++;
}
else
i = 0
dbSync();
}
that will point objects once every loop, one by one,and since the loop is fast, it will act as a for loop, like what funkbrotha said

so if you use the first example the loop will be slower.

Amnzero
15
Years of Service
User Offline
Joined: 1st Aug 2009
Location:
Posted: 9th Aug 2009 14:20
Uhm.. What are you doing that you have that many objects on screen? Besides updating them that way would look like crap for this application. You cant update them one by one each time the program loops. Your objects would not keep up with camera movement assuming that the camera was moving constantly. The updates would be jerky, at best.

if(enemy == Amnzero) runAway();
Amnzero->WebSite = L"http://neovance.com/";
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 9th Aug 2009 14:23
Hassan, your example is fine as long as you're willing to wait 5 and a half minutes for everything to update.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 9th Aug 2009 14:36
5 mins? hell no...its done in few seconds, besides, it was an example, dbPointObject came on my mind so i used it, you probably wont have 20000 objects or else the game wouldnt even load...at least before some hours

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 9th Aug 2009 14:39
Quote: "5 mins? hell no...its done in few seconds"


Sure, at a frame rate of 60 you'll be doing 60 objects per second, and 20000/60 is 333 seconds, which is 5 and a half minutes.

And I realise it was just an example, I was just pointing it out.
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 9th Aug 2009 14:41
If you need everything updated every loop then you have to update everything every loop, if you dont then you can kind of do what Hassan is saying and split your 100 objects into groups of 20 and update over 5 frames.
But that was not what the op was asking so why are we talking about it.
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 9th Aug 2009 14:45
Quote: "so if you use the first example the loop will be slower."


So what you're saying is that running a statement many times is slower than running it just once?

In my foliage library I did a similar thing except in my examples I updated 100 items a frame, but that was because updating the visible LOD of thousands of trees can get quite slow if you move rather fast through the vegetation.

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 9th Aug 2009 15:15
reading one book takes less time than reading two books, simple as that

Amnzero
15
Years of Service
User Offline
Joined: 1st Aug 2009
Location:
Posted: 9th Aug 2009 15:19
Not if you read both at the same time...

if(enemy == Amnzero) runAway();
Amnzero->WebSite = L"http://neovance.com/";
Aldur
17
Years of Service
User Offline
Joined: 8th Oct 2007
Location: Melbourne, Australia
Posted: 9th Aug 2009 15:24
Hey guys,
Finally found what I was doing wrong!
It wasn't my for loop that wasn't working, for some reason it would update instanced objects so I changed them to clones.
Thanks all for your help anyway
Jamie.
Mista Wilson
16
Years of Service
User Offline
Joined: 27th Aug 2008
Location: Brisbane, Australia
Posted: 9th Aug 2009 16:58
Quote: "It wasn't my for loop that wasn't working, for some reason it would update instanced objects so I changed them to clones"


That will be because when you make a instance of an object in GDK, I believe the instance shares most of it's properties with it's parent object where changing the parent will change all the instances of that parent, whereas clones are individual objects in their own right, just "copied" from another object.

If it ain't broke.... DONT FIX IT !!!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 9th Aug 2009 20:20
Quote: "Not if you read both at the same time..."


Then you end up cock-eyed.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Amnzero
15
Years of Service
User Offline
Joined: 1st Aug 2009
Location:
Posted: 9th Aug 2009 20:23
Quote: "Then you end up cock-eyed."


If your human. Computers are multitaskers!

if(enemy == Amnzero) runAway();
Amnzero->WebSite = L"http://neovance.com/";
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 9th Aug 2009 20:31
Quote: "If your human. Computers are multitaskers!"


Only if they're multi-core and even then there are limits. Otherwise, computers may seem to do multiple things at one time but it's still a matter of having the CPU divide it's attention to each task in time slices.

Can you guarantee that your computer is actually using all it's cores on your program? Are you threading your program to make the must use of them? Can you even thread a for loop? Does one thread have to wait until another thread is complete to make use of what it's done?

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Amnzero
15
Years of Service
User Offline
Joined: 1st Aug 2009
Location:
Posted: 9th Aug 2009 22:49
Actually I was just looking into this today. I was looking at Intel TBB. You should google it. Also for single core systems that have a newer Nvidia GPU I was looking at Nvidia Cuda which allows you to use the GPU processor like a second(or higher depending on how many cores you already have)processor. Interesting stuff.

Even an asyncronous call on a single processor is faster than straight code. Though it does eat up resources and can cause the main code to slow down a little. In the long run it is benificial.

Quote: "Can you guarantee that your computer is actually using all it's cores on your program?"


Assuming you know how many cores to support and how to set the affinity for them...

Quote: "Are you threading your program to make the must use of them?"


Well I guess that would be answered differently by everyone. I personally have not incorperated it into a game yet. Oh wait yes I did. I used a seperate thread in a texas holdem game to calculate hand strength..probability of victory and stuff. I had to because the library I used to calculate the information was pretty slow. and I did not want the few second pause in the game.. I moved it to another thread and allowed that information to be calculated while the hand continued to be played.

Quote: "Can you even thread a for loop?"


I think you can thread just a for loop using OpenMD(never tried it myself, just have documentation to go off of, and even that is pretty vague), but other than that I think you have to have the loop inside a function call for most other threading applications.

Quote: "Does one thread have to wait until another thread is complete to make use of what it's done?"


Technically speaking yes, but at the same time no. Threading is a complicated subject to even idealize sometimes. Most of the time you would make an asyncronous call to a function and continue on through your program. Once the function completed you would have a callback to do any post processing for the thread. Native thread mainainance is a very complex ordeal. For the application of updating the way a lot of objects faced in a scene I think this would be a good use for another thread because I think you can make the db function calls from within that outside thread. and the way the GDK is put together you dont have to worry about complex operations like passing images, or other non thread safe data between them.

But I could be wrong I have yet to actually try using multithreading with the GDK. At the moment it is just fun to think about.

if(enemy == Amnzero) runAway();
Amnzero->WebSite = L"http://neovance.com/";

Login to post a reply

Server time is: 2024-11-19 03:08:20
Your offset time is: 2024-11-19 03:08:20