Jump to content
  • Advertisement
ggenije

How to program smarter??

Recommended Posts

59 minutes ago, ggenije said:

So why call a millions of unnecessary if statements?

If you want to avoid this completely the only way is to have one array for small minions and a second for large minions. So you have two Update functions too, each looping only over the appropriate enemy type and no others.

Share this post


Link to post
Share on other sites
Advertisement

Like this?

public class Enemy
{
    float health;
	float attack;
	float speed;
	void Attack(){}...
}
public class Minion : MonoBehaviour
{
  Enemy enemy;
    void Update()
    {
		MinionWork...
    }
}
public class Boss : MonoBehaviour
{
  	Enemy enemy;
    void Update()
    {
		BossWork...
    }
}

 

Edited by ggenije

Share this post


Link to post
Share on other sites
16 minutes ago, ggenije said:

Like this?


public class Enemy
{
    float health;
	float attack;
	float speed;
	void Attack(){}...
}
public class Minion : MonoBehaviour
{
  Enemy enemy;
    void Update()
    {
		MinionWork...
    }
}
public class Boss : MonoBehaviour
{
  	Enemy enemy;
    void Update()
    {
		BossWork...
    }
}

 

Again It's been a while for me with C# but assuming MonoBehavior is either a class with a virtual Update, or an interface, something like that could work. Then you can just keep an array of MonoBehavior objects and call Update on them all. You can also subclass for specific kinds of Minion and Bosses if you need to.

Share this post


Link to post
Share on other sites
26 minutes ago, ggenije said:

Like this?

Yes. 

But eventually since now both classes have an Enemy, there might be a more elegant way to handle this using the ECS system Unity is built upon. But i have no experience here... anyone?

What seems wrong to me is the Attack function now being unaware if being a boss or a minion. 

There likely is a better way, probably obvious to people more familiar with Unity, ECS and gameplay code than me...

 

 

Share this post


Link to post
Share on other sites

He can easily move Attack and/or other functions to MonoBehavior and rename it to MultiBehavior, or alternatively create more interfaces. There are a lot of options.

Share this post


Link to post
Share on other sites
46 minutes ago, Gnollrunner said:

I was more speaking in terms of general program options. Any C#/Unity specific stuff I'll defer to you on.

Haha, don't defer anything to me!

I only found that link while trying to understand and put into context, what you were saying. ;) 

Share this post


Link to post
Share on other sites
28 minutes ago, Septopus said:

Haha, don't defer anything to me!

I only found that link while trying to understand and put into context, what you were saying. ;) 

Still...You program in C# Unity every day. I program in C++ DX11 everyday, haven't programed in C# in years, and never used Unity. I'm sure you know some things I don't.🤔

 

Share this post


Link to post
Share on other sites

If you want in-depth help with this situation you might need to supply some screen shots of the editor, what your attachments are to game objects, and some sample code (as mentioned).

You said you want your minions to all act the same. What you have to do is have to do is create the game object, attach scripts, and create a prefab. Then they will all act according to the code attached. Can I ask something? What is the minor different thing the minions do? Do they all do this minor different thing at a random time, or just some of them do it? If just some of them do it, you could create two prefabs of the same enemy. One with that behavior and one without. If you want that code on all of them you're going to have to make a class variable to determine if they are doing it.

Minion - will walk about randomly, tap his foot on the ground if no one is in ear shot, have a seat.

If you want to say a minion doesn't do one of those things you could have class member that is an array of behaviors. 

array behaviors['walk about', 'tap foot impatiently', 'have a seat on ground'];

When you instantiate the objects you could just leave one out. And your inner code would look in the array for behaviors and see if the condition is right.

EDIT: Sorry, I didn't see the end 6 posts. I didn't know you had elaborated.

Edited by EeksGames

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!