Hello, I am currently working on an MM2-style knife projectile system that involves the use of perks and special kill effects. What is the best and most efficient way to script this on a large scale efficiently?
I did already create a version of it, but I decided to restart it as it turned out to be a load of spaghetti code that couldn’t be easily expanded upon.
Use OOP if you havent, it really helps you on organization and expansion.
Break down your problems into smaller problems, use diagrams even to get an understanding on what you need done.
When i say make it as organized as possible, i mean it. It will really help you out. Organize your code into folders, use module scripts to use them as many times as you need instead of copy paste, put comments, make as many functions as possible.
Would you consider “self” necessary to OOP, I’ve always put myself off from learning it.
also in my example would it make sense to use “Classes” or is that not something that would be useful here?
Yes and no.
In your case, you mentioned knife mm2 game with perks and special effects.
You can always do this without OOP, but that means:
If you plan to add many knifes and you want to put a code, you have to copy and paste instead of using module script
What about perks? It would be benificial if they were in a module script and automate it.
Conclusion: You can do this without OOP, however as a programmer you should look into it because it will come in handy if for example, you want to use the same script 100 times to different scripts.
Well its not hard, there are many guides out there, at first you will be confused but with time and practice you will understand.
Self is very important, when you use the keyword self, you tell the script to the class itself, not the module
Lets say we have a class person.
And then we have a Method
function Person.new()
return setmetatable({
self.Name = "John"},Person)
end
function Person:SayHi()
print(self.Name," Said hi!")
end
This syntax may not be right because im on phone but this will be our example.
If we use our code to create the new class using .new, and then we use the method sayhi, then it will print “John Said hi!”
Thats because the value john is saved within the class, using self.
If we didnt use self, then the module script wouldnt know his name, because its saved in the metatable and lua access that and sees the self keyword.
This also makes our code much cleaner. We arent confusing our variables, auto completion helps us too.
left out a very important part which is setting __index member of Person to Person
local Person = {}
Person.__index = Person
function Person.new(name)
return setmetatable({name = name}, Person)
end
function Person:talk(message)
print(message, "-", self.name)
end
also table:method(...) syntax is syntactic sugar for table.method(table, ...)
It would deftintly work. But again it can work without it.
Here is an example:
local knife = {}
knife.__index = knife
function knife.new(Perk,Effect) --or whatever you need
return setmetatable({
Self.Perk = Perk,
Self.Effect = Effect,
Self.Damage = 100,
Self.Speed = 10},knife) -- and whatever else, you can change the variables here!
end
local function ThrowPhysics(Velocity)
-- all our throw physic code here
end
function knife:Throw()
-- all our throw code here
-- call throwphysics at the end
ThrowPhysics(Self.Speed) -- our self.Speed is 10
end
function knife:Melee()
-- all our code here
end
return knife = {}
Now how would this look on our server?
local knifemodule = require(pathhere)
local perk = pathhere --lets say its a module script that returns the function
local effects = pathhere -- same thing
local knifeClass = knifemodule.new(perk,effects)
--if all goes well, we can connect our events and use our methods with no problem
if statement then -- lets say we connect User input service, we can do:
knifeClass:Throw()
end
-- we can do the same for the rest while keeping our code clean and have less lines
We can now use the same script to be under every knife tool!
Im sorry if this doesnt make sense, my bad on my part. Let me know for any questions
Also, of course you can always use this without OOP, but you have to make sure you connect all the stuff you need, oop is just a nice way of using the same code over and over without copying and pasting and also organized well.
You dont really know what you are talking about (i did read your other posts under this thread and it just proves that fact further)
The thing you are looking for is called a functional programming and NOT OOP.
Why are you implementing physics into an MM2-style game? I feel like a knife that travels straight is good unless you really want your knives to curve and stuff
There is already a module handling range weapons, fastcastredux, it offers alot of stuff. It can be such a headache building it yourself but if you love suffering and want to learn then create a range module based on alot of raycasts.