Page 1 of 1

Detecting the direction the player is facing error

Posted: Sun Feb 26, 2017 6:10 am
by jamisco
One can use the IPlayer facing direction command in order to get the direction a player is facing either left or right... if the player is facing right, the facing direction command returns the integer 1, if the player is facing left it returns -1... but my problem is with the -1 aspect of it ... here is my code

Code: Select all


public void OnUpdate( TriggerArgs Btn )

        {

            IPlayer caller = (IPlayer)Btn.Sender;

            IUser pusher = caller.GetUser();

            IPlayer playr = pusher.GetPlayer();

            Vector2 worldPos = playr.GetWorldPosition() + new Vector2 (5,5);

            int playr_dir =  playr.FacingDirection;

            ProjectileItem rocket = ProjectileItem.UZI;

            if (playr_dir == 1)
            {
                IObject rndPositive_Obj =  Game.GetSingleObjectByCustomId("rndObj_pos"); 
                
                // i have 2 objects in my map that allow me to make the bullet either go left or right, the object in this case is rndObj_pos ...  this  object is in the right side of the map making the bullet go right 

                Vector2 positiveObj_pstn = rndPositive_Obj.GetWorldPosition();

                Game.SpawnProjectile(rocket, worldPos, positiveObj_pstn);

                Game.ShowPopupMessage(positiveObj_pstn.ToString());

            } else if (playr_dir == -1)
            {
                IObject rndNegative_Obj = Game.GetSingleObjectByCustomId("rndObj_neg");

		// i have 2 objects in my map that allow me to make the bullet either go left or right, the object in this case is rndObj_neg ...  this  object is in the leftside of the map making the bullet goes left


                Vector2 negativeObj_pstn = rndNegative_Obj.GetWorldPosition();

                Game.SpawnProjectile(rocket, worldPos, negativeObj_pstn);

                Game.ShowPopupMessage(negativeObj_pstn.ToString());

            }
        }
        
now the following code simply spawns a uzi bullet in the direction the user is facing, but then if i were to turn left and press the button (the button is how i activate the script to shoot the bullet) ... i get this error msg

Code: Select all

Script Error
Error in method 'Btn_Press(TriggerArgs)' in map script. See the exception for more details:
--- Exception ---
Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
   at SFDScript.GameScript.Btn_Press(TriggerArgs Btn)
   
i simply do not understand what that error msg means and i do not exactly understand how to fix my code appropriately according to it... please help me

Thanks. ;) ;)

Re: Detecting the direction the player is facing error

Posted: Sun Feb 26, 2017 3:11 pm
by ShutDownMan
I think you missed so many things, that I'm only going to give you the code and, if you want, try to understand it:

Code: Select all


public void OnUpdate(TriggerArgs Btn)
{
	// Get sender as IPlayer
	IPlayer sender = (IPlayer)Btn.Sender;
	// Get sender's position + offset vector
	Vector2 worldPos = sender.GetWorldPosition() + new Vector2(5, 5);
	// Get sender's facing direction
	int playr_dir = sender.FacingDirection;
	// Projectile to be shoot
	ProjectileItem rocket = ProjectileItem.UZI;

	// Initialize object to be shoot var
	IObject obj_ToShoot = null;

	// If sender is facing right
	if (playr_dir == 1)
	{
		// Get object by customID
		obj_ToShoot = Game.GetSingleObjectByCustomId("rndObj_pos");
	}
	else // If facing left
	{
		//Get object by custom ID
		obj_ToShoot = Game.GetSingleObjectByCustomId("rndObj_neg");
	}
	// Failsafe, if object exists in map
	if (obj_ToShoot != null)
	{
		// Get object to shoot pos
		Vector2 objToShootPosition = obj_ToShoot.GetWorldPosition();

		// Spawn projectile rocket, coming from sender, to objToShootPos
		Game.SpawnProjectile(rocket, worldPos, objToShootPosition);
	}
	else // If not
	{
		// Log in console
		Game.ShowPopupMessage("OBJECT wasn't found in map!");
	}
}

Also, I think you don't have an object with the customID of "rndObj_neg" and that's why it's throwing a System.NullReferenceException

Re: Detecting the direction the player is facing error

Posted: Sun Feb 26, 2017 4:05 pm
by jamisco
Thanks for the code mr ShutDownMan, the problem wasn't an error in any of our code, it was the fact that the object in the left side of the map, had a type of dynamic. Even when i used your code, it said "Object not found" but if i may ask one more question, what does the object being dynamic have to do with anything, i mean i taught making the object dynamic meant the object's location could be moved by in game events such as shoot a rocket and it or something. And why would it throw a System.NullReferenceException?

Re: Detecting the direction the player is facing error

Posted: Sun Feb 26, 2017 4:09 pm
by ShutDownMan
Simple:

If it's dynamic and has no collision (is bg or a InvBlockNoCollision) it will simply fall off the map and be destroyed.

Re: Detecting the direction the player is facing error

Posted: Sun Feb 26, 2017 4:41 pm
by jamisco
Ok, i see, the object was in the far side of the map, the place where a player cant see, it must have probably fallen off