My game is becoming increasingly complex, making the overview more difficult.
That is why I wondered what the best way is to keep my scripts organized.
For example, should I place everything from the enemy AI in one script or do I have to create each function in a separate script (which makes communication between scripts more difficult) …
Also, as a general tip, don’t have scripts call each other directly. Instead, use fire and on to send/receive events. The Flappy Bird game, for example, uses this approach:
Yes, you can do that. You can have the declaration of a script type in one script, and several of its methods in one or more other files.
You just need to make sure the other files are loaded after the script file that has the declaration of the script type (in your projects settings, script loading order tab).
The filename can be anything, it doesn’t matter how it’s called.
All Javascript is being appended to the same site so it’s “connected” automatically.
For example here is your enemy.js initial script:
var Enemy = pc.createScript('enemy');
// initialize code called once per entity
Enemy.prototype.initialize = function() {
};
// update code called every frame
Enemy.prototype.update = function(dt) {
};
And here is another script file named enemy-movement-more.js, where you can remove the initial code Playcanvas puts in and you can add methods extending the enemy.js script:
If you would like more info on why you should use one over the other search for OOP programming paradigms, especially talks from Alan Kay (he could be the father of what we are talking here).
Yeah, exactly. My preference is the event fire/on style. It’s just because I believe decoupling scripts makes your code more robust (say if you add/remove scripts at runtime via the Editor).
Hello @Leonidas, It looks like this is not longer working after I use the parse button. The main enemy script can no longer find the functions. Any idea how I can fix this?
I use the method above to order my functions in diffrent scripts. But after i used the parse button of the script where i removed the initials it don’t work anymore. I don’t think its because midnamed because the problem is with all functions right now.
I found the problem. My scripts with no initials where to far after the main script in the loading order. So they where not yet loaded while the main script already need them.