[Tutorial] /copy [object] command
#1

I recently made a mapping script and had people asking me for specific features I included. One of those being a /copy command. It's probably the main command I had issues with, as it involved storing values in an array and I wasn't really good with them during my noob days. That isn't the case now, though. Here it is, for whoever wants an example on how to make a /copy command.

Basic scripting knowledge is recommended, by the way.

I used the following includes in the code below. If you wish to copy it, make sure to include them yourself:

pawn Code:
#include <a_samp>
#include <sscanf2>
#include <zcmd>
To move on, we'll be using two variables:

pawn Code:
// oModel[id] for storing the object models. The array stands for object ID
new oModel[MAX_OBJECTS];
// ObjectCount for determining the obj ID when we can't manually pull it
new ObjectCount;
As an example, let's assume that this is our object spawning code:
pawn Code:
CMD:object(playerid, params[])
{
    // Everything should be obvious. We use "objid" for the sscanf param, and "objsel" for
    // the object selection after it has been spawned.
    new objid, objsel, Float: X, Float: Y, Float: Z;
    GetPlayerPos(playerid, X, Y, Z);

    // Please note how we use "objid". The syntax is: /object [modelid], so we can simply store
    // "objid" as it's technically the model ID.
    if(sscanf(params, "i", objid)) return SendClientMessage(playerid, -1, "/object [param]");

    // objsel and EditObject for editing after spawning it, self explanatory stuff
    objsel = CreateObject(objid, X, Y +1, Z, 0.00, 0.00, 0.00);
    EditObject(playerid, objsel); // Selecting the object...

    return true;
}
In order to store our model ID, we'll have to add the following:
pawn Code:
ObjectCount ++; // multiplies objectcount to suffice as the object ID
oModel[ObjectCount] = objid; // stores "objid" aka model in oModel[object ID].
As such, it would look like this:
pawn Code:
CMD:object(playerid, params[])
{
    new objid, objsel, Float: X, Float: Y, Float: Z;
    GetPlayerPos(playerid, X, Y, Z);

    if(sscanf(params, "i", objid)) return SendClientMessage(playerid, -1, "/object [param]");

    ObjectCount ++; // multiplies objectcount to suffice as the object ID
    oModel[ObjectCount] = objid; // stores "objid" aka model in oModel[object ID].

    objsel = CreateObject(objid, X, Y +1, Z, 0.00, 0.00, 0.00);
    EditObject(playerid, objsel);

    return true;
}
An example of a working copy command, using the variables we defined:
pawn Code:
CMD:copy(playerid, params[])
{
    // Same as before, objid and objsel (same purpose as before), and xyz & rxryrz floats.
    new objid, objsel, Float: X, Float: Y, Float: Z, Float: rX, Float: rY, Float: rZ;

    // sscanf modifier, should be self explanatory
    if(sscanf(params, "i", objid)) return SendClientMessage(playerid, -1, "/copy [param]");
    // We check if the object is a valid object. If the object you're trying to copy doesn't exist, it'll show you a message.
    if(!IsValidObject(objid)) return SendClientMessage(playerid, -1, "The object ID you entered isn't valid.");

    // Using our floats and storing the object coordinates and rotation in it.
    // Please note that we're extracting them from "objid", aka the param we used @sscanf
    GetObjectPos(objid, X, Y, Z), GetObjectRot(objid, rX, rY, rZ);

    // Since we're adding a new object, we multiply ObjectCount's value.
    ObjectCount ++;
    // Now pulling the modelid from oModel, we use the sscanf param
    // to determine what objectid to pull the model from.
    oModel[ObjectCount] = oModel[objid];

    // And again, objsel for selection after spawning and then using our stored
    // coordinates and rotation to determine the correct position of the new object
    objsel = CreateObject(oModel[objid], X, Y, Z, rX, rY, rZ);
    EditObject(playerid, objsel); // Self explanatory

    return true;
}
Feel free to leave feedback, I'd appreciate it. For leechers, check this parse for no comments/full script: http://pastebin.com/NNcNdeRj

Have fun.
Reply


Messages In This Thread
/copy [object] command - by Mionee - 19.06.2014, 18:25
Re: /copy [object] command - by TheNeeraz - 20.06.2014, 06:02

Forum Jump:


Users browsing this thread: 2 Guest(s)