SA-MP Forums Archive
Better way - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Better way (/showthread.php?tid=619789)



Better way - GoldenLion - 22.10.2016

Hello, I need to create a function that gets an item's model so I've got a question now: is there a better or faster way to do a thing like this below?
Код:
GetItemModel(item[])
{
	new model;
	
	if (!strcmp(item, "Apple")) model = ..;
	else if (!strcmp(item, "Orange")) model = ..;
	else if (!strcmp(item, "Bottle of Water")) model = ..;
	
	...etc, going to have like 20 of these

        return model;
}



Re: Better way - Dayvison_ - 22.10.2016

PHP код:
new global_model;
GetItemModel(item[])
{
    static function[
32];
    
format(function, sizeof(function), "it_model@%s"item);
    
CallLocalFunction(function, "");
    return 
global_model;
}
#define AddModel:%0(%1) forward it_model@%0(); public it_model@%0() {global_model = %1; }
AddModel:Apple(1)
AddModel:Orange(2
Is a way, if have many models strcmps all cases can be slower. Otherwise you can use y_stringhash


Re: Better way - Kaliber - 22.10.2016

You can do it like this:

PHP код:
GetItemModel(const item[])
{
    
enum e_loc_store
    
{
        
e_loc_Name[16],
        
e_loc_id
    
};
    static const 
store[][e_loc_store] = {
        {
"Apfel"999},
        {
"Orange"1337},
        {
"Erdbeere"12}
    };
    for(new 
ii<sizeof(store); i++)
    {
        if(!
strcmp(store[i][e_loc_Name], item)) return store[i][e_loc_id];
    }
    return -
1;

//Edit:
@Day_:
Better would be:

PHP код:
GetItemModel(const item[]) 

    new function[
32] = "it_model@";
    
strcat(function, item);
    return 
CallLocalFunction(function, "");

#define AddModel:%0(%1) forward it_model@%0(); public it_model@%0(){return %1;} 
AddModel:Apple(1
AddModel:Orange(2



Re: Better way - GoldenLion - 22.10.2016

Thank you guys. Kaliber's one looks easier, but which one would be faster? For some reason I care about the speed right now, if I didn't I would use if and else if.


Re: Better way - Kaliber - 22.10.2016

If you have more items, Day_'s way is faster


Re: Better way - GoldenLion - 22.10.2016

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
If you have more items, Day_'s way is faster
Alright, thank you.


Re: Better way - GoldenLion - 22.10.2016

OK, I tried Day_'s way, it works perfectly. But I can't add items with spaces (e.g Bottle of Water). How could I make it work with spaces?


Re: Better way - Konstantinos - 22.10.2016

I think you make things more complicated when you can use something like this:
pawn Код:
enum
{
    ITEM_APPLE,
    ITEM_ORANGE,
    ITEM_BOTTLE_OF_WATER,
    ...
};
then you can use itemid as an integer which would make everything much easier, even in using a switch statement to retrieve the item's name or model etc.


Re: Better way - GoldenLion - 22.10.2016

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
I think you make things more complicated when you can use something like this:
pawn Код:
enum
{
    ITEM_APPLE,
    ITEM_ORANGE,
    ITEM_BOTTLE_OF_WATER,
    ...
};
then you can use itemid as an integer which would make everything much easier, even in using a switch statement to retrieve the item's name or model etc.
Thank you for your suggestion, but I guess I'll stick to the current way. :P


Re: Better way - Gammix - 22.10.2016

You are ignoring the fastest and the easiest method:
Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
I think you make things more complicated when you can use something like this:
pawn Код:
enum
{
    ITEM_APPLE,
    ITEM_ORANGE,
    ITEM_BOTTLE_OF_WATER,
    ...
};
then you can use itemid as an integer which would make everything much easier, even in using a switch statement to retrieve the item's name or model etc.
Here's an example:
pawn Код:
enum
{
    ITEM_APPLE,
    ITEM_ORANGE,
    ITEM_BOTTLE_OF_WATER,
    ...
};

new const ITEM_MODEL[] =
{
    19000, // ITEM_APPLE
    19001, // ITEM_ORANGE
    19002, // ITEM_BOTTLE_OF_WATER
    ....
};

GetItemModel(itemid)
{
    return (itemid < 0 || itemid >= sizeof (ITEM_MODEL)) ? (-1) : (ITEM_MODEL[itemid]);
}
// exmaple: GetItemModel(ITEM_APPLE) ---will return 19000