SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 02/02/2020, 11:43 AM   #1
Bect
Big Clucker
 
Bect's Avatar
 
Join Date: Apr 2011
Posts: 179
Reputation: 1
Default I need help with "foreach" [SOLVED]

[SOLVED] Thanks to: Vizi10

How to use "foreach" ??
I have this and i want to use "foreach".
Example:

Code:
new Object[5];

public OnFilterScriptInit()
{
    Object[0] = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Object[1] = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Object[2] = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Object[3] = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Object[4] = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    return 1;
}
public OnFilterScriptExit()
{
    if (IsValidObject(Object[0]))
    {
        DestroyObject(Object[0]);
    }
    if (IsValidObject(Object[1]))
    {
        DestroyObject(Object[1]);
    }
    if (IsValidObject(Object[2]))
    {
        DestroyObject(Object[2]);
    }
    if (IsValidObject(Object[3]))
    {
        DestroyObject(Object[3]);
    }
    if (IsValidObject(Object[4]))
    {
        DestroyObject(Object[4]);
    }
    return 1;
}
I want something like that:
Code:
new Object[5];

public OnFilterScriptInit()
{
    Object[0] = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Object[1] = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Object[2] = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Object[3] = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Object[4] = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    return 1;
}
public OnFilterScriptExit()
{
    foreach (new OBJ: Object[5])
    {
        if (IsValidObject(OBJ))
        {
            DestroyObject(OBJ);
        }
    }
    return 1;
}

Last edited by Bect; 02/02/2020 at 01:12 PM.
Bect is offline   Reply With Quote
Old 02/02/2020, 11:55 AM   #2
Vizi10
Huge Clucker
 
Join Date: May 2017
Location: Iran
Posts: 310
Reputation: 8
Default Re: I need help with "foreach"

pawn Code:
new Iterator:Objects<MAX_OBJECTS>;

public OnFilterScriptInit()
{
    new ObjectID;
    ObjectID = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Iter_Add(Objects, ObjectID);
    ObjectID = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Iter_Add(Objects, ObjectID);
    ObjectID = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Iter_Add(Objects, ObjectID);
    ObjectID = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Iter_Add(Objects, ObjectID);
    ObjectID = CreateObject(modelid, X, Y, Z, rX, rY, rZ, dD);
    Iter_Add(Objects, ObjectID);
    return 1;
}

public OnFilterScriptExit()
{
    foreach(new o : Objects)
    {
        DestroyObject(o);
    }
    Iter_Clear(Objects);
    return 1;
}
Vizi10 is online now   Reply With Quote
Old 02/02/2020, 12:26 PM   #3
Bect
Big Clucker
 
Bect's Avatar
 
Join Date: Apr 2011
Posts: 179
Reputation: 1
Default Re: I need help with "foreach"

Pawno gives no erros but when I go to test, it doesn't work. The objects do not disappear.

Edit: It was my mistake. It works just fine. Thank you ! + rep !

Last edited by Bect; 02/02/2020 at 01:10 PM.
Bect is offline   Reply With Quote
Old 02/02/2020, 12:49 PM   #4
SiaReyes
Gangsta
 
SiaReyes's Avatar
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 622
Reputation: 57
Default Re: I need help with "foreach"

Code:
public OnFilterScriptExit()
{
    new o = Iter_Free(Objects);
    DestroyObject(o);
    Iter_Clear(Objects);
    return 1;
}
Maybe try this
SiaReyes is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert "for" to "foreach" AaronJ Scripting Help 7 16/06/2016 06:50 PM
Problem: fatal error 100: cannot read from file: "foreach" Ryox175 Scripting Help 3 12/01/2014 04:07 PM
"fatal error 111: user error: "Old foreach.inc files are no longer compatible with YSI."" Edvin Scripting Help 4 26/12/2012 03:09 PM
S32_House.inc(27) : fatal error 100: cannot read from file: "foreach" xXRealLegitXx Scripting Help 2 20/10/2012 08:24 PM


All times are GMT. The time now is 06:31 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.