Indie Dev

Hello Guest!. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, sell your games, upload content, as well as connect with other members through your own private inbox!

Help with creating plugin commands

Run

Towns Guard
Xy$
0.00
So I'm fairly new to scripting, and I just started on a second plugin when I noticed that it collided with my first plugin.
It seems that I have implemented plugin commands incorrectly and caused them to call each other, which causes the stack size to be exceeded.
Here's the code I used for creating the command. It's the same for both plugins, except with a different string for the command.
var aliasPluginCommand = Game_Interpreter.prototype.pluginCommand;

Game_Interpreter.prototype.pluginCommand = function(command, args){

aliasPluginCommand.call(this,command, args);
if(command == "some_command"){
console.log("performing command");
}
}

I understand that the likely problem is aliasPluginCommand.call(this,command, args); from plugin1 is calling the pluginCommand function for plugin2, which is in turn calling it in plugin1 again. Or something at least similar to that.
I have never used aliasing before so I'm unsure if I understand it completely.

Any assistance on a better way to add the commands to the plugins would be appreciated.
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
What's the other plugin look like? It should not conflict unless you use the same name for the alias. Change the alias name and things should be good to go. I would also recommend using dev namespace for your alias variables instead. So example
Code:
LTN.scriptinitials_oldclass_oldfunction = oldclass.prototype.oldfunction;
 

Run

Towns Guard
Xy$
0.00
Thank you, changing the alias name fixed it, I was unaware that they couldn't share the same name.
Onward goes the scripting!
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
Yea, when you create a variable outside the function, you are putting it into the global scope, meaning its located with all global variables from all scripts. Glad I could help :)
 

Run

Towns Guard
Xy$
0.00
I guess that means I should follow the convention of using the dev namespace for all global variables?
Or is it possible to make them only accessible in the script which declares them?
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
Honestly, I'm not sure. I believe anonymous functions may help out but I have yet to use them or get a full understanding of them. I believe they keep things away from the global scope. I would recommend using the dev namespace for all plugins, they are super easy and they make things really organised. You create the dev namespace at the top of script lime this
Code:
Var LTN = LTN | {}
Now you can store all your aiases within this var like so.
Code:
LTN.Eu_oldclass_oldfunc = oldclass.prototype.oldfunc
Sorry I'm on my mobile phone so I can't get into much detail and there may be spelling mistakes . I hope this helps, I would recommend taking a look at other plugins to see how things are done.
 

Run

Towns Guard
Xy$
0.00
I'm assuming that's how javascript creates objects?
It at least seems that way.
From what I know of using anonymous code in other languages, it wouldn't be any different than placing it in local scope, in which case may defeat the purpose in some cases.
I'll research it, as well as check out some other plugins.
Thanks for the help!
 

eivl

Local Hero
Xy$
0.00
I guess that means I should follow the convention of using the dev namespace for all global variables?
Or is it possible to make them only accessible in the script which declares them?
If you need to have a variable global, like if you are creating a set of multiple plugins then yes it is reasonable to use your own namespace.
I use my own namespace because i want to easily debug my variables during development, but it is not needed for any single plugin.
You can self invoke a function by doing this
JavaScript:
(function(){
// some code…
})();
Using extra braces like before function keyword is simply coding convention and is used even by famous javascript libraries such as jQuery and also recommended by Douglas Crockford.
 
Top