What is the best approach?

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.

3 Likes

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.

Good luck

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.

I do use module scripts I just haven’t gotten into the whole classes and self aspect of things

1 Like

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.

How would my example work for classes, I don’t think it would right? that example would be for creating the item and putting it into the world no?

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, ...)

1 Like

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.

1 Like

I apologize for missing that, it was the very important part, thats really needed. Thank you for noticing

1 Like

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.

2 Likes

Well you may be right, this is just my approach, you have yours. You should explain on your part to help therealmarcus.

Last question, how about the projectiles? Is it best to use physics or somehow simulate it through a raycast, or perhaps some other method

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

Yes curvature is required for this, Needs drop like a real knife would have.

There are a lot of calculations you would have to do, but using OOP like @lamcellari would make your life easier.

1 Like

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.