Constructors

Started by Mötley, Apr 29, 2016, 06:30 PM

Previous topic - Next topic

Mötley

What is the real preference of a constructor when scripting in VC:MP?

My preference would be accounting Information on a player say

Password
Ip
Account_Level
Registration_Date
Last_Active

Then I would create a class to hold a players stats in while on the server like

Joins
Kills
Deaths

I noticed some scriptwriters will have Constructors follow along the class rather than adding in differently, as follows.

Cash
Bank
Kills
Deaths
Level
Last_IP
Wanted_Level

What is the correct way of a constructor? I feel that bank cash kills etc should not be in a constructor rather than it should be in the class for stats. and constructor for getting the password and account level etc. If I where to add any of this to a class then I would but I would possibly create another class somewhere else to keep data clean and seperated,.

password = null;
account_level = 0;
ip = null; // No IP
Registration_Date = 0;
Last_Active = 0;
Logged = false;

::I just need to clear things up.

Mötley

#1
So I could add 32 things in on class as follows in the constructor? Rather than (shorter answer) separating the data?

I mean In a way it makes sense, but seems not clean

I think maybe there should be a class at the beginning before the constructor and after the constructor add in the rest of the class before finishing off this function. The first on for stats the second for login data  .

.

Those are member variables, properties, attributes, whatever you want to call them. Not constructors. Their order is irelevant because a class or instance is nothing more than a fancy table (in Squirrel). And tables are sorted (in Squirrel). The constructor is the function called when an instance of that class is created.
.

Mötley

#3
What you said is logical now.. I think I have adventured around two much in my scripting lately (LUA)
Of course with more functions to call upon in the constructor for the Cash bank kills etc, I guess I am not as confused as I was as I was adventuring with something like,,

class Player
{
Cash = 0;
Bank = 0;
Kills = 0;
Deaths = 0;
Wanted_Level = 0;

constructor( accounting)
{
if (( query, 0 ) )
{
password
ip
level
registration_date
last_active
}
}

function register( player, password )
{
// This function registers a player

password
ip
level
registration_date
last_active
}

password = null;
level = 0;
ip = null;
registration_date = 0;
last_active = 0;
logged_in = false;
};

KAKAN

instead of doing so much, you can something like thia if you have the MySQL plugin.
class Player
{
stats = null;

constructor( player )
{
this.stats = {};
local q = ::MySQL_query(db,"SELECT Kills,Deaths,Bank,Cash FROM Accounts WHERE Name='"+player.Name+"'"), data = ::MySQL_fetch_assoc( q ); //This auto corrector changes "MySQL".tolower() to "MySQL"
foreach( idx, value in data ){
this.stats[idx] = data; //The end.
}
}
}
Just an example of how you can do it.
oh no

.

Sorry, but wtf are we talking about here? Because I fail to understand.
.

Mötley

All I want to know is the correct usage. Now I trust ^ S.L.C on that he told me those were member variables, properties, attributes etc "Seems like he didn't want to go to far but I understand 80% where he was going",.

It is Vital that I know the correct usage, I know constructors can be used it different methods than Accounting with stats etc.

All I really want is to be capable for using a constructor with classes in a professional layout. As some methods I have seen seems not professional(not clean).

A cleaner system is always whats best when uploaded online.
So I will Leave this topic open in case another scriptwriter drops by to share any other logic's .