AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Constant Expression (https://forums.alliedmods.net/showthread.php?t=27317)

MaximusBrood 04-19-2006 16:28

Constant Expression
 
Why doesn't this work

Code:
new f[2] = {params[0], SOME_DEFINE}

where

Code:
#define SOME_DEFINE 0 new params[1] params[0] = 3

Gives me this error

Code:

error 008: must be a constant expression; assumed zero
---

BONUS QUESTION:

Can you use any kind of pointer in Pawn, exept for the pass by reference when working with functions?

PM 04-19-2006 17:15

Re: Constant Expression
 
Well, the error message basically explains it:
You may only use constant expressions for such initializer lists.
i.e no "normal" variables.
Either use all constants, or first declare the array and then assign the non-constant things separately.



Pointers... well, you could hack together some semi-pointers by mixing up byref and byval with callfunc stuff.

Code:
#include <amxmodx> public helper_getptr(x) {    return x; } public helper_deref_read(&x)   // was that the syntax for byref variables? {    return x; } public helper_deref_assign(&x, val) {    return (x = val); } #define getptr(%1) (callfunc_begin("helper_getptr"),callfunc_push_intrf(%1),callfunc_end()) #define deref_read(%1) (callfunc_begin("helper_deref_read"),callfunc_push_int(%1),callfunc_end()) #define deref_assign(%1,%2) (callfunc_begin("helper_deref_assign"),callfunc_push_int(%1),callfunc_push_int(%2),callfunc_end()) public mwah() {    new hello = 10;        new gaben = getptr(hello);    new hello_pm = deref_read(gaben);    server_print("Should be 10: %d", hello_pm);        deref_assign(gaben, 20);        server_print("Should be 20: %d", hello);    server_print("Should be 10: %d", hello_pm); }

I just hacked this together and haven't tested it (only compile), but you get the idea. So it may not work, maybe I made typos, maybe the comma operator is broken (i doubt that though), whatever. But you get the idea ;)

Of course, this is neither very useful nor effective performance-wise (actually, it'll have pretty awful performance)

EDIT: In fact, it turns out that BAIL has added get_addr_val, get_var_addr, set_addr_val to core. You might want to check them out in the funcwiki. Still I'm leaving my code here because I had some good fun writing it.

MaximusBrood 04-19-2006 17:31

Thanks PM

I was trying to improve performance with a pointer to avoid copying a 256 char string.

I will certainly look for the functions you mentioned.
But first I'll have a good night of sleep :D

PM 04-19-2006 17:36

Quote:

Originally Posted by MaximusBrood
Thanks PM

I was trying to improve performance with a pointer to avoid copying a 256 char string.

I will certainly look for the functions you mentioned.
But first I'll have a good night of sleep :D

:o make it global i guess :)
as arguments, arrays are always passed by reference automatically
maybe you want to share it between plugins? that wouldn't work even with this "pointer" stuff because these are really addresses into the plugin's memory..

Hmm, more info please :)

Twilight Suzuka 04-19-2006 19:21

I'm the one that asked for the address hacking tools XD

The best method for doing this though, would be MemHack, as you can get the actual address.


All times are GMT -4. The time now is 05:01.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.