Better way
#1

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;
}
Reply
#2

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
Reply
#3

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
Reply
#4

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.
Reply
#5

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

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

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?
Reply
#8

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.
Reply
#9

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
Reply
#10

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
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)