Hello everyone,
As I was writing my Zombie class script, I realized how messy and unreadable my code had become due to the logic statements everywhere. (don’t mind the errors)
I was wondering if you should put logic in objects like this. Are you supposed to handle logic outside the object or in the object itself?
p.s: I am a beginner in coding and not very good at computer science vocab so don’t use too complex words
1 Like
I think this could be done, but it would be better practice to use a function. You could do something like this:
local function ToCalculatePath(CloseToTarget, InStraightLine, TargetMoved)
if not CloseToTarget and not InStraightLine and TargetMoved then
return true
else
return false
end
end
You can then call this function inside of an if statement like so:
if ToCalculatePath(..., ..., ...) then ... end
This function would evaluate to true or false based on the values you pass in.
I know I can do that but it doesn’t really answer my question if I should do logic inside an object or do logic outside of the object.
There is no problem with putting logic inside of objects, and it’s a good thing to do when you’ll be dealing with multiple objects of the same type. This way, the objects themselves can call their own function rather than an external function having to be called externally for each object.
In semantic terms, functions inside of objects are called methods.
2 Likes
Oh I see. I guess I’ll just use logic inside the object but put it in a separate method. Thanks for the tips!
EDIT: Also thanks for sticking to simple terms; really helps me understand
1 Like