Game engine API · Lesson 11
Registering a Custom Module
Register your own controller in the 2d or 3d slot with modulKaydet(ad, denetleyici). Learn the kur, duraklat, devam, durdur and sahneYukle contract.
modulKaydet is the single entry point for adding a third-party module. Invalid names or controllers without kur are rejected.
What you will learn
- The modulKaydet signature and accepted names
- The controller contract
- The risk of overwriting a built-in module
API signatures
| Call | Result |
|---|---|
modulKaydet(ad, denetleyici) | {ok, hata?} · kur is required |
basla(mod, config) | {ok, hata?} · validates mode and config |
kayitliModuller() | array of module names |
The controller contract
The engine requires the controller to have a kur(kok, config, api) method. The api object carries olay, yayinla, puanAta and azalt helpers.
duraklat, devam, durdur, sahneYukle and oyunlar are called defensively; a missing method does not throw, but the related behaviour does not happen.
The smallest valid controller
var M = window.OyunMotoru;
var denetleyici = {
kur: function (kok, cfg, api) {
kok.textContent = 'Custom module · ' + (cfg.oyun || 'none');
api.yayinla('basla', { mod: 'ozel' });
return { ok: true, oyun: cfg.oyun || 'none' };
},
duraklat: function () { return { ok: true }; },
devam: function () { return { ok: true }; },
durdur: function () { return { ok: true }; },
sahneYukle: function (ad) { return { ok: true }; },
oyunlar: function () { return [{ id: 'none', ad: 'None', aciklama: '' }]; }
};
console.log(M.modulKaydet('3d', denetleyici)); // {ok:true}What happens in this code? The MOD_ADLARI table accepts 2, 3, 2d and 3d; they resolve to the 2d or 3d keys respectively.
Overwrite risk and validation
Registering a second time under the same key silently replaces the old controller. Avoid overwriting the built-in 3d module except on a disposable page.
kayitliModuller() returns only keys; the registry is unchanged when registration fails.
Use the module
var M = window.OyunMotoru;
var oncesi = M.kayitliModuller().length;
console.log(M.modulKaydet('3d', { kur: function () {} }).ok); // false
console.log(M.kayitliModuller().length === oncesi); // true
// After a valid registration:
M.basla('3d', {
kok: document.getElementById('oyunAlani3D'),
oyun: 'none'
});
console.log(M.durum().mod); // '3d'What happens in this code? A failed registration never touches the registry; moduller[ana] is assigned only when denetleyici.kur is a function.
Common mistakes
| Wrong approach | Result | Correct approach |
|---|---|---|
| Controller without kur | modulKaydet() returns false | Add the kur function |
| Unknown module name | Registration is rejected | Use 2d or 3d |
| Overwriting the 3d slot | Built-in scenes disappear | Experiment on a separate page |
| Writing to the host with innerHTML | CSP violation | Use textContent |
Try it yourself
Register the controller above and confirm the text "Custom module · none" appears after basla(). Then remove kur and confirm the registration is rejected.