SA-MP Forums Archive
[Question] How to share the same function? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP (https://sampforum.blast.hk/forumdisplay.php?fid=3)
+--- Forum: General (https://sampforum.blast.hk/forumdisplay.php?fid=13)
+--- Thread: [Question] How to share the same function? (/showthread.php?tid=574171)



[Question] How to share the same function? - Logofero - 14.05.2015

There are 2 files included:

test1.inc
PHP Code:
/*
// imitation lack of function
#if !defined  debug_1
stock debug_1() {
    print("debug file 1");
}
#endif*/ 
test2.inc
PHP Code:
#if !defined  debug_1
stock debug_1() {
    print(
"debug file 2");
}
#endif 
gamemode:
PHP Code:
#include <a_samp>
#include <test1> 
#include <test2>
main()
{
    print(
"\n----------------------------------");
    print(
" Blank Gamemode by your name here");
    print(
"----------------------------------\n");
    
    print(
"sta");
    
debug_1();
    print(
"eof");

One function debug_1();

Question: What are the methods of using directives? Condition: You can not delete function physically. One of the functions can be commented out and must operate the other

My method for some reason, causes an error:
HTML Code:
[12:35:10] sta
[12:35:10] Script[gamemodes/new.amx]: Run time error 6: "Invalid instruction"
Add: Method found. Decided:

test1.inc > If not available
PHP Code:
#if !defined  debug_1
#define debug_1 new_debug_1
stock debug_1() {
    print(
"debug file 1");
}
#endif 
test2.inc < used functions
PHP Code:
#if !defined  debug_1
#define debug_1 new_debug_1
stock debug_1() {
    print(
"debug file 2");
}
#endif 
gamemode:
PHP Code:
#include <a_samp>
#include <test1> //faworite functions
#include <test2> //alternative functions
main()
{
    print(
"\n----------------------------------");
    print(
" Blank Gamemode by your name here");
    print(
"----------------------------------\n");
    
    print(
"sta");
    
debug_1();
    print(
"eof");

Print if all included:
Code:
sta
debug file 1
eof
Print if one #include <test2> included:
Code:
sta
debug file 2
eof



Re: [Question] How to share the same function? - ikkentim - 14.05.2015

You can use states: https://sampforum.blast.hk/showthread.php?tid=570939


Re: [Question] How to share the same function? - Logofero - 14.05.2015

The use of new and alternative functions. Method found. Decided

test1.inc > If not available
PHP Code:
#if !defined  debug_1
#define debug_1 new_debug_1
stock debug_1() {
    print(
"debug file 1");
}
#endif 
test2.inc < used functions
PHP Code:
#if !defined  debug_1
#define debug_1 new_debug_1
stock debug_1() {
    print(
"debug file 2");
}
#endif 
gamemode:
PHP Code:
#include <a_samp>
#include <test1> //faworite functions
#include <test2> //alternative functions
main()
{
    print(
"\n----------------------------------");
    print(
" Blank Gamemode by your name here");
    print(
"----------------------------------\n");
    
    print(
"sta");
    
debug_1();
    print(
"eof");

Print if all included:
Code:
sta
debug file 1
eof
Print if one #include <test2> included:
Code:
sta
debug file 2
eof



Respuesta: [Question] How to share the same function? - admantis - 14.05.2015

Use y_hooks included in the YSI 4 library (documentation). This only works for native callbacks, if you want to do it with custom callbacks use an alternative method, such as naming them differently or ALS hooking.