Use value of constant as part of a macro
#1

Hi,

I try to write a macro which does the same with OnPlayerPickupPickUp as for OnPlayerCommandText in zcmd and Co.

I have an enum for all type of pickups (e.g. PICKUPTYPE_HOUSE, PICKUPTYPE_HOTEL, PICKUPTYPE_GASSTATION, ...).

In OnPlayerPickupPickUp I want to call the pickup type function with CallLocalFunction. I'm using "PickupEvent_%d" as the function name (%d = value of PICKUPTYPE_..., e.g. 2 will be "PickupEvent_2").

That was the easy part. But how can I write the macro which contains the forward and public for the pickup type function?

I want to define my pickup type function in this way:
Code:
PickupEvent:HOUSE(playerID, pickupID)
And that should be translated to that:
Code:
forward PickupEvent_1(playerID, pickupID);
public PickupEvent_1(playerID, pickupID)
Any way to make that working?
Reply
#2

pawn Code:
#define PickupEvent:HOUSE(%0,%1)                                                           \
    forward PickupEvent_1(%0,%1);                                                             \
    public PickupEvent_1(%0,%1)
this should do it
Reply
#3

Quote:
Originally Posted by Jonny5
View Post
pawn Code:
#define PickupEvent:HOUSE(%0,%1)                                                           \
    forward PickupEvent_1(%0,%1);                                                             \
    public PickupEvent_1(%0,%1)
this should do it
No, what I mean is to use the value of the constant (e.g. PICKUPTYPE_HOUSE = 1) as part of the function name (e.g. PickupEvent_1)
And PICKUPTYPE_HOUSE should be shortened to HOUSE.
That should work dynamically. So I just want to define the new pickup type in my enum and add the PickupEvent:<newtype>(pickupID, pickupType).
Reply
#4

OH i see let me retry this will edit in a min!

EDIT:

looks like it will need a few marcos to make it work
its kinda out of my skill set im still gonna try but
this is how im learning!

https://sampforum.blast.hk/showthread.php?tid=166680

not sure if i will have a solution for you or not.

EDIT AGAIN:

okay when you say HOUSE should be shorten you'll have to at least make a marco to convert it back to the real name so for ever type you'll need a marco, no biggy
im still trying..
Reply
#5

I think I'm on the right way.

My macro:
Code:
#include <a_samp>

#define MYCONSTANT_A 1
#define MYCONSTANT_B 2
#define MYCONSTANT_C 3

#define MyTest:%0(%1) forward TestFunc_#%0(%1); public TestFunc_#%0(%1)

main(){}

public OnGameModeInit()
{
	AddPlayerClass(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	SetGameModeText("Test GM");
	new funcName[32];
	format(funcName, sizeof(funcName), "TestFunc_%d", MYCONSTANT_B);
	printf("Trying to call %s", funcName);
	CallLocalFunction(funcName, "dd", 123, 456);
	return true;
}


MyTest:MYCONSTANT_B(var1, var2)
{
	printf("Executed MYCONSTANT_B (%d, %d)", var1, var2);
}
Trying to compile that gives me two errors but if I run the pawn preprocessor using the -l command line option of the pawn compiler I find the following in the generated .list file:

Code:
forward TestFunc_#2(var1, var2); public TestFunc_#2(var1, var2)
{
	printf("Executed MYCONSTANT_B (%d, %d)", var1, var2);
}
So I just have to find a way to remove the #. If I remove it in the macro I get that:
Code:
forward TestFunc_MYCONSTANT_B(var1, var2); public TestFunc_MYCONSTANT_B(var1, var2)
{
	printf("Executed MYCONSTANT_B (%d, %d)", var1, var2);
}
Reply
#6

im come up with this
will replace HOUSE with you constant
then the rest
it should work let me know
pawn Code:
#define HOUSE PICKUPTYPE_HOUSE

#define PickupEvent:%0(%1,%2)           \
    forward PickupEvent_%0(%1,%2);      \
    public PickupEvent_%0(%1,%2)
Reply
#7

Quote:
Originally Posted by Jonny5
View Post
im come up with this
will replace HOUSE with you constant
then the rest
it should work let me know
pawn Code:
#define HOUSE PICKUPTYPE_HOUSE

#define PickupEvent:%0(%1,%2)           \
    forward PickupEvent_%0(%1,%2);      \
    public PickupEvent_%0(%1,%2)
lol no

What I want is to convert the value of the constant to the real value (e.g. 2) and use that as part of the function name. See my previous post.

I hope it's clear now.
Reply
#8

yes your using #define for your constant
so it will be replaced at compile time and = to its value not its name!

but your right my code still dont work!

sorry. im still into this now as I want to know too!
Reply
#9

Mhh... lol?

Original code:
Code:
#define MYCONSTANT_A 1
#define MYCONSTANT_B 2
#define MYCONSTANT_C 3

#define MyTest:%0(%1) MyTest2:#%0(%1)
#define MyTest2:#%0(%1) forward TestFunc_ %0(%1)

MyTest:MYCONSTANT_B(var1, var2)
{
	printf("Executed MYCONSTANT_B (%d, %d)", var1, var2);
}
Listing result:
Code:
forward TestFunc_ 2(var1, var2)
{
	printf("Executed MYCONSTANT_B (%d, %d)", var1, var2);
}
That's the correct way... But how can I remove the space before the "2"?

The listing result should look like that:
Code:
forward TestFunc_2(var1, var2)
{
	printf("Executed MYCONSTANT_B (%d, %d)", var1, var2);
}
If I remove the space before "2" the listing result looks like that:
Code:
forward TestFunc_MYCONSTANT_B(var1, var2)
{
	printf("Executed MYCONSTANT_B (%d, %d)", var1, var2);
}
And I know that the code will even not work if the space get's removed because there is just a forward and no public.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)