vendredi 2 août 2019

How can use void functions (different parameters) call in trinary operator or write if-else in 1 line

I am using unity2019.2.0b with c# langauge.

When i create object and I change properties of object via script like scale rotation position or velocity

In my case is working fine with if-else statement, but I want to use trinary operator because each properties setting per 1 line script for improved readability.

So my question is how can i convert if else statement to trinary operator which doesn't have return value functions

The concept what I want is to check movable is true or not

  • if true then call function1(parameter1)

  • if false then call function2(parameter1)

or

  • if true then call function3(parameter1)

  • if false then call function4(parameter1, parameter2)

all functions are no return value functions.

I want to write like this (movable is boolean)

movable? SetObjectMoving(gameobject) : RemoveComponent<MoveComponent>(gameobject);

this is code when use if-else statement

if(movable)
{
ObjectMovingSystem(gameobject);
}else{
RemoveComponent<MoveComponent>(gameobject);
}

And this is my full code

    private void CreateEntity()
    {

        for (int i = 0; i < spawnCountPerFrame; i++)
        {
            Entity instance = shapeFactory.GetEntityRandom();
            entityManager.SetComponentData<Translation>(instance, new Translation { Value = SpawnZoneOfLevel.SpawnPoint });
            entityManager.SetComponentData<Rotation>(instance, new Rotation { Value = Random.rotation });
            entityManager.SetComponentData<Scale>(instance, new Scale { Value = Random.Range(0.1f, 1f) });
            //(movable ? SetEntityMovement(instance) : RemoveMovementComponent<PersistantObjectMovement>(instance));

            //PersistantObjectmovement
            if (movable)
            {
                SetEntityMovement(instance);
            }
            else
            {
                RemoveMovementComponent<PersistantObjectMovement>(instance);
            }

            //(rotatable ? SetEntityRotation(instance) : RemoveMovementComponent<PersistantObjectRotation>(instance));
            //PersistantObjectRotation
            if (rotatable)
            {
                SetEntityRotation(instance);
            }
            else
            {
                RemoveMovementComponent<PersistantObjectRotation>(instance);
            }


            entityList.Add(instance);
        }
    }


    private void SetEntityMovement(Entity instance)
    {
        entityManager.SetComponentData(instance, new PersistantObjectMovement
        {
            direction = Random.onUnitSphere,
            speed = Random.Range(0.5f, 1)
        });
    }
    private void SetEntityRotation(Entity instance)
    {
        entityManager.SetComponentData(instance, new PersistantObjectRotation
        {
            angularVelocity = Random.onUnitSphere * Random.Range(0f, 90f),
            radiantPerSecond = math.radians(Random.Range(120f, 360f))
        });
    }

    private void RemoveMovementComponent<T>(Entity instance) where T : IComponentData
    {
        entityManager.RemoveComponent(instance, typeof(T));
    }

I know this is not really necessary to do, but i want to know there is anyway to do if-else statement write 1 line like trinary operator?

Aucun commentaire:

Enregistrer un commentaire