Vice City: Multiplayer

Server Development => Scripting and Server Management => Topic started by: MacTavish on Oct 22, 2015, 12:10 PM

Title: [Request] Example about saving instance in array/table
Post by: MacTavish on Oct 22, 2015, 12:10 PM
The topic name says all but i will explain a bit more. I wanna know that how we could save and instance such as player nicks in an array or table and after adding how to remove just an nick from the array or table
Title: Re: [Request] Example about saving instance in array/table
Post by: rulk on Oct 22, 2015, 12:27 PM
1. First create a dynamic or static array.

// This creates a static array of 10 elements with 'null' as the index values.
MyArray <- array( 10, null );

// This creates a dynamic array. ( there are no elements in the array, as of yet )
MyArray <- [];

2. Now, we can create our class. ( remember to use the constructor/deconstructor )

// Class Header
class MyFirstClass
{
// The Constructor.
  constructor( ... ) {

MyProperty1 = vargv[ 0 ];
MyProperty2 = vargv[ 1 ];
MyProperty3 = vargv[ 2 ];
}

// Reset Property values
MyProperty1 = null;
MyProperty2 = null;
MyProperty3 = null;
}

3. Finally, we can create a instance of our class and store it in our array index.

// For static arrays, use ...
MyArray[ 0 ] = MyFirstClass( MyProperty1, MyProperty2, MyProperty3 );

// For dynamic arrays, use ...
MyArray.push( MyFirstClass( MyProperty1, MyProperty2, MyProperty3 ) );

I tried not to complicate it for you, if you are unsure about anything, please let me know.

regards,

rulk

sorry, but the 'code' tag messes up the formatting :-(
Title: Re: [Request] Example about saving instance in array/table
Post by: EK.IceFlake on Oct 22, 2015, 12:45 PM
Quote from: Kusanagi on Oct 22, 2015, 12:10 PMand after adding how to remove just an nick from the array or table
I'll just build upon rulks example
To remove, find his ID and remove it
MyArray.remove(player.ID);Also you can use tables.
Add a global table [slc please don't complain]
MyTable <- {};To add a guy
MyTable.rawset(player.Name, MyClass());To remove a guy
MyTable.rawset(player.Name, null);To access a guy
MyTable["Kusangi"].MyInternalVariable1 = "Is a noob not";
MyTable["Kusangi"].MyInternalVariable2 = "Kinda funny looking";
MyTable["Kusangi"].MyInternalVariable3 = "Likes to wear professional clothes and hide a katana inside";
MyClass
class MyClass
{
    MyInternalVariable1 = null;
    MyInternalVariable2 = null;
    MyInternalVariable3 = null;
}
Title: Re: [Request] Example about saving instance in array/table
Post by: MacTavish on Oct 22, 2015, 02:51 PM
Thanks for Examples guys