Optimizing code
Displaying 1-12 of 12 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Time_Wizard

I'm trying to figure out where most of the slowdown is coming from in my code, and I noticed that there's a whole lot of unnecessary looping going on, even though it's not doing any processing during it.

Basically, would this be a significant source of slowdown if most of the EnemyStructArray was empty?
void Enemies(){
for(x=0;x<100;x++){
switch(EnemyStructArray[x].ID){
case 0:
case 1: A;
Hundred;
Lines;
Of;
Code;
}
}
}


And if so, would this be faster?

void Enemies(){
for(x=0;x<100;x++){
switch(EnemyStructArray[x].ID){
case 0:
case 1: Enemy1AI(x);
}
}
}

void Enemy1AI(int EnemyStructArraySlot){
A;
Hundred;
Lines;
Of;
Code;
}

Posted on 2005-07-28 23:49:37

basil

It would not be any faster. In fact it would be slower, since it's performing the same lines plus a function call.
Your options are to reduce the number of times case 1 executes or take some code out of those hundred lines. If you provide the code in question I or somebody else should be able to help you out.

Posted on 2005-07-29 03:47:24

mcgrue

It would not, however, be a significant slowdown. I do recommend you pulling something like that in it's own function, just for your sanity's sake.

Posted on 2005-07-29 04:48:35

Jesse

If there are only 2 cases, an if() would be somewhat faster than a switch().

Posted on 2005-07-29 07:43:41

Overkill

TimeWizard: You could save a lot of time by keeping a count of how many enemies you have currently and doing something like:

void ProcessEnemies()
{
for (i = 0; i < enemy_count; i++)
{
switch(enemy[i].id)
{
case 1:
// Sneaky amateur ninja.
EnemyMove(i, MOVE_CAUTIOUS);
if (PlayerInEnemyRange(i))
{
EnemyAttackPlayer(i);
}
case 2:
// Cowardly Idiot.
EnemyMove(i, MOVE_RANDOM);
if (PlayerInEnemyRange(i))
{
EnemyEvadePlayer(i);
}
}
}
}


Note how I didn't include case 0, since I assume your case 0 does nothing. I'm making lots of assumptions, but hopefully good suggestions. Break the hundred lines of code into smaller functions that handle specific aspects of the enemy, especially if your enemies might share similarities with other enemies. This helps break down repetition, and it also makes one tweak to a function affect many enemies instead of having to redo each of them every time you want to change something to do with their movement and actions and etc.

Posted on 2005-07-29 13:20:36

Omni

How about do what console games do...and don't process every sprite at once. process only those onscreen, or wait till they reach a certain distance of the screen to start processing them.

Posted on 2005-07-29 17:55:47

mcgrue

Oh, is this action-game AI, not RPG?

Posted on 2005-07-31 09:01:27

Overkill

I'm guessing. I don't *think* an RPG would typically require a hundred lines of code per enemy type. If they do, the code would probably be some deal inefficient! Time_Wizard never specified, I assumed it is an action game though, since I don't really see the need for that much individuality in enemies typically in an RPG.

Posted on 2005-07-31 23:33:11

Time_Wizard

Yeah, it's a platformer.

Posted on 2005-08-03 22:57:17

CrazyAznGamer

Quote:Originally posted by Time_Wizard

Yeah, it's a platformer.

A platformer, I like. =)

Posted on 2005-08-03 23:08:45

Time_Wizard

A Castlevania game to be specific. In the unlikely event Konami actually cares and I get crap about it, I'll just call it Transylpalace or some BS like that.

Posted on 2005-08-04 01:02:32

CrazyAznGamer

Quote:Originally posted by Time_Wizard

A Castlevania game to be specific. In the unlikely event Konami actually cares and I get crap about it, I'll just call it Transylpalace or some BS like that.

!!!!!!!!
Castlevania!!!! I really like. =D

Posted on 2005-08-04 13:12:01


Displaying 1-12 of 12 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.