How to program smarter??

Started by
19 comments, last by EeksGames 5 years, 4 months ago
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.

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...
    }
}

 

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.

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...

 

 

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.

Here's a page that describes what @Gnollrunner is trying to say, directly in Unity specific terms. (I think) ;)

https://blogs.unity3d.com/2015/12/23/1k-update-calls/

6 minutes ago, Septopus said:

Here's a page that describes what @Gnollrunner is trying to say, directly in Unity specific terms. (I think) ;)

https://blogs.unity3d.com/2015/12/23/1k-update-calls/

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

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. ;) 

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.?

 

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.

Eeks Games is currently working on Other Realms, an immersive RPG experience for die hard fans.

https://www.gamedev.net/projects/1003-other-realms/

 

This topic is closed to new replies.

Advertisement