Jump to content

Would OOP work in this situation?


AnonXVI

Recommended Posts

Hello,

So I'm pretty new to scripting and stuff, and I'm doing some random stuff around MTA scripting.

I'm Thinking about making a RP gamemode for practice, and thought that it should be in OOP(Because more organized and stuff).

But I can't find information about using OOP when you're seperating everything to different resources.

Like not whole 1 gamemode resource, but loads of seperate script resources, like login job1 job2 bank sql and etc.

Question 1. Is it posible to use OOP in situation like this? If yes, should I? Or should I just use Procedural Function Programming?

Question 2. Should I consider about not using seperate resources, and using 1 whole gamemode resource? If yes, why? What benefits would it give over seperate resources? It would remove me ability to update little stuff without stopping server play.

(Answer only if I should use OOP)Question 3. Could I get some useful LUA OOP Tips, Tricks, Tutorials, Links, and/or Information? I don't have any experience with any language's OOP.

And Very Thanks In Advance!

Link to comment

Personally I prefer not to use OOP when scripting Lua, so I won't comment on these questions.

As for Question 2, you basically answered it yourself already. As far as I know there wouldn't be too big differences whether you have many small or just a big one. If you have lots of smaller ones, refreshing all the resources could take some time - and it might end up a bit unorganized in terms of folder structure.

If you make a big one, there would be less resources to refresh - but the loading time of this resource might be considerably longer(but this shouldn't matter if it's only started along with the server).

The biggest drawback as I see it, is the one you just said yourself - not being able to apply fixes and updates without having to restart the entire thing.

I think in the end, it mostly comes down to personal preference above anything else. There may be some differences in terms of performance, but I doubt they'd be too severe to become gamebreaking. :)

Link to comment

Thanks for detailed answer.

Personally I prefer not to use OOP when scripting Lua

Is there a serious reason which made you not use OOP on Lua? Or just habit and etc.? :D

If you have lots of smaller ones, refreshing all the resources could take some time - and it might end up a bit unorganized in terms of folder structure.
If you make a big one, there would be less resources to refresh - but the loading time of this resource might be considerably longer(but this shouldn't matter if it's only started along with the server).
The biggest drawback as I see it, is the one you just said yourself - not being able to apply fixes and updates without having to restart the entire thing.

So long story short, one big resource has more downsides than lots of smalls, and lots of smalls downsides are personal and minor. Well I'm okay with lots of smalls then, just wanted to make sure if it won't affect performance much and stuff. So only OOP question left. Thanks!

I think in the end, it mostly comes down to personal preference above anything else. There may be some differences in terms of performance, but I doubt they'd be too severe to become gamebreaking.

Well, I'm flexible person, so I can get around with most stuff which is better to use, but true.

Link to comment

1. Yes, you can use OOP in a situation like that. However, it becomes a bit more complex (see metatables and loading a class into a resource via loadstring). Personally, I use OOP for all of my MTA coding except calling/exporting other resources. I use the procedural exports.resource:function(args) method. OOP will make your code more organised, easier to read and you will have to type less (plr.account.name as opposed to getAccountName(getPlayerAccount(plr))). I would recommend using OOP as MTA is heading in that direction anyway and it just looks nicer.

2. Dealman said it pretty well. I have personally used lots of smaller resources (and lots of servers also tend to do that) and find it much easier to work with. I find it easier to diagnose and debug problems if you can isolate it to a specific resource. I encourage you to experiment with both and find out where you're comfortable most and what you prefer.

3. OOP, as the name suggests, is object-oriented programming. That means you code with objects (not to be confused with elements or objects in MTA). There are two types of functions (can also be called methods) - static and instance. Static functions are functions where you don't need an object to call them (Vehicle.create(), Element.getAllByType() are some examples). Instance calls are where you need to pass an existing object from that class into the function/method (player:setName(), element:setHealth() are some examples). Familiarise yourself with that and learn what they mean.

Don't think you need to code everything in OOP either. I still have to code some things the procedural way simply because the OOP function does not exist yet.

https://wiki.multitheftauto.com/wiki/OOP_Introduction - An introduction to OOP

https://wiki.multitheftauto.com/wiki/OOP_client - A list of all client-side OOP functions

https://wiki.multitheftauto.com/wiki/OOP_server - A list of all server-side OOP functions

https://wiki.multitheftauto.com/wiki/Vector - Different types of vectors. A lot of OOP function take Vector3/Vector2 instead of normal x, y, z coordinates.

https://wiki.multitheftauto.com/wiki/Matrix - Matrices. A little more complicated, but very powerful. You can use these to minimise the amount of math you might need to do. A highlight of OOP.

https://wiki.multitheftauto.com/wiki/MTA_Classes - A list of all classes in MTA.

https://wiki.multitheftauto.com/wiki/Element_tree - The element tree. Learning this will save you a lot of time later on (speaking from experience).

Some OOP questions that have been asked on this forum:

https://forum.multitheftauto.com/viewtopic.php?f=91&t=86225

https://forum.multitheftauto.com/viewtopic.php?f=91&t=93452

https://forum.multitheftauto.com/viewtopic.php?f=91&t=85392

The main OOP guy around here is qaisjp. He's pretty good to talk to if you have any questions.

Link to comment
1. Yes, you can use OOP in a situation like that. However, it becomes a bit more complex (see metatables and loading a class into a resource via loadstring). Personally, I use OOP for all of my MTA coding except calling/exporting other resources. I use the procedural exports.resource:function(args) method. OOP will make your code more organised, easier to read and you will have to type less (plr.account.name as opposed to getAccountName(getPlayerAccount(plr))). I would recommend using OOP as MTA is heading in that direction anyway and it just looks nicer.

2. Dealman said it pretty well. I have personally used lots of smaller resources (and lots of servers also tend to do that) and find it much easier to work with. I find it easier to diagnose and debug problems if you can isolate it to a specific resource. I encourage you to experiment with both and find out where you're comfortable most and what you prefer.

3. OOP, as the name suggests, is object-oriented programming. That means you code with objects (not to be confused with elements or objects in MTA). There are two types of functions (can also be called methods) - static and instance. Static functions are functions where you don't need an object to call them (Vehicle.create(), Element.getAllByType() are some examples). Instance calls are where you need to pass an existing object from that class into the function/method (player:setName(), element:setHealth() are some examples). Familiarise yourself with that and learn what they mean.

Don't think you need to code everything in OOP either. I still have to code some things the procedural way simply because the OOP function does not exist yet.

https://wiki.multitheftauto.com/wiki/OOP_Introduction - An introduction to OOP

https://wiki.multitheftauto.com/wiki/OOP_client - A list of all client-side OOP functions

https://wiki.multitheftauto.com/wiki/OOP_server - A list of all server-side OOP functions

https://wiki.multitheftauto.com/wiki/Vector - Different types of vectors. A lot of OOP function take Vector3/Vector2 instead of normal x, y, z coordinates.

https://wiki.multitheftauto.com/wiki/Matrix - Matrices. A little more complicated, but very powerful. You can use these to minimise the amount of math you might need to do. A highlight of OOP.

https://wiki.multitheftauto.com/wiki/MTA_Classes - A list of all classes in MTA.

https://wiki.multitheftauto.com/wiki/Element_tree - The element tree. Learning this will save you a lot of time later on (speaking from experience).

Some OOP questions that have been asked on this forum:

https://forum.multitheftauto.com/viewtopic.php?f=91&t=86225

https://forum.multitheftauto.com/viewtopic.php?f=91&t=93452

https://forum.multitheftauto.com/viewtopic.php?f=91&t=85392

The main OOP guy around here is qaisjp. He's pretty good to talk to if you have any questions.

So I played some with OOP, and it seems more better for me personally. Decided to use that. Made simple SQL resource, which works. But I guess I will have to re-code it to procedural, because there is no effective way to use export SQL functions in OOP to others resources. But it's no problem, because it's small code, so procedural works. And nothing is lost, because learned big OOP basics, and can read code easier in mind.

About 1 folder or smaller ones: Decided to use loads of smaller ones, because of features you and Dealman mentioned. But there is one thing going around my mind about it, what stuff should I put in "gamemode" resource? Like, there is loads of "scripts" login, jobs and stuff, so what is left to put in "gamemode" resource? Nothing? Doesn't seem that it needs to be one big handler, because there would be nothing to handle in it.

Thanks for answering to third question so detailed, it helped me a lot. Got better logic idea how stuff works.

Dealman is right. I wrote an entire lobby script ( around 6000 lines ) in lua without using OOP. It worked well. Its a matter of choice. I use OOP in C++ only because its necessary there .

So I guess it's just a personal matter, and best is the one, you like more.

Sorry for my grammar mistakes, living in a country where english is not the main language. I'm new to this stuff, so I can be wrong about it too. Thanks for so detailed answers, helped me a lot.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...