VC:MP 0.4 (Beta) > Script Releases

Mansion Gate

(1/1)

Flockshot:
Today i release a simple function
What it do is add a gate ,at the entrance of Mansion,which can open and close by hotkeys.

OnPlayerStart function.
we bind the key and create the gate here.Also we will make a global variable that tell us is gate open or close.

--- Code: ---OnServerStart()
{
open <- false;
mansiongate <- CreateObject(6000,0,Vector(-277, -495, 10),255);

opengate <- BindKey(true,0x104F ,0,0); //The Key IS "O"
closegate <- BindKey(true,0x205A,0,0); //The Key IS "Z"
}

--- End code ---

OnKeyDown function
where we will add our function.

--- Code: ---function onKeyDown(player, bindid)
{
if(bindid==opengate)
{
if(open==false) MoveGate(player,mansiongate.ID,"open");
else MessagePlayer("Gate is already open.",player);
}

else if(bindid==closegate)
{
if(open==true) MoveGate(player,mansiongate.ID,"close");
else MessagePlayer("Gate is already closed.",player);
}
}

--- End code ---

MoveGate function
we will open or close the gate here.

--- Code: ---function MoveGate(player,gate, status)
{
local obj=FindObject(gate);
if(obj)
{
if(status=="open")
{


MessagePlayer("Opening Gate",player);
obj.MoveBy(Vector(8,0,0), 2800);
open=true;
}
else if(status=="close")
{

MessagePlayer("Closing Gate",player);
obj.MoveBy(Vector(-8,0,0), 2800);
open=false;

}
}
else MessagePlayer("Object could not be found.",player);
}

--- End code ---

This is it for this function or
If you want to only use 1 key for both opening and closing then use this.

OnPlayerStart function.
Same just we dont bind the key "Z".

--- Code: ---OnServerStart()
{
open <- false;
mansiongate <- CreateObject(6000,0,Vector(-277, -495, 10),255);

gate <- BindKey(true,0x104F ,0,0); //The Key IS "O"
}

--- End code ---

OnKeyDown function
we will only use one key here.

--- Code: ---function onKeyDown(player, bindid)
{
if(bindid==gate)
{
if(open==false) MoveGate(player,masiongate.ID,"open");
else MoveGate(player,mansiongate.ID,"close");
}

}

--- End code ---


MoveGate function
Same func

--- Code: ---function MoveGate(player,gate, status)
{
local obj=FindObject(gate);
if(obj)
{
if(status=="open")
{


MessagePlayer("Opening Gate",player);
obj.MoveBy(Vector(8,0,0), 2800);
open=true;
}
else if(status=="close")
{

MessagePlayer("Closing Gate",player);
obj.MoveBy(Vector(-8,0,0), 2800);
open=false;

}
}
else MessagePlayer("Object could not be found.",player);
}

--- End code ---

Screenshots:


sseebbyy:
By releasing this script to the public, you will help many newbies. :)
(I would use a class to create these gates, to make it easier, instead of this way)
But...  there is a problem. What happens if the gate is not id 0 ?Another object will move instead of the gate.

To fix that, note the object with an word like "mansiongate".
How you do that ? Like this:

--- Code: ---mansiongate <- CreateObject(6000,0,Vector(-277, -495, 10),255);
--- End code ---

Then you simply move it using:

--- Code: ---mansiongate.MoveBy(Vector(-8,0,0), 2800);
--- End code ---


By the way, it's good that it uses a global function as "MoveGate", for closing/opening the gate.
I suggest you to add one more parameter that will represent the gate (object), so you can use the same function for any gate you want. :P
Like this:

--- Code: ---function MoveGate(player,gate,status)
{
   if(status="open")
   {
         obj.MoveBy(Vector(8,0,0), 2800);
   }
}

--- End code ---

Flockshot:

--- Quote from: sseebbyy on October 12, 2014, 12:19:33 am ---By releasing this script to the public, you will help many newbies. :)
(I would use a class to create these gates, to make it easier, instead of this way)
But...  there is a problem. What happens if the gate is not id 0 ?Another object will move instead of the gate.

To fix that, note the object with an word like "mansiongate".
How you do that ? Like this:

--- Code: ---mansiongate <- CreateObject(6000,0,Vector(-277, -495, 10),255);
--- End code ---

Then you simply move it using:

--- Code: ---mansiongate.MoveBy(Vector(-8,0,0), 2800);
--- End code ---


By the way, it's good that it uses a global function as "MoveGate", for closing/opening the gate.
I suggest you to add one more parameter that will represent the gate (object), so you can use the same function for any gate you want. :P
Like this:

--- Code: ---function MoveGate(player,gate,status)
{
   if(status="open")
   {
         obj.MoveBy(Vector(8,0,0), 2800);
   }
}

--- End code ---


--- End quote ---
Hmm.
I have chnaged it now the function will work for any object and also i have created class for mansion gate.
player can add multiple gates in game and can only use one function.

soulshaker:
Why not you make the gate auto open and close instead of bindkeys, as if some newbies joins server they may face problem to enter or exit mansion(just a suggestion)

sseebbyy:

--- Quote from: soulshaker on October 12, 2014, 12:09:21 pm ---Why not you make the gate auto open and close instead of bindkeys, as if some newbies joins server they may face problem to enter or exit mansion(just a suggestion)

--- End quote ---

it can be configured to be actioned just by the owner of the property. ;p



--- Quote from: Flockshot on October 12, 2014, 08:41:25 am ---Hmm.
I have chnaged it now the function will work for any object and also i have created class for mansion gate.
player can add multiple gates in game and can only use one function.

--- End quote ---

By class I mean class{} with constructor and etc.
(so it would be easier to check gate's status, like gate.status that will return the status of the gate, obviously)
Anyway, here comes another problem... the variable open is a global one, so when a player will open/close any gate, it will be set for all instead of only to that gate.

Navigation

[0] Message Index

Go to full version