W
W
wlastas2021-08-18 21:57:58
C++ / C#
wlastas, 2021-08-18 21:57:58

Can it be written in one line?

public class Tasker :aTasker {
....
   protected override void MakeTask() {
         var leader =( (ui as UI).leader == null || (ui as UI).b_leader_dead || !(ui as UI).leader.isValid || ....  )? null : (ui as UI).leader.Owner; //так работает
//можно сделать в 3 строки, но это приводит к ошибкам в подобном аналогичном использовании - см. коммент ниже
var lui =ui as UI; 
var l = lui.leader;
 var leader = (l == null || lui.b_leader_dead || !l.isValid)? null : l.Owner;  
// можно ли сделать как то так:
     var leader => { var lui =ui as UI; var l = lui.leader; return (l == null || lui.b_leader_dead|| !l.isValid) ? null : l.Owner; }; 
    ....
    }
....
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
@
@insighter, 2021-08-19
@wlastas

var leader = new Func<Entity>(() => { var lui =ui as UI; var l = lui.leader; return (l == null || lui.b_leader_dead|| !l.isValid) ? null : l.Owner;  })();

V
Vasily Bannikov, 2021-08-18
@vabka

can be written like this:

if(ui is UI {leader: { IsValid: true, Owner: var leaderOwner},  b_leader_is_dead: false, } )
{
 // делаем что-то с владельцем лидера
}

But I don't know if pattern matching is available in Unity

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question