Public Group

How to program smarter??

Recommended Posts

My question:

For example I have class enemy, let's say I have 100 enemy types.

Now in Update()(each frame)

if(enemyType==Type1)
{doWhat Type1 does}
else if(enemyType==Type2)
{doWhat Type2 does}
else if(enemyType==Type3)
{doWhat Type3 does}
...
else if(enemyType==Type100)
{doWhat Type100 does}


so in each frame I can call up to 100*enemy count

so is there any way to avoid this , to tell it earlier what update should it do

I am working in unity, but is there universal way or?

Share on other sites

What you're talking about is efficiency. To get better you have to find all the long code segments that can become smaller segments. It takes time, but the key is knowing the language. The more you know about the language the more efficient you will become. In game dev if you are starting as a coder, the last place you might want to start is with a game. If you're using C# study C#. Learn about classes and objects, you really need them. Learning web development opened my eyes too. Learning data bases with mysql carries over nicely to sqlite (embedded data base for software.) Learning the web stuff opened my eyes on how to update software via downloads (Unity asset bundles.)

To get rid of your jumble of if statements you want to make prefabs of your bad guys. Make a bad guy, attach scripts to it, and make that a prefab. Then you are controlling one bad guy at a time. And you want to do prefabs of all your bad guy types. It's like programming behaviors. When you drop the prefab in the scene they will all behave according to the attached code. I'm kind of a newbie too, so research prefabs. That will get the ball rolling a bit. And remember what I said about learning the language in general, and taking a peek at web programming. Game dev is a long haul. For someone like me, I had to settle for text game for now, because a graphical game will not happen without funding, and a couple of smart coders.

Edited by EeksGames

Share on other sites
17 minutes ago, EeksGames said:

What you're talking about is efficiency. To get better you have to find all the long code segments that can become smaller segments. It takes time, but the key is knowing the language. The more you know about the language the more efficient you will become. In game dev if you are starting as a coder, the last place you might want to start is with a game. If you're using C# study C#. Learn about classes and objects, you really need them. Learning web development opened my eyes too. Learning data bases with mysql carries over nicely to sqlite (embedded data base for software.) Learning the web stuff opened my eyes on how to update software via downloads (Unity asset bundles.)

To get rid of your jumble of if statements you want to make prefabs of your bad guys. Make a bad guy, attach scripts to it, and make that a prefab. Then you are controlling one bad guy at a time. And you want to do prefabs of all your bad guy types. It's like programming behaviors. When you drop the prefab in the scene they will all behave according to the attached code. I'm kind of a newbie too, so research prefabs. That will get the ball rolling a bit. And remember what I said about learning the language in general, and taking a peek at web programming. Game dev is a long haul. For someone like me, I had to settle for text game for now, because a graphical game will not happen without funding, and a couple of smart coders.

My problem isn't that I have many if statements,but to do it only once, not in every update.

Share on other sites
33 minutes ago, ggenije said:

so is there any way to avoid this , to tell it earlier what update should it do

The first thing to try is probably to make behavior data driven, not code driven.

So all the ifs process similar code with some branches, but what happens exactly is driven by data that can be tweaked by a game designer (non programmer) with editing a text file, for instance containing the number of damage a certain type of enemy does.

As a bonus chances increase you can reuse the code in a later game where only the data needs to be adapted.

Share on other sites

If you really want to just do it once you need to wrap it in another if statement with a flag bool. Maybe someone can suggest something better.

if(doingtask)
{

// process here

}

You need to find a constructive way to trip taskdone on and off, while watching out for making it flicker on and off in your update.

You could also have an array of string flags.

Edited by EeksGames

Share on other sites

Wouldn't a switch over the enemy type work? At least it beats sequentially testing so many values. The compiler will likely convert it to a dispatch table, so it jumps directly to the right piece of code.

Share on other sites

Also supply samples of your actual code, so people can see exactly what you're doing. I think you'll get better tailored support with code and an explanation. It's hard for people if they have no clue what you're trying to do.

Share on other sites

In your specific example it should take one check rather than 100.  There are a number of ways to handle this situation given the language you are using and your preference. I program in C++ so this will be the basis for my answer

Imagine each enemy type can be considered a number: 1 to 100. Now it's simple enough to set up a table of function (pointers) for your different behaviors. You index the table by your enemy type, call the behavior, and you are done. In C++ and similar languages you can also set up 100 little subclasses that all over ride a virtual for specific behaviors. Likewise this will more or less give you a single dispatch, so the performance will be good, and it should work in C# also. Probably the table dispatch will also work.

Edited by Gnollrunner

Share on other sites

What I am doing is next:

with class Enemy there is type Boss and non Boss(minions)    i use same model for minions and bosses , just scaled.

99.99% of enemies are minions and they behave the same (expect some minor things that happens once in few seconds)

but 0,001% are boss that behave differently.

So why call a millions of unnecessary if statements?

I know that this statements they do not bother too much , but what is there  a lot of different types/behaviors .

Also different bosses doing different things.

Share on other sites
20 minutes ago, ggenije said:

What I am doing is next:

with class Enemy there is type Boss and non Boss(minions)    i use same model for minions and bosses , just scaled.

99.99% of enemies are minions and they behave the same (expect some minor things that happens once in few seconds)

but 0,001% are boss that behave differently.

So why call a millions of unnecessary if statements?

I know that this statements they do not bother too much , but what is there  a lot of different types/behaviors .

Also different bosses doing different things.

I can't decern what your exact requirements are, however IMO much of what you are trying to do can be handled with basic polymorphism (virtual functions and overrides) like I aluded to at the end of my first reply. You should be able to dump the vast majority of your ifs.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 11
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633718
• Total Posts
3013522
×