Loop through defined objects?
#1

Hey, how can I loop through this defined object, I got on top
pawn Код:
new Objects[5];

//Defined Object + loop
Objects[?]  = Createobject...
//I need to loop through the amount of times this has been used so lets say if I used this the first time I want it to be Objects[1], second time Objects[2]. And so on
Reply
#2

Read about "for ()".
https://sampwiki.blast.hk/wiki/Control_S...res#for_.28.29
if you have any questions after you've read it i'll be glad to help you out
Reply
#3

Yea I came across this a multiple times but still couldn't figure it out. I tried using this
pawn Код:
for(new i = 0; i < 5; i++)
    {
        Objects[i] = CreateObject......
    }
This didn't work well when I tested this.
Reply
#4

One way would be to replace CreateObject with your own function. Loop alternative above should also work, if you have indexed arguments in an array.

pawn Код:
new objects[5];
new i_count;
pawn Код:
stock my_CreateObject(model, Float:x, Float:y, Float:z, Float:rx, Float:ry, Float:rz)
{
    //some code here
    objects[i_count] = CreateObject(model, x, y, z, rx, ry, rz);
    i_count++;
}
I hope you have some understanding of where to put functions and how to call them.
Reply
#5

Quote:
Originally Posted by RALL0
Посмотреть сообщение
Yea I came across this a multiple times but still couldn't figure it out. I tried using this
pawn Код:
for(new i = 0; i < 5; i++)
    {
        Objects[i] = CreateObject......
    }
This didn't work well when I tested this.
That code should work fine. What didn't work?
Reply
#6

Quote:
Originally Posted by RALL0
Посмотреть сообщение
Yea I came across this a multiple times but still couldn't figure it out. I tried using this
pawn Код:
for(new i = 0; i < 5; i++)
    {
        Objects[i] = CreateObject......
    }
This didn't work well when I tested this.
What exatcly do you want to do with the loop?

The loop you made is creating 5 objects.
Reply
#7

The comment at the code explains what isn't working well
pawn Код:
CMD:unloadforklift(playerid, params[])
{
    new Float:Pos[5];
    new vehicleid = GetPlayerVehicleID(playerid);
    if(!IsAForklift(vehicleid)) return SendClientMessage(playerid, COLOR_DARKRED,"[SERVER] {FFFFFF}You must be a driver of a forklift to load a forklift.");
    if(ForkliftLoaded[vehicleid] == 0) return SendClientMessage(playerid, COLOR_DARKRED,"[SERVER] {FFFFFF}This forklift isnґt carrying a crate.");
    ForkliftLoaded[vehicleid] = 0;
    DestroyObject(ForkliftObject[vehicleid][vObject]);
    GetPlayerPos(playerid, Pos[0],Pos[1],Pos[2]); GetPlayerFacingAngle(playerid, Pos[3]);
    GetXYInFrontOfPlayer(playerid, Pos[0], Pos[1], 5);
    for(new i = 0; i < 5; i++)
    {
        CrateObjectsPlaced[i] = CreateObject(964, Pos[0]+90, Pos[1], Pos[2]-0,72, Pos[3]-45, -45,180);
    }
    return 1;
}
   
   
CMD:loadforklift(playerid, params[])
{
    new vehicleid = GetPlayerVehicleID(playerid);
    new crate = PlayerNearCrate(playerid);
    new crate1 = PlayerNearPlacedCrate(playerid);
    if(ForkliftLoaded[vehicleid] == 1) return SendClientMessage(playerid, COLOR_DARKRED,"[SERVER] {FFFFFF}This forklift is already carrying a crate.");
    if(!IsAForklift(vehicleid)) return SendClientMessage(playerid, COLOR_DARKRED,"[SERVER] {FFFFFF}You must be a driver of a forklift to load a forklift.");
    if(crate != -1)
    {
        ForkliftLoaded[vehicleid] = 1;
        DestroyObject(CrateObjects[crate]);
        ForkliftObject[vehicleid][vObject] = CreateObject(964, 0, 0.9, -0.12, 0, 0, 0);
        AttachObjectToVehicle(ForkliftObject[vehicleid][vObject], vehicleid, 0, 0.9, -0.12, 0, 0, 0);
    }
    else if(crate1 != -1)
    {
        ForkliftLoaded[vehicleid] = 1;
        DestroyObject(CrateObjectsPlaced[crate1]);//It doesn't really destroy the nearest crate, however I suspect that the trouble is at the loop of mine, because the DestroyObject thats a couple of line above this is working fine.
        ForkliftObject[vehicleid][vObject] = CreateObject(964, 0, 0.9, -0.12, 0, 0, 0);
        AttachObjectToVehicle(ForkliftObject[vehicleid][vObject], vehicleid, 0, 0.9, -0.12, 0, 0, 0);
    }
    else return SendClientMessage(playerid, COLOR_DARKRED,"[SERVER] {FFFFFF}You aren't near any crate.");
    return 1;
}
Reply
#8

Quote:
Originally Posted by RALL0
Посмотреть сообщение
The comment at the code explains what isn't working well
pawn Код:
CMD:unloadforklift(playerid, params[])
{
    new Float:Pos[5];
    new vehicleid = GetPlayerVehicleID(playerid);
    if(!IsAForklift(vehicleid)) return SendClientMessage(playerid, COLOR_DARKRED,"[SERVER] {FFFFFF}You must be a driver of a forklift to load a forklift.");
    if(ForkliftLoaded[vehicleid] == 0) return SendClientMessage(playerid, COLOR_DARKRED,"[SERVER] {FFFFFF}This forklift isnґt carrying a crate.");
    ForkliftLoaded[vehicleid] = 0;
    DestroyObject(ForkliftObject[vehicleid][vObject]);
    GetPlayerPos(playerid, Pos[0],Pos[1],Pos[2]); GetPlayerFacingAngle(playerid, Pos[3]);
    GetXYInFrontOfPlayer(playerid, Pos[0], Pos[1], 5);
    for(new i = 0; i < 5; i++)
    {
        CrateObjectsPlaced[i] = CreateObject(964, Pos[0]+90, Pos[1], Pos[2]-0,72, Pos[3]-45, -45,180);
    }
    return 1;
}
   
   
CMD:loadforklift(playerid, params[])
{
    new vehicleid = GetPlayerVehicleID(playerid);
    new crate = PlayerNearCrate(playerid);
    new crate1 = PlayerNearPlacedCrate(playerid);
    if(ForkliftLoaded[vehicleid] == 1) return SendClientMessage(playerid, COLOR_DARKRED,"[SERVER] {FFFFFF}This forklift is already carrying a crate.");
    if(!IsAForklift(vehicleid)) return SendClientMessage(playerid, COLOR_DARKRED,"[SERVER] {FFFFFF}You must be a driver of a forklift to load a forklift.");
    if(crate != -1)
    {
        ForkliftLoaded[vehicleid] = 1;
        DestroyObject(CrateObjects[crate]);
        ForkliftObject[vehicleid][vObject] = CreateObject(964, 0, 0.9, -0.12, 0, 0, 0);
        AttachObjectToVehicle(ForkliftObject[vehicleid][vObject], vehicleid, 0, 0.9, -0.12, 0, 0, 0);
    }
    else if(crate1 != -1)
    {
        ForkliftLoaded[vehicleid] = 1;
        DestroyObject(CrateObjectsPlaced[crate1]);//It doesn't really destroy the nearest crate, however I suspect that the trouble is at the loop of mine, because the DestroyObject thats a couple of line above this is working fine.
        ForkliftObject[vehicleid][vObject] = CreateObject(964, 0, 0.9, -0.12, 0, 0, 0);
        AttachObjectToVehicle(ForkliftObject[vehicleid][vObject], vehicleid, 0, 0.9, -0.12, 0, 0, 0);
    }
    else return SendClientMessage(playerid, COLOR_DARKRED,"[SERVER] {FFFFFF}You aren't near any crate.");
    return 1;
}
The problem is that if crate will be 0 or any other positive number the "else if" wont be called, change the "else if" to just "if".
Reply
#9

I still got the same problem, however it will add a object on the forklift but won't delete the one on the ground. So this has nothing to do with the else if or if. I really think it has something to do with the loop because the problem isn't only under this command, I got another public function with the same loop and same DestroyObject but it won't delete the object at all.
Reply
#10

Bump
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)