SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 24/12/2019, 06:34 PM   #1
Khodafez
Little Clucker
 
Join Date: Sep 2019
Location: Spain
Posts: 16
Reputation: 0
Default How to write less lines

In my inventory system, users can have 5 objects in their pocket, and I have written their names 6 times, for the 5 slots and the reamining for what they have in their hands. Like this:

Code:
new ObjectOneName[80];
if(PlayerInfo[playerid][ObjectOne] == 0) { ObjectOneName = "-"; }
else if(PlayerInfo[playerid][ObjectOne] == 1) { ObjectOneName = "Phone"; }
else if(PlayerInfo[playerid][ObjectOne] == 2) { ObjectOneName = "Cigarette"; }
else if(PlayerInfo[playerid][ObjectOne] == 3) { ObjectOneName = "Pistol"; }
new ObjectTwoName[80];
if(PlayerInfo[playerid][ObjectTwo] == 0) { ObjectTwoName = "-"; }
else if(PlayerInfo[playerid][ObjectTwo] == 1) { ObjectTwoName = "Phone"; }
else if(PlayerInfo[playerid][ObjectTwo] == 2) { ObjectTwoName = "Cigarette"; }
else if(PlayerInfo[playerid][ObjectTwo] == 3) { ObjectTwoName = "Pistol"; }
There are way more objects, but I write only these here in order not to overwhelm any possible helper. My problem is, I have written their names too many times, my question is: is it possible to write their names just once? And link them with the slots from 1 to 5.
Khodafez is offline   Reply With Quote
Old 24/12/2019, 07:00 PM   #2
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 939
Reputation: 165
Default Re: How to write less lines

An array is what you need, for both the object names and objects slots. Avoid [ObjectOne], [ObjectTwo] etc. and instead make it [Object][x]
pawn Code:
static const ObjectNames[][] =
{
    "-",            // slot 0
    "Phone",        // slot 1
    "Cigarette",    // slot 2
    "Pistol"        // slot 3
};
Validate the value and access the array directly.
__________________
Calisthenics is online now   Reply With Quote
Old 25/12/2019, 06:00 AM   #3
Mobtiesgangsa
Big Clucker
 
Mobtiesgangsa's Avatar
 
Join Date: Oct 2016
Location: The Strip
Posts: 146
Reputation: 16
Default Re: How to write less lines

loops, switch statements, arrays etc...
__________________
Under Development providing MySQL Features for Mad's gamemode with other
modifications.
Mobtiesgangsa is offline   Reply With Quote
Old 25/12/2019, 10:18 AM   #4
Khodafez
Little Clucker
 
Join Date: Sep 2019
Location: Spain
Posts: 16
Reputation: 0
Default Re: How to write less lines

Thank you for replying and helping me. I have one final question, how could I show to the user the object names when they use the command /inventory:

Code:
SendClientMessage(playerid, COLOR_GREEN, "| Inventory |");
format(string, sizeof(string), "[1] %s", ObjectOneName);
SendClientMessage(playerid, COLOR_WHITE, string);
format(string, sizeof(string), "[2] %s", ObjectTwoName);
SendClientMessage(playerid, COLOR_WHITE, string);
SendClientMessage(playerid, COLOR_GREEN, "-----");
I would like to learn how to use the array you sent to show the users the correct name of each object, and I wanna get rid of the excessive amount of lines that repeat the names many times.
Khodafez is offline   Reply With Quote
Old 25/12/2019, 11:51 AM   #5
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 939
Reputation: 165
Default Re: How to write less lines

As long as inventory slots are stored in an array, you can use a loop to display them and get rid of the repeated lines. Here is an example:
pawn Code:
#define MAX_INVENTORY_SLOTS 5

enum ...
{
    ...,
    Object[MAX_INVENTORY_SLOTS],
    ...
};

new PlayerInfo[MAX_PLAYERS][...];
pawn Code:
SendClientMessage(playerid, COLOR_GREEN, "| Inventory |");

new inv_slot;
for (new i = 0; i < MAX_INVENTORY_SLOTS; i++)
{
    inv_slot = PlayerInfo[playerid][Object][i];
    if (0 <= inv_slot < sizeof (ObjectNames)) // value in bounds of `ObjectNames` array
    {
        // array index starts from 0 so i + 1 to start from 1 in the message
        format(string, sizeof (string), "[%d] %s", i + 1, ObjectNames[inv_slot]);
        SendClientMessage(playerid, COLOR_WHITE, string);
    }
}

SendClientMessage(playerid, COLOR_GREEN, "-----");
__________________
Calisthenics is online now   Reply With Quote
Old 25/12/2019, 06:44 PM   #6
Khodafez
Little Clucker
 
Join Date: Sep 2019
Location: Spain
Posts: 16
Reputation: 0
Default Re: How to write less lines

Thank you so much, I've taken your time and you created the code, giving reputation to your post in return is the least I can do.

How can I request this post to be closed/archived?
Khodafez 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
will long lines in script 100k lines slow down gamemode PSYCHOBABYKILLA Server Support 2 25/04/2015 12:31 PM
[Tutorial] Using y_xml to write/read xml files. [Another way to write CFG data but better] Ahmad45123 Tutorials 8 13/03/2015 03:41 AM
Not able to write anything Kyle_Olsen Server Support 2 11/11/2010 05:25 PM
Why does it write two lines to a file instead of one? Nakash Help Archive 0 27/03/2010 11:54 PM
How Do You Write This? sidhu123 Everything and Nothing 2 12/09/2009 05:05 AM


All times are GMT. The time now is 10:23 AM.


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