Flutter’da print ve debugPrint

 

print ve debugPrint ile test

Merhaba,

Flutter ile ilgili do’s & dont’s postlarına bakarken karşılaştığım bir konu vardı: print ve debugPrint. print ile test etmek, aşama aşama nerede olduğumu görmek için program yazarken breakpoint koymak kadar çok kullanıyorum console ekranını. Ancak print ile ilerlersem “Don’t invoke ‘print’ in production code” uyarısı ile karşılaşıyorum. Aynı satırı debugPrint ile yazarsam bu uyarı kayboluyor. O yüzden bugün bu ikisinin farkını, birinde neden uyarı verdiğini, diğerinde vermediğini araştırmak istedim. Şimdi ikisini de detaylıca inceleyelim.

print Nedir?

print Dart dilinin standart çıktı fonksiyonu. Terminalde veya konsolda belirtilen metni yazdırmayı sağlıyor.

void main() {
print("Bu bir print fonksiyonu örneğidir.");
}

Dezavantajı nedir?

  • Birinci dezavantajı uzun metinler veya çok fazla satır çıktısı yazdırmak istediğimizde konsolda “output truncated” (çıktı kesildi) hatası verebiliyor (daha önce başıma geldi, debugPrint ile onu araştırdığımda karşılaşmıştım). Yani, çok fazla veriyi aynı anda yazdırırken problem yaşayabiliyoruz.
  • İkinci konu da aslında birinci ile bağlantılı. Çok sayıda veya büyük veri çıktıları almak istediğimizde performans kaybı oluşabiliyor. Yani performans açısından daha az verimli.

debugPrint Nedir?

debugPrint Flutter uygulamaları için optimize edilmiş, Flutter çerçevesine özel bir fonksiyondur. Aynı şekilde konsola metni yazdırıyor ancak uzun metinlerin bölünmesini ve kesilmesini engelliyor.

void main() {
debugPrint("Bu bir debugPrint fonksiyonu örneğidir.");
}

Avantajı nedir?

  • debugPrint fonksiyonu, metinlerin satır sınırlarını kontrol edip gerektiğinde çıktıyı parçalara bölüyor. Bu sayede, uzun metinler düzgün şekilde kesilmeden yazdırılıyor.
  • Bu sayede büyük verileri yazdırırken debugPrint daha performanslı ve uygulamanın performansını olumsuz etkilememiş oluyor.
  • debugPrint çıktıyı varsayılan olarak 800 karakterle sınırlıyor. Ancak daha uzun çıktılar için debugPrint'in “wrapWidth” parametresini kullanabiliriz.

wrapWidth Kullanımı

debugPrint fonksiyonunun wrapWidth parametresi, uzun metinlerin nasıl sarılacağını (wrap) kontrol etmemizi sağlar. Burada sarmak, her bir satırın kaç karakterden sonra bölüneceğini belirlemek anlamına geliyor.

String longText = 'Bu çok uzun bir metin örneğidir. ' * 20;  //Bu metni arka
//arkaya 20 kez yazdırmak demek.

// wrapWidth olmadan debugPrint kullanımı (standart olan)
debugPrint("wrapWidth olmadan:", wrapWidth: 800);
debugPrint(longText);

// wrapWidth parametresi ile debugPrint kullanımı (50. karakterden sonra bölünmesi))
debugPrint("\nwrapWidth ile (50 karakter):", wrapWidth: 800);
debugPrint(longText, wrapWidth: 50);
  • wrapWidth OlmadandebugPrint varsayılan sarma genişliğiyle (800 karakter) kullanılır.
  • wrapWidth ile (50 Karakter)wrapWidth parametresi 50 karakter olarak ayarlanır, böylece her satır 50 karakterden sonra bölünür.

Aşağıda bu kodların çıktısını göreceğiz: metinler belirlenen karakter sayısından sonra yeni satıra geçiyor olacak. Bu, uzun log mesajlarını daha okunabilir hale getirmek için özellikle yararlı bir kullanım.

wrapWidth olmadan:
Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir. Bu çok uzun bir metin örneğidir.

wrapWidth ile (50 karakter):
Bu çok uzun bir metin örneğidir.Bu çok uzun bir
metin örneğidir.Bu çok uzun bir metin örneğidir.Bu
çok uzun bir metin örneğidir.Bu çok uzun bir metin
örneğidir.Bu çok uzun bir metin örneğidir.Bu çok
uzun bir metin örneğidir.Bu çok uzun bir metin
örneğidir.Bu çok uzun bir metin örneğidir.Bu çok
uzun bir metin örneğidir.Bu çok uzun bir metin
örneğidir.Bu çok uzun bir metin örneğidir.Bu çok
uzun bir metin örneğidir.Bu çok uzun bir metin
örneğidir.Bu çok uzun bir metin örneğidir.Bu çok
uzun bir metin örneğidir.Bu çok uzun bir metin
örneğidir.Bu çok uzun bir metin örneğidir.Bu çok
uzun bir metin örneğidir.Bu çok uzun bir metin
örneğidir.

Hangi Durumda Hangisi Kullanılmalı?

  • Kısa ve Basit Çıktılar: Basit ve kısa çıktılar için print’i halen kullanabiliriz (sarı uyarıdan rahatsız olmuyorsak).
  • Uzun ve Karmaşık Çıktılar: Uzun metinler veya çok fazla veri yazdırmak gerektiğinde debugPrint’i tercih edilmek daha akıllıca olur.
  • Genel Kullanım: Yine de Flutter projelerinde hata ayıklama yaparken genellikle debugPrint kullanmak daha uygun ve güvenilir.

Gördüğümüz gibi uygulamada yapacağımız ufak değişiklikler performansta ve kullanımda büyük farklar yaratabiliyor. Ben araştırmamı yaptıktan sonra artık tüm testlerimde print yerine debugPrint kullanmaya karar verdim, sarı uyarıdan da kurtuldum. Umarım faydalı olmuştur.

Teşekkürler.

Selin.

Rigidbody and Collider Concepts in Unity

 

Hello everyone,

Today, I wanted to explain two concepts that should be learnt in the first place when starting Unity to new game development friends like me. After opening an empty project and adding that famous cube (in all the tutorials I saw, the teaching started by adding a cube :), the first thing to do is usually adding Rigidbody to the cube. So let’s start with this.

What is Rigidbody?

What is Rigidbody? Let’s examine this under this title. Rigidbody is a component used in Unity to give an object physical behaviour. Adding a Rigidbody to an object means allowing that object to be managed by the physics engine. So what is a physics engine? The physics engine simulates the movement and interaction of game objects in the physical world. This ensures that the object has physical properties such as mass, gravity, speed, force, and moves in accordance with the laws of physics. It also allows realistic motion and collision calculations. So it is very necessary for games.

What are the Basic Properties of Rigidbody?

Let’s examine the basic properties that appear in the inspector window when we add Rigidbody.

Rigidbody Inspector
  • Mass: Determines the mass of the object. Mass affects the resistance of the object against forces. Since the value we will write here means the mass of the object, objects with higher mass require more force.
  • Drag (Friction): It is the resistance force against the movement of the object. The higher the value we write here, the slower the object moves.
  • Angular Drag (Angular Friction): It is the resistance force against the rotational movement of the object.
  • Use Gravity: Enabling this option will cause the object to be under the influence of gravity, and if it is high up, it will fall downwards when the game is started. If there is no object to stop it when it falls, it will disappear from the game scene, but its presence will remain in the game system.
  • Is Kinematic: When this option is enabled, the object is not affected by the physics engine, but can move when it hits other objects. Kinematic objects are usually controlled programmatically.

What can we do using rigidbody?

Under this heading, let’s examine what we can do using the rigidbody feature.

  • Moving objects by applying force and torque. Thanks to this application, objects can fall faster, roll when falling or different reactions can occur.
  • Letting objects free fall. An object positioned higher than the ground can fall downwards when the game is started and stop when it hits the ground, or if the ground does not have a collider feature, it can pass through the ground and continue travelling in space.
  • Setting objects to react physically after collision. Post-collision disappearance function or any animation and effect can be added.

What is Collider?

Another feature that has been used in Unity since the beginning is collider. It is a component used to determine the presence of an object in the physical world. It can be called like a 3D frame placed around it. Adding Collider to an object allows that object to react to collisions. The Collider component can be of various types depending on the shape of the object. Now let’s look at which types it can be according to its shape.

What are Collider Types?

  • Box Collider: It is used for rectangular prism-shaped objects. It has a simple box shape.
  • Sphere Collider: Used for sphere-shaped objects.
  • Capsule Collider: Used for objects in the shape of a capsule (two hemispheres and a cylinder).
  • Mesh Collider: Used for complex shaped objects. It creates the collision area using the mesh structure of the object. Generally suitable for static (stationary) objects.
  • Terrain Collider: Used for terrain objects.

What are the Basic Properties of Collider?

Let’s examine the basic properties that appear in the inspector window when we add the Collider.

Collider Inspector
  • Is Trigger: When this option is enabled, Collider acts as a trigger instead of a collision. It initiates trigger events instead of physically interacting with other objects.
  • Material: A Physic Material is used to add physical properties to the Collider. This determines properties such as friction and flexibility.

What can we do using Collider?

Under this heading, let’s examine what we can do using the collider feature.

  • We can make objects collide with each other and give physical reactions. For example, when there is a collision, the objects go back separately or an animation like an explosion effect.
  • Enabling objects to initiate trigger events when they enter certain areas. For example, making an object disappear when it enters the collider area of another object.

Since we know both components separately, let’s finally examine how they can be used together.

Using Rigidbody and Collider Together

Adding both Rigidbody and Collider to an object allows us to fully manage the object’s interactions in the physical world. Since this management is a very important part of the game development process, learning these two components can make us a better game developer to begin with.

Rigidbody manages the physical movements and forces of the object, while Collider manages the collisions and trigger events of the object with other objects.

For example, let’s take a ball dropping and rolling on the ground in a game scene:

    • By adding a Rigidbody to the ball, we can ensure that it remains under the influence of gravity and moves with physical forces, i.e. rolls realistically.
  • At the same time, by adding a Sphere Collider to the ball, we can manage its collisions with other objects and add an effect or other triggered event.

In this way, the Rigidbody and Collider components work together to realistically simulate the physical behaviour and interactions of the object.

In this article, I tried to explain the importance of Rigidbody and Collider by talking about what it is. I hope it was useful.

Good coding to everyone.

Selin.