about callback hook speed..
#1

as a modular programmer, I use hook erverywhere, but something still confused me. speed.
before use a hook, my callback just like this:

Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
	switch(dialogid) {
		case DIALOG_ID_A : return RE_DIALOG_A(playerid, response, listitem, inputtext);
		case DIALOG_ID_B : return RE_DIALOG_B(playerid, response, listitem, inputtext);
		case DIALOG_ID_C : return RE_DIALOG_C(playerid, response, listitem, inputtext);
		case DIALOG_ID_D : return RE_DIALOG_D(playerid, response, listitem, inputtext);
		case DIALOG_ID_E : return RE_DIALOG_E(playerid, response, listitem, inputtext);
		case DIALOG_ID_F : return RE_DIALOG_F(playerid, response, listitem, inputtext);
	}
	return 1;
}
if player response a dialog, the callback match one condition and return.
now, we use hook and however it will run every hook-callback.
if I hooked 100 times of this callback, it will just loop 100 times.

so, I cant think a method to resolve this problem.
how you guys code with hook?
Reply
#2

What loop are u talking about??
Reply
#3

This is not hooking in the normal sense and it is fact encouraged to do this because it improves readability. A single function (including callbacks) ideally consist of less than 20 lines.
Reply
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
This is not hooking in the normal sense and it is fact encouraged to do this because it improves readability. A single function (including callbacks) ideally consist of less than 20 lines.
in fact, I use the normal hook style right now. Let me take an example.

PHP код:
isPlayerInVehicleA() {
    if(
IsPlayerInVehicle(playeridvehicleid_A)) {
        
// do something...
        
return true;
    }
    return 
false;
}
isPlayerInVehicleB() {
    if(
IsPlayerInVehicle(playeridvehicleid_B)) {
        
// do something...
        
return true;
    }
    return 
false;

PHP код:
public OnPlayerStateChange(playeridnewstateoldstate) {
    
#if defined A_OnPlayerStateChange
        
A_OnPlayerStateChange(playeridnewstateoldstate);
    
#endif
    
if(newstate == PLAYER_STATE_DRIVER)
        
isPlayerInVehicleA(playerid);
}
#if defined _ALS_OnPlayerStateChange
    #undef OnPlayerStateChange
#else
    #define _ALS_OnPlayerStateChange
#endif
#define OnPlayerStateChange A_OnPlayerStateChange
#if defined A_OnPlayerStateChange
    
forward A_OnPlayerStateChange(playeridnewstateoldstate);
#endif
public OnPlayerStateChange(playeridnewstateoldstate) {
    
#if defined B_OnPlayerStateChange
        
B_OnPlayerStateChange(playeridnewstateoldstate);
    
#endif
    
if(newstate == PLAYER_STATE_DRIVER)
        
isPlayerInVehicleB(playerid)
}
#if defined _ALS_OnPlayerStateChange
    #undef OnPlayerStateChange
#else
    #define _ALS_OnPlayerStateChange
#endif
#define OnPlayerStateChange B_OnPlayerStateChange
#if defined B_OnPlayerStateChange
    
forward B_OnPlayerStateChange(playeridnewstateoldstate);
#endif 
This callback will be called twice, and after detect player that is in Vehciel A, then it will detect once again that is player in Vehicle B.


But in my style, it will be:

if player in vehicle A then done this callback, and no detect next.

PHP код:
public OnPlayerStateChange(playeridnewstateoldstate) {
    if(
newstate == PLAYER_STATE_DRIVER)
        if(
isPlayerInVehicleA(playerid)) return;
        if(
isPlayerInVehicleB(playerid)) return;
    return 
1;



so..... I mean, why people like using hook with his main function Modular Programming
https://sampforum.blast.hk/showthread.php?tid=597338

this tutorial introduce the hook way to make a gamemode.
but sorry i cant see anymore faster than my style.

I think hook should be used in a include.


so, my way is proper ?? thx.
Reply
#5

EDIT.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)