This forum is locked and will eventually go offline. If you have feedback to share you can find us in our Discord channel "MythoLogic Interactive" https://discord.gg/nECKnbT7gk

Forum rules

Detecting the direction the player is facing error

Here you can find answered ScriptAPI topics.
Forum rules
By using the forum you agree to the following rules.
Locked
jamisco
Superfighter
Superfighter
Posts: 67
Joined: Sun Nov 06, 2016 11:34 pm
Title: Da God
SFD Account: Jamisco
SFD Alias: Jamisco
Started SFD: either late 2015 or early 2016
Location: Somewhere in the east of the United states
Gender:
Age: 105

Detecting the direction the player is facing error

Post by jamisco » Sun Feb 26, 2017 6:10 am

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. ;) ;)
0 x
Is it better to be feared or respected... please, is it too much to ask for both?

User avatar
ShutDownMan
Fighter
Fighter
Posts: 32
Joined: Sat Mar 19, 2016 7:17 pm
Title: Yeah, science!
SFD Alias: ShutDownMan, Devil Shut
Started SFD: 1.2.something
Location: Hu3, Brazil
Gender:
Age: 26
Contact:

Post by ShutDownMan » Sun Feb 26, 2017 3:11 pm

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
1 x
~ShutDownMan

jamisco
Superfighter
Superfighter
Posts: 67
Joined: Sun Nov 06, 2016 11:34 pm
Title: Da God
SFD Account: Jamisco
SFD Alias: Jamisco
Started SFD: either late 2015 or early 2016
Location: Somewhere in the east of the United states
Gender:
Age: 105

Post by jamisco » Sun Feb 26, 2017 4:05 pm

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?
0 x
Is it better to be feared or respected... please, is it too much to ask for both?

User avatar
ShutDownMan
Fighter
Fighter
Posts: 32
Joined: Sat Mar 19, 2016 7:17 pm
Title: Yeah, science!
SFD Alias: ShutDownMan, Devil Shut
Started SFD: 1.2.something
Location: Hu3, Brazil
Gender:
Age: 26
Contact:

Post by ShutDownMan » Sun Feb 26, 2017 4:09 pm

Simple:

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

jamisco
Superfighter
Superfighter
Posts: 67
Joined: Sun Nov 06, 2016 11:34 pm
Title: Da God
SFD Account: Jamisco
SFD Alias: Jamisco
Started SFD: either late 2015 or early 2016
Location: Somewhere in the east of the United states
Gender:
Age: 105

Post by jamisco » Sun Feb 26, 2017 4:41 pm

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
0 x
Is it better to be feared or respected... please, is it too much to ask for both?

Locked