PixelPest wrote:S1eth wrote:PixelPest wrote:
The code with onTick() and onNPCKill() was just an example of one of the many things you can do with it. Although it's an interesting idea, I think I'll keep it the way I have it due to the other two parameters (they're set to false by default which is why I didn't use them in the example). Thanks for the suggestion though
http://hastebin.com/idujiwihad.lua
You can thank me later.
Um. Okay. Why should I use this though? It's about twice as long as my code. I'm not saying no thank you, I'm just wondering why this is so much better even though it's much less concise
There are a number of reasons.
1. It can be used in levels like this:
Code: Select all
local galaxyComets = API.load("galaxyComets");
galaxyComets.daredevil.activate();
And this is your code, for comparison
Code: Select all
local galaxyComets = API.load("galaxyComets");
local var = true;
function onTick()
if var then
galaxyComets.daredevilRun();
else
galaxyComets.daredevilRun(false);
end
end
Your code is a total disaster. If you don't want your daredevilRun to be active, don't call it.
And don't name your important variable "var". Your parents didn't name you "human" when you were born.
2.
It's not full of bugs and unintended side effects like your code is.
You had this line of code:
Code: Select all
registerEvent(galaxyComets, "onTick", "daredevilRun", "purpleCoinsRun", false);
It is pretty clear that you do not understand what registerEvent does.
Your function has 5 arguments, but registerEvent only allows for four arguments.
Your last argument is ignored.
And "purpleCoinsRun" is NOT registered.
Instead, you are using "purpleCoinsRun" in place of false, but the string evaluates to TRUE, which means that your registered event "daredevilRun" will be execated every tick before the onTick() function.
Secondly, this code calls daredevilRun() automatically every tick with no arguments.
And in addition to that, you wrote some code that calls this function A SECOND TIME every frame.
Even if you have var = false, you call it once as if it were true, and a second time as if it were false.
That's just not how coding works.
On top of that, you have a "firstTick" part, but you don't turn it off after the first tick.
And when it is turned off, you forgot to turn it back on when you activate the run a second time.
3.
Readability and maintainability.
Multiple small well-named functions are better than one big function.
I wrote functions for activating and deactivating your run, which sets up all varaibles for the user.
We only execute code when we need to. We don't mute the audio every single frame even though it is already muted, for example.
4.
Maybe studying this will make you learn how to properly write code.
Maybe you will finally look up how registerEvent works.
Maybe you will stop using snippets of code you don't understand.
Maybe