Skip to main content

Loading Scenes — Use sahneYukle(ad, veri) to switch the running game or 3D scene. Learn why basla() must come first and how the sahne event is published.

Game engine API · Lesson 3

Loading Scenes

Use sahneYukle(ad, veri) to switch the running game or 3D scene. Learn why basla() must come first and how the sahne event is published.

sahneYukle only works on a running session. It forwards the new scene name to the active controller, updates the state and publishes the sahne event.

What you will learn

  • The sahneYukle(ad, veri) signature
  • Switching 2D games and 3D scenes
  • Reading error cases

API signatures

CallResult
basla(mod, config){ok, hata?} · validates mode and config
sahneYukle(ad, veri){ok, hata?} · needs a running session
durum()state copy · mutation has no effect
olay(ad, fn)function that cancels the subscription

A running session is required

If durumNesnesi.calisiyor is false, sahneYukle returns ok:false with a Turkish error message asking you to call basla() first. An empty scene name is rejected with another error message.

A controller returning ok:false is also forwarded as an error.

Safe scene switching

var M = window.OyunMotoru;
var alan = document.getElementById('oyunAlani2D');
M.basla('2d', { kok: alan, oyun: 'labirent' });
var r = M.sahneYukle('yakalama', {
  hedef: 5,
  sure: 20000,
  tohum: 7
});
if (!r.ok) console.log('Could not load:', r.hata);
console.log(M.durum().sahne); // 'yakalama'

What happens in this code? The 2D controller passes the data object straight to Oyunlar2D.kur(ad, config). The hedef, sure and tohum fields here are real config fields of the catch game.

Switching a 3D scene

In 3D mode the scene name must be one of the Oyun3D registry ids: geometri, renkler, gunes-sistemi, sekiller, molecule (molekul), colour cube (renk-kupu), spectrum (tayf) or cell (hucre). Unknown names are rejected by dogrula().

Listen for the scene event

var M = window.OyunMotoru;
var alan = document.getElementById('oyunAlani3D');
M.olay('sahne', function (e) {
  console.log('New scene:', e.ad);
});
M.basla('3d', { kok: alan, oyun: 'geometri', genislik: 480, yukseklik: 320 });
M.sahneYukle('gunes-sistemi', {});

What happens in this code? On success the core publishes the sahne event with {ad: name}. The 3D controller may publish its own sahne event as well.

Common mistakes

Wrong approachResultCorrect approach
Calling sahneYukle() before basla()ok:false and an error messageCall basla() first
Empty scene nameThe name is rejected with ok:falseSupply a valid id
Unknown 3D scenedogrula() rejects itUse one of the eight registered ids
Passing null as dataThe engine substitutes {}Passing an object is safer

Try it yourself

Start the maze, switch to the hafiza game with sahneYukle(), and confirm that durum().sahne shows two different names in sequence.