Solved Negative values to TossEntity Function

Question that is answered or resolved.

Crimsondeath

Active member
Hi to everyone,
Is there a way to send a negative value from a variable with random value to the tossentity function?

Code:
void tosserRandom(void Bomb, float dx, float dy, float dz, int Negative)
{
  void self = getlocalvar("self");
  void candamage  = getentityproperty(self, "candamage");
  int Direction = getentityproperty(self, "direction");
  int x = getentityproperty(self, "x");
  int y = getentityproperty(self, "a");
  int z = getentityproperty(self, "z");
  float Vx = rand()%9+9;
  float Vy = 2;
  float Vz = rand()%95+95;
  void Shot;

  if (Vx < 1){
    Vx = 1/10;
  }else{
    Vx = Vx/10;
  }
  if (Negative == 1){
    Vz = Vz/100;
    Vz = -Vz; //negative value?
  }else{
    Vz = Vz/100;
  }

  if (Direction == 0){ //Is entity facing left?
    dx = -dx; //Reverse X direction to match facing
  }

  Shot = projectile(Bomb, x+dx, z+dz, y+dy, Direction, 0, 1, 0);
  tossentity(Shot, Vy, Vx, Vz); // <----- here :(
  changeentityproperty(Shot, "speed", Vx);
  changeentityproperty(Shot, "owner", self);
  changeentityproperty(Shot, "candamage", candamage);
}

The script work fine when I input the number manually:

Code:
...
  Shot = projectile(Bomb, x+dx, z+dz, y+dy, Direction, 0, 1, 0);
  tossentity(Shot, Vy, Vx, -0.65); // the bomb fly away from the screen :D
  changeentityproperty(Shot, "speed", Vx);
  changeentityproperty(Shot, "owner", self);
  changeentityproperty(Shot, "candamage", candamage);
...

But didn't work if I send it from the variable :( :

Code:
...
  float Vx = rand()%9+9;
  float Vy = 2;
  float Vz = rand()%95+95; // From here x.x
  void Shot;
...

The values I need in tossentity Z are the ones from -0.95 to 0.
Everything else is working fine.

EDIT: I miss miscategorize this post can some mod move it to Ask and Answer? :( Sorry.
 
Last edited:
Solution
float Vz = rand()%95+95;
Because this line will never output a negative value.
When you do rand()%95 it will output a number from -95 to 95. But you are adding 95 just after that.
So, say, if the result was "-62", now it will be "33". Even if the result was "-95" originally, now it will be 0 (-95+95)

But what I found strange is this part:

C-like:
if (Negative == 1){
    Vz = Vz/100;
    Vz = -Vz; //negative value?
  }else{
    Vz = Vz/100;
  }

Let's say the result from rand() was 65. Them you divided it by 100, which gives you 0.65 and, in the end, you made that value negative.

So unless I miss something, this should work already.

Just for debugging propouses, add those 2 lines to your code and watch the log.txt...
float Vz = rand()%95+95;
Because this line will never output a negative value.
When you do rand()%95 it will output a number from -95 to 95. But you are adding 95 just after that.
So, say, if the result was "-62", now it will be "33". Even if the result was "-95" originally, now it will be 0 (-95+95)

But what I found strange is this part:

C-like:
if (Negative == 1){
    Vz = Vz/100;
    Vz = -Vz; //negative value?
  }else{
    Vz = Vz/100;
  }

Let's say the result from rand() was 65. Them you divided it by 100, which gives you 0.65 and, in the end, you made that value negative.

So unless I miss something, this should work already.

Just for debugging propouses, add those 2 lines to your code and watch the log.txt

C-like:
void tosserRandom(void Bomb, float dx, float dy, float dz, int Negative)
{
  void self = getlocalvar("self");
  void candamage  = getentityproperty(self, "candamage");
  int Direction = getentityproperty(self, "direction");
  int x = getentityproperty(self, "x");
  int y = getentityproperty(self, "a");
  int z = getentityproperty(self, "z");
  float Vx = rand()%9+9;
  float Vy = 2;
  float Vz = rand()%95+95;
  void Shot;

  if (Vx < 1){
    Vx = 1/10;
  }else{
    Vx = Vx/10;
  }
  if (Negative == 1){
    Vz = Vz/100;
    Vz = -Vz; //negative value?
  }else{
    Vz = Vz/100;
  }
  log("Initial Vel Z: "+Vz+"\n"); //<---- add this
  if (Direction == 0){ //Is entity facing left?
    dx = -dx; //Reverse X direction to match facing
  }

  Shot = projectile(Bomb, x+dx, z+dz, y+dy, Direction, 0, 1, 0);
  tossentity(Shot, Vy, Vx, Vz); // <----- here :(
  log("Proccessed Vel Z: "+Vz+"\n"); //<---- add this
  changeentityproperty(Shot, "speed", Vx);
  changeentityproperty(Shot, "owner", self);
  changeentityproperty(Shot, "candamage", candamage);
}
 
Last edited:
Solution
Thanks @O Ilusionista , I didn't know about "log()" function, I use it in the whole process:
Code:
 Free Ram: 7310528512 Bytes
 Used Ram: 207867904 Bytes
Total sprites mapped: 3766

Original: 61
Turn Negative: -61
Division Result: 0
Initial Vel Z: 0
Proccessed Vel Z: 0
Original: 158
Turn Negative: -158
Division Result: -1
Initial Vel Z: -1
Proccessed Vel Z: -1
Original: 66
Turn Negative: -66
Division Result: 0
Initial Vel Z: 0
Proccessed Vel Z: 0

************ Shutting Down ************

So the problem was I used an integer value to divide a float value xdddd

Putting the code like this solved the problem xd:
Code:
if (Negative == 1){
    Vz = Vz/100.00; // float value xd
    Vz = -Vz; //negative value?
  }else{
    Vz = Vz/100.00; // float value xd
  }

Thanks :D
 
Back
Top Bottom