SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 21/01/2020, 10:31 PM   #1
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 510
Reputation: 15
Question Comparing names in an enum

In short, I'm working on a garage feature for my apartment system. Currently the command is /entergarage and they must be in the checkpoint outside their garage inside a vehicle. The loop is to check if their name matches with any of the names in the apartments enum. This would work as a player is only allowed one apartment at a time.

pawn Code:
foreach(new i : Apartments)
{
    if(strcmp(Name[playerid], aptInfo[i][apt_owner], true) == 0)
    {
        Error(playerid, "You do not own an apartment here");
        return 1;
    }
    else
    {
        //enter garage
    }
}

However, it does not allow them to enter the garage. Has anyone got any suggestions?
FunnyBear is offline   Reply With Quote
Old 22/01/2020, 01:33 AM   #2
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Location: 🇩🇪
Posts: 1,775
Reputation: 619
Default Re: Comparing names in an enum

strcmp returns 0 if the strings match, so you have to switch the enter garage and error message code (or change == 0 to != 0).
NaS is offline   Reply With Quote
Old 22/01/2020, 12:31 PM   #3
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 510
Reputation: 15
Default Re: Comparing names in an enum

Quote:
Originally Posted by NaS View Post
strcmp returns 0 if the strings match, so you have to switch the enter garage and error message code (or change == 0 to != 0).
If I do this, it puts me into someone else's apartment.
FunnyBear is offline   Reply With Quote
Old 22/01/2020, 04:10 PM   #4
ShadowMortar
Huge Clucker
 
ShadowMortar's Avatar
 
Join Date: Jul 2016
Location: Croatia
Posts: 279
Reputation: 56
Default Re: Comparing names in an enum

Try this:
PHP Code:
#define         equal(%0,%1)     !strcmp(%0,%1,false) && strlen(%0) == strlen(%1)

foreach(new Apartments)
{
    if( !
equalNameplayerid ], aptInfo[i][apt_owner] ) )
    {
        
Error(playerid"You do not own an apartment here");
        return 
1;
    }
    else
    {
        
//enter garage
    
}

__________________
Quote:
Originally Posted by CheezIt View Post
Follow your heart and own style.
Feel free to PM me if you need help.
Quote:
Originally Posted by Pottus View Post
That is perfectly fine, I'm just pointing out where you made mistakes what does hearing "good job" get you? Absolutely nothing. You need to hear the problems otherwise you'll keep making the same mistakes
ShadowMortar is offline   Reply With Quote
Old 22/01/2020, 04:25 PM   #5
YouShootIDodge
Big Clucker
 
Join Date: May 2019
Posts: 63
Reputation: 14
Default Re: Comparing names in an enum

Just what NaS said, if you're confused, this is what the application looks like:

Code:
foreach(new i : Apartments)
{
    if(strcmp(Name[playerid], aptInfo[i][apt_owner], true) != 0)
    {
        Error(playerid, "You do not own an apartment here");
        return 1;
    }
    else
    {
        //enter garage
    }
}
YouShootIDodge is offline   Reply With Quote
Old 22/01/2020, 04:29 PM   #6
DRIFT_HUNTER
High-roller
 
Join Date: Oct 2009
Posts: 2,135
Reputation: 215
Default Re: Comparing names in an enum

strcmp function is returning 0 if both strings match, or one of them is empty. So first make sure that apt_owner is not empty before you compare it with player name.
DRIFT_HUNTER is offline   Reply With Quote
Old 22/01/2020, 05:02 PM   #7
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 510
Reputation: 15
Default Re: Comparing names in an enum

Still does not work. Here's what I've got.

pawn Code:
foreach(new i : Apartments)
{
    if(strcmp(Name[playerid], aptInfo[i][apt_owner], true) != 0)
    {
        Error(playerid, "You do not own an apartment here");
        return 1;
    }
    else
    {
        CurrentAptID[playerid] = i;
        new vehicleid = GetPlayerVehicleID(playerid);
        SetVehiclePos(vehicleid, 1414.5531, -1213.1821, 151.7402);
        SetVehicleZAngle(vehicleid, 86.1254);
        LinkVehicleToInterior(vehicleid, 1);
        SetVehicleVirtualWorld(GetPlayerVehicleID(playerid), aptInfo[i][apt_id]);

        SetPlayerPos(playerid, 1414.5531, -1213.1821, 151.7402);
        SetPlayerInterior(playerid, 0);
        SetPlayerVirtualWorld(playerid, 0);
        PutPlayerInVehicle(playerid, vehicleid, 0);

        SendClientMessage(playerid, COLOR_HOUSE, "Apartment: {FFFFFF}You have successfully entered your garage, use /exitgarage to leave.");

        SetPlayerInterior(playerid, 1);
        SetPlayerVirtualWorld(playerid, aptInfo[i][apt_id]);
        TogglePlayerControllable(playerid, 1);
        return 1;
    }
}

Also apt_owner wouldn't be empty as unowned houses are set to "None"
FunnyBear is offline   Reply With Quote
Old 22/01/2020, 05:38 PM   #8
SiaReyes
Gangsta
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 622
Reputation: 57
Default Re: Comparing names in an enum

Code:
foreach(new i : Apartments) if(checkpointid == aptInfo[i][apt_cp]) // Check if player is in a garage CP. and get it's ID

if(!strcmp(Name[playerid], aptInfo[i][apt_owner], true)) return Error(playerid, "You do not own an apartment here");
SiaReyes is offline   Reply With Quote
Old 22/01/2020, 06:00 PM   #9
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 510
Reputation: 15
Default Re: Comparing names in an enum

Quote:
Originally Posted by SiaReyes View Post
Code:
foreach(new i : Apartments) if(checkpointid == aptInfo[i][apt_cp]) // Check if player is in a garage CP. and get it's ID

if(!strcmp(Name[playerid], aptInfo[i][apt_owner], true)) return Error(playerid, "You do not own an apartment here");
I've already tried this. Thank you, though.
FunnyBear is offline   Reply With Quote
Old 22/01/2020, 06:12 PM   #10
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,229
Reputation: 205
Default Re: Comparing names in an enum

The problem is that you return inside the loop which breaks the next iterations.

- You own apartment 3.
- Loop starts, you do not own the first apartment so it stops the loop.

You have to let the loop finish otherwise it is undetermined. Depending on the code execution that takes place after the loop (if any), there are two ways:
pawn Code:
foreach(new i : Apartments)
{
    if(strcmp(Name[playerid], aptInfo[i][apt_owner], true) == 0)
    {
        // owning apartment
        return 1; // break out of the function
    }
}

// not owning an apartment
pawn Code:
new bool: owning_apartment;

foreach(new i : Apartments)
{
    if(strcmp(Name[playerid], aptInfo[i][apt_owner], true) == 0)
    {
        // owning apartment

        owning_apartment = true;
        break; // break out of the loop only
    }
}

if (!owning_apartment)
{
    // not owning an apartment
}
__________________
Calisthenics 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
Storing in a ENUM interfears with another enum justjamie Scripting Help 9 14/08/2016 07:20 AM
Pawn: enum type in enum sagosagi1 Scripting Help 6 19/05/2014 08:34 AM
Multiple instances of an enum inside an enum Enforcer501 Scripting Help 4 18/06/2013 02:31 PM
[HELP] Comparing names XxCozzaxX Help Archive 4 08/02/2011 02:23 PM
[Include] ALL model names (and bone names) Slice Includes 14 26/11/2010 07:48 PM


All times are GMT. The time now is 02:26 PM.


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