Reputation: 53
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 ternary operator because each properties setting per 1 line script for improved readability.
So my question is how can i convert if else statement to ternary 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 ternary operator?
Upvotes: 1
Views: 699
Reputation: 53
@steve16351 gave good idea for me.
Use ternary operator with 1 line what i want it.
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 ? (Action<Entity>)SetEntityMovement : RemoveMovementComponent<PersistantObjectMovement>)(instance);
(rotatable ? (Action<Entity>)SetEntityRotation : RemoveMovementComponent<PersistantObjectRotation>)(instance);
How about you guys think with this code?
Upvotes: 1
Reputation: 77
If you really want to do it you can use:
(movable ? (Action<bool>)SetEntityMovement : RemoveMovementComponent<PersistantObjectMovement>)(instance)
Upvotes: 0
Reputation: 62
I think you mean ternary operator not trinary operator.
Ternary operators are there to return a value, if you are not useing them to return a value you are abusing them. In otherwords if someone reading your code sees a ternary operator he can expect the operation to return a value.
If you use the ternary operator instead of an if statement just to conditionally execute code you are making the code less readable not more readable.
The reason this does not work is because the ternary operator needs to know the return type to be able to compile. It does this by looking at the return types of the clauses following the '?'. void methods do not have a return type and so compilation of the ternary operator will fail attempting to get the return type.
Upvotes: 0
Reputation: 5389
I guess this qualifies :P
if (movable) SetEntityMovement(instance); else RemoveMovementComponent<PersistantObjectMovement>(instance);
I don't think there's a ternary style operator for this. I would probably just go with the normal if-else with braces.
Upvotes: 0