SOLVED - Playerindex on spawngun

Toranks

Active member
If any player spawn an entity with spawngun script, and inside that entity I use the following line:
Code:
void plyr = getentityproperty(self, "playerindex");
"playerindex" always return 0 (first player), instead of the parent player. How can it be solved?

Spawngun script:
Code:
void spawnGun5(void Name, float dx, float dy, float dz, int Num, void Ani)
{ // Spawn gun with ani animation, store it and bind it
   void self = getlocalvar("self");
   void Spawn;
   Spawn = spawn01(Name, dx, dy, 0);
   setentityvar(self, Num, Spawn); // Stores spawned gun to be killed later
   bindentity(Spawn, self, dx, dz, dy, 0, 0); // Bind spawned gun
   performattack(Spawn, openborconstant(Ani));
}
 
If I am reading this right, and the script is part of the sub entity being spawned, you can't do that. There is no player index, because the sub entity is not a player. So you'll always get 0, which just happens to be the same value as a valid player 1.

What exactly are you trying to do?

DC
 
I'm trying to handle the spawned entity with the player controller, using playerkeys. This code, for example:
Code:
@script
	void self = getlocalvar("self");
	void plyr = getentityproperty(self, "playerindex");
	float Vx = getlocalvar("x"+self);
	float Vz = getlocalvar("z"+self);

	if(frame >= 1 )
	{
	  if( playerkeys(plyr, 0, "moveup"))
	  {
	  changeentityproperty(self, "velocity", Vx, -0.5);
	  }
	  if( playerkeys(plyr, 0, "movedown"))
	  {
	  changeentityproperty(self, "velocity", Vx, 0.5);
	  }
	  if( playerkeys(plyr, 0, "moveleft"))
	  {
	  changeentityproperty(self, "velocity", -2, Vz);
	  }
	  if( playerkeys(plyr, 0, "moveright"))
	  {
	  changeentityproperty(self, "velocity", 2, Vz);
	  }
	}
	@end_script
 
yeah he has script in non player entity and wants to enable control with keys of player, but that needs to take into account all players and i dont know but i think its just better to do it all in player entity.
If player 1 and 2 woiuld be in play, which one would control it ?
Just do it in player entity as parent entity to control the chld.
It would be just better if he could explan what he wants to do, there might be other faster and easier way.

This is from other script but might be able to acquire playerindex in enemy animation, try to modify it
Code:
	@script
    void vSelf    = getlocalvar("self");             //Caller.
    void vEntity;                                     //Target entity placeholder.
    int iEntity;                                     //Entity enumeration holder.
    int iType;                                       //Entity type.
    int iMax      = openborvariant("ent_max");       //Entity count.
    int x = getentityproperty(vSelf, "x");
    int Dir = getentityproperty(vSelf, "direction");
    int Ex;
void plyr;
    //Enumerate and loop through entity collection.
    for(iEntity=0; iEntity<iMax; iEntity++){
      vEntity = getentity(iEntity);                 //Get target entity from current loop.
      iType   = getentityproperty(vEntity, "type"); //Get target type.

      //Enemy, NPC & player type?
      if( iType == openborconstant("TYPE_PLAYER")){
 plyr = getentityproperty(iType, "playerindex");
        Ex = getentityproperty(vEntity, "x");

        if(Dir==1 && Ex >= x + 1 && Ex <= x + 220){
          changeentityproperty(vEntity, "position", Ex+2);
        } else if(Dir==0 && Ex <= x - 1 && Ex >= x - 220){
          changeentityproperty(vEntity, "position", Ex-2);
        }
      }
    }
	@end_script
 
Bloodbane said:
So let me confirm this: you want to spawn other entity and control its movement with player's control right?

Yes!

bWWd said:
yeah he has script in non player entity and wants to enable control with keys of player, but that needs to take into account all players and i dont know but i think its just better to do it all in player entity.
If player 1 and 2 woiuld be in play, which one would control it ?
Just do it in player entity as parent entity to control the chld.
It would be just better if he could explan what he wants to do, there might be other faster and easier way.

The player entity simply makes a freespecial that summons a temporary giant vehicle. I want this vehicle to be driven only by the player who summoned it, not always the first player. I don't know how to script INSIDE the invoked entity that recognizes which player invoked it and returns the correct value in playerindex.
Maybe using temporary local variables? But they should be properly erased when the vehicle disappears, and no strange things happen when TWO players summon the vehicle at the same time
 
Ah just as I've thought  8)

Anyways, for your purpose, I recommend using keyall to acquire keypress and keyrelease event from each player and store it in global variable. These variables can then be acquired by entities to act or move based on variable.

If you need example, download Map Demo which has many features. One of the features is Paladin's disc summon. After he summons the disc, he can move it around by pressing direction keys.
 
bWWd said:
parent it

I was looking at the demo that Bloodbane gave me, and I have made these changes on the script inside the spawned vehicle. However, it still returns the value 0 (first player). Did I miss something or did I put something wrong?

script @script
void main()
{
void self = getlocalvar("self");
  void plr = getentityproperty(self, "parent");
void anim = getentityproperty(self,"animationID");
void frame = getentityproperty(self,"animpos");
void dir = getentityproperty(self,"direction");
void Par = getentityproperty(self,"parent");
void plyr = getentityproperty(Par,"playerindex");


    if ( anim == openborconstant("ANI_FOLLOW9") && frame > 1)
{
  if( playerkeys(plyr, 0, "attack") && dir == 1)
  {
  performattack(self, openborconstant("ANI_FOLLOW10"));
  }
  if( playerkeys(plyr, 0, "attack") && dir == 0)
  {
  performattack(self, openborconstant("ANI_FOLLOW11"));
  }
  if( playerkeys(plyr, 0, "moveleft"))
  {
  changeentityproperty(self, "direction", 0);
  }
  if( playerkeys(plyr, 0, "moveright"))
  {
  changeentityproperty(self, "direction", 1);
  }
}
}
@end_script
 
I'm afraid it returns 0 because it's default value not because it refers to player 1. I've tried acquiring that value from spawned entity whose parent is an enemy instead. And the result is 0 even though it's from enemy instead of player.

I had tried doing it your way like above but it failed. I forgot why I concluded that but I decided to use other method which is what I've done in Map Demo.

Oh yes, aside of Paladin, Arthur also uses the system for his NPC summon system.

 
Thank!!! Arthur has a better script. Spawchild. This says:
Code:
void spawnChild(void Name, float dx, float dy, float dz)
{ // Spawn entity and set it as child
   void self = getlocalvar("self");
   void Spawn;

   Spawn = spawn01(Name, dx, dy, dz);
   changeentityproperty(Spawn, "parent", self);
}

I've copied the code to spawngun5:

Code:
void spawnGun5(void Name, float dx, float dy, float dz, int Num, void Ani)
{ // Spawn gun with ani animation, store it and bind it
   void self = getlocalvar("self");
   void Spawn;
   Spawn = spawn01(Name, dx, dy, 0);
   setentityvar(self, Num, Spawn); // Stores spawned gun to be killed later
   bindentity(Spawn, self, dx, dz, dy, 0, 0); // Bind spawned gun
   performattack(Spawn, openborconstant(Ani));
}

This way:

Code:
void spawnChild(void Name, float dx, float dy, float dz, int Num, void Ani)
{ // Spawn entity and set it as child with ani animation, store it and bind it
   void self = getlocalvar("self");
   void Spawn;

   Spawn = spawn01(Name, dx, dy, dz);
   changeentityproperty(Spawn, "parent", self);
   setentityvar(self, Num, Spawn); // Stores spawned gun to be killed later
   bindentity(Spawn, self, dx, dz, dy, 0, 0); // Bind spawned gun
   performattack(Spawn, openborconstant(Ani));
}

Now, if I need to parent any spawn script, I only need to add this line:
  changeentityproperty(Spawn, "parent", self);

And all the scripts inside the spawned unit can use this code and playerindex return the correct value:
  void Par = getentityproperty(self,"parent");
  void plyr = getentityproperty(Par,"playerindex");


Is very simple, as bWWd says before. What happens is that I did not know that it was necessary to add parent property to a spawn, I thought that by default if you spawn from an entity, the parent is that unit.
 
Is very simple, as bWWd says before. What happens is that I did not know that it was necessary to add parent property to a spawn, I thought that by default if you spawn from an entity, the parent is that unit.
If you use a native function to spawn it (like knife), yes. But not by script.
With script you have way more control, but you have to handle that control.  "With great powers..."

If you spawn a projectile with KNIFE and set it to hurt players, it won't hurt the player who spawns it, thanks to the "owner" entity property. But it you spawn it by script, it will hurt you too.

Plus, its not wrong that you need to set the parent when you use script, because sometimes an entity doesn't have a parent at all - for example, if you spawn an entity at a level.
 
Back
Top Bottom