Create a variable equal to the current value of another variable (create a constant)
#1

Hello.

How can I create a constant y which will be equal to the current value of a variable x?
So y must be equal only to the specific current value of x and must not change along with x further.

So in the specific moment of the game I want to create a constant y equal to the current value of a variable x.

How can I do this?
Many thanks in advance.
Reply
#2

pawn Code:
new x=2;
new y;
y=x;
printf("x==%i | y==%i", x, y);
x=5;
printf("x==%i | y==%i", x, y);
If I'm not wrong x will change its value and y won't.
y is a variable though; does it have to be a constant? If yes, why?

#Edit#:
I've just tried it out and you obviously can also use a constant for that. Code for that:
pawn Code:
new x = 2;
new const y = x;
printf("x==%i | y==%i", x, y);
x=6;
printf("x==%i | y==%i", x, y);
Reply
#3

DeathOnaStick,
thanks, but

pawn Code:
new Float:pos[3],Float:Angle;
GetPlayerPos(i,pos[0],pos[1],pos[2]);
GetPlayerFacingAngle(i,Angle);
new const lesX = pos[0]; //2668
new const lesY = pos[1]; //2669
new const lesZ = pos[2]; //2670
new const lesA = Angle; //2671
and then

pawn Code:
SetPlayerPos(i,lesX,lesY,lesZ);
SetPlayerFacingAngle(i,lesA);

I'm getting

pawn Code:
C:\SAMP\mod1.pwn(2668) : warning 213: tag mismatch
C:\SAMP\mod1.pwn(2669) : warning 213: tag mismatch
C:\SAMP\mod1.pwn(2670) : warning 213: tag mismatch
C:\SAMP\mod1.pwn(2671) : warning 213: tag mismatch
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase

Header size:           5272 bytes
Code size:          1115648 bytes
Data size:          3882756 bytes
Stack/heap size:      16384 bytes; estimated max. usage=4922 cells (19688 bytes)
Total requirements: 5020060 bytes

4 Warnings.

and the game is starting to lag heavily when SetPlayerPos is called with this code and almost hangs my PC.

------------------------------------------------
The thing is that I'm trying to declare variables (or constants) equal to player's current coordinates and then, after changing player's coordinates a few times, set his coordinates back to those.
If I just declare variables equal to x, y, z, they're changing along with player's position and later, when player calls to SetPlayerPos, he teleports right to the same place where he's standing at the moment, not to his previous location...
Reply
#4

Quote:
Originally Posted by WellDone
View Post
DeathOnaStick,
thanks, but

pawn Code:
new Float:pos[3],Float:Angle;
GetPlayerPos(i,pos[0],pos[1],pos[2]);
GetPlayerFacingAngle(i,Angle);
new const lesX = pos[0]; //2668
new const lesY = pos[1]; //2669
new const lesZ = pos[2]; //2670
new const lesA = Angle; //2671
and then

pawn Code:
SetPlayerPos(i,lesX,lesY,lesZ);
SetPlayerFacingAngle(i,lesA);

I'm getting

pawn Code:
C:\SAMP\mod1.pwn(2668) : warning 213: tag mismatch
C:\SAMP\mod1.pwn(2669) : warning 213: tag mismatch
C:\SAMP\mod1.pwn(2670) : warning 213: tag mismatch
C:\SAMP\mod1.pwn(2671) : warning 213: tag mismatch
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase

Header size:           5272 bytes
Code size:          1115648 bytes
Data size:          3882756 bytes
Stack/heap size:      16384 bytes; estimated max. usage=4922 cells (19688 bytes)
Total requirements: 5020060 bytes

4 Warnings.

and the game is starting to lag heavily when SetPlayerPos is called with this code.
To fix the tag missmatch try this:
pawn Code:
new Float:pos[3],Float:Angle;
GetPlayerPos(i,pos[0],pos[1],pos[2]);
GetPlayerFacingAngle(i,Angle);
new const Float:lesX = pos[0]; //2668
new const Float:lesY = pos[1]; //2669
new const Float:lesZ = pos[2]; //2670
new const Float:lesA = Angle; //2671
Reply
#5

Quote:
Originally Posted by DeathOnaStick
View Post
To fix the tag missmatch try this:
pawn Code:
new Float:pos[3],Float:Angle;
GetPlayerPos(i,pos[0],pos[1],pos[2]);
GetPlayerFacingAngle(i,Angle);
new const Float:lesX = pos[0]; //2668
new const Float:lesY = pos[1]; //2669
new const Float:lesZ = pos[2]; //2670
new const Float:lesA = Angle; //2671
Thanks, my bad.
But player still doesn't teleport back to his previous location with this code.
Can you please help me with this?
Reply
#6

What does it do instead of porting them back to their location? Does it do nothing or something weird?
Reply
#7

Quote:
Originally Posted by DeathOnaStick
View Post
What does it do instead of porting them back to their location? Does it do nothing or something weird?
Player just remains at his current position and doesn't teleport back.
Reply
#8

Might I see all code that has to do with it? The problem is probably somewhere more deep.
Reply
#9

Quote:
Originally Posted by DeathOnaStick
View Post
Might I see all code that has to do with it? The problem is probably somewhere more deep.
Please take a look at this thread: https://sampforum.blast.hk/showthread.php?tid=408382
There my problem is described more deeply and with more code.

In that thread I just used playerid instead of i and Tut instead of Les.
So there aren't any problems with mixing variables, just letting you know.


P.S. I already figured out about SetCameraBehindPlayer(i);
It's not the problem though. Camera gets back to player, but player remains at the same location.
Reply
#10

Quote:

"const

new const
MY_CONSTANT[] = {1, 2, 3};
const is not widerly used however it declares a variable which can not be modified by code. There are a few uses for this - functions with const array parameters can sometimes be compiled more efficiently or you may want something like a define but which is an array. const is a modifier, it must go with new or another variable declarator. If you try modify a const variable the compiler will complain."

Also why are you making 8 variables when you can make just 4..
Just make the variables for getpos and then use them to set his position..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)