Dear forum users! In compliance with the new European GDPR regulations, we'd just like to inform you that if you have an account, your email address is stored in our database. We do not share your information with third parties, and your email address and password are encrypted for security reasons.

New to the forum? Say hello in this topic! Also make sure to read the rules.

Object references not set to an instance of an object

Here you can find answered ScriptAPI topics.
Forum rules
By using the forum you agree to the following rules.
Post Reply
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: 103

Object references not set to an instance of an object

Post by jamisco » Thu Jun 29, 2017 1:55 am

I decided to create a new post since the other one was getting out of its original subject
I have the following script, , but when I run the game, I get this error, but the script does get compiled with any errors

Script Error
Error in method 'ActivateTheGunner(CreateTriggerArgs)' 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.ActivateTheGunner(TriggerArgs args)

Code: Select all

 public void ActivateTheGunner(TriggerArgs args)
        {
            IObject Gunner_ID = Game.GetSingleObjectByCustomID("The_Gunner")
            IPlayer TheGunner = Game.GetPlayer(Gunner_ID.UniqueId)
            IPlayer[] Enemies = new IPlayer[] { }
            int i = 0; // used for enemy array
            foreach(IPlayer plyr in Game.GetPlayers())
            {
                PlayerTeam  Plyr_team = plyr.GetTeam()
                if (Plyr_team != TheGunner.GetTeam())
                {
                    Enemies[i] = plyr
                    i++
                }
            }

            Vector2 Gunner_Position = TheGunner.GetWorldPosition()
            Enemy2Attack(Enemies, Gunner_Position)
        }

        public void Enemy2Attack(IPlayer[] Enemies, Vector2 Gunner_Position)
        {
            Vector2[] Enemy_Position = new Vector2[] { }
            int i = 0                              // used for enemy postiion array
            IPlayer enemy2Attack = Enemies[i]
            Vector2[] distances = new Vector2[] { }
            foreach(IPlayer plyr in Enemies)
            {
                Enemy_Position[i] = plyr.GetWorldPosition()
                distances[i] = new Vector2(Vector2.Distance(Enemy_Position[i], Gunner_Position))
                i++
            }
            

            Vector2 A, B, C
            int D = 0
            A = distances[0]
            for (int p = 0; p < distances.Length - 1; p++)
            {
                A = distances[0]
                B = distances[p]

                if (Vector2.Min(A,B) == A)
                {
                    enemy2Attack = Enemies[0]
                    D = 0
                }
                else
                {
                    A = distances[p]
                    enemy2Attack = Enemies[p]
                    D = p
                }
            }
            
            Attack_Enemy(enemy2Attack, Enemy_Position[D])
        } 
 
        public void Attack_Enemy(IPlayer Enemy2Attack, Vector2 Enemy_Position)
        {
            PlayerCommandType Run = PlayerCommandType.Run
            PlayerCommand run2Enemy = new PlayerCommand(Run, Enemy_Position)
        }
        
what is causing the error, I believe all my values are initialized, and nothing should be null

here is a picture of my map editor if it might help

Picture in map editor
0 x
Is it better to be feared or respected... please, is it too much to ask for both?

kirill240
Fighter
Fighter
Posts: 18
Joined: Mon Jan 09, 2017 1:01 pm
SFD Account: Mr.A woman's heart

Post by kirill240 » Thu Jun 29, 2017 12:15 pm

Replace the first lines with this:

Code: Select all

IObjectPlayerSpawnTrigger Gunner_ID = Game.GetSingleObjectByCustomID("The_Gunner") as IObjectPlayerSpawnTrigger;
IPlayer TheGunner = Gunner_ID.CreatePlayer();
1 x

kirill240
Fighter
Fighter
Posts: 18
Joined: Mon Jan 09, 2017 1:01 pm
SFD Account: Mr.A woman's heart

Post by kirill240 » Thu Jun 29, 2017 12:16 pm

The point is that you are trying to get an IPlayer from the PlayerSpawnTrigger object. So this thing does not work. I learned this with the help of this pacing:

Code: Select all

IObject Gunner_ID = Game.GetSingleObjectByCustomID("The_Gunner");IPlayer TheGunner = Game.GetPlayer(Gunner_ID.UniqueId);if (TheGunner == null)Game.CreateDialogue("true", Gunner_ID);
1 x

kirill240
Fighter
Fighter
Posts: 18
Joined: Mon Jan 09, 2017 1:01 pm
SFD Account: Mr.A woman's heart

Post by kirill240 » Thu Jun 29, 2017 12:17 pm

And slightly change the player's environment as in the screenshots:
https://postimg.org/image/jt01jgy3z/
https://postimg.org/image/a65npiskv/
1 x

kirill240
Fighter
Fighter
Posts: 18
Joined: Mon Jan 09, 2017 1:01 pm
SFD Account: Mr.A woman's heart

Post by kirill240 » Thu Jun 29, 2017 12:19 pm

Sorry, if my answer contains too many characters, I get an error sending an answer.
1 x

User avatar
Scraper
Fighter
Fighter
Posts: 22
Joined: Tue Nov 01, 2016 9:06 am
SFD Account: Scrapper
SFD Alias: Scrapper
Started SFD: PreAlpha 1.7.1
Gender:

Post by Scraper » Thu Jun 29, 2017 12:21 pm

What will you use the script for?
0 x

kirill240
Fighter
Fighter
Posts: 18
Joined: Mon Jan 09, 2017 1:01 pm
SFD Account: Mr.A woman's heart

Post by kirill240 » Thu Jun 29, 2017 12:31 pm

jamisco, Do you have a Skype or discord or Telegram or other? I could help you correct all the mistakes so as not to clog the topic and make the code more adequate and beautiful.
1 x

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: 103

Post by jamisco » Thu Jun 29, 2017 4:13 pm

kirill240 wrote:
Thu Jun 29, 2017 12:19 pm
Sorry, if my answer contains too many characters, I get an error sending an answer.
Is the error a getpost/php or something similar?
Also i greatly appreciate the answer, been waiting for like 3 days
0 x
Is it better to be feared or respected... please, is it too much to ask for both?

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: 103

Post by jamisco » Thu Jun 29, 2017 4:15 pm

kirill240 wrote:
Thu Jun 29, 2017 12:31 pm
jamisco, Do you have a Skype or discord or Telegram or other? I could help you correct all the mistakes so as not to clog the topic and make the code more adequate and beautiful.
Yea, just go on discord and look up jamisco
0 x
Is it better to be feared or respected... please, is it too much to ask for both?

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: 103

Post by jamisco » Thu Jun 29, 2017 4:16 pm

Scraper wrote:
Thu Jun 29, 2017 12:21 pm
What will you use the script for?
Planning to create highly advance AI, need experience for a programming job in the future. Btw when i say advanced, i mean advanced
0 x
Is it better to be feared or respected... please, is it too much to ask for both?

kirill240
Fighter
Fighter
Posts: 18
Joined: Mon Jan 09, 2017 1:01 pm
SFD Account: Mr.A woman's heart

Post by kirill240 » Thu Jun 29, 2017 4:20 pm

jamisco wrote:
Thu Jun 29, 2017 4:15 pm
kirill240 wrote:
Thu Jun 29, 2017 12:31 pm
jamisco, Do you have a Skype or discord or Telegram or other? I could help you correct all the mistakes so as not to clog the topic and make the code more adequate and beautiful.
Yea, just go on discord and look up jamisco
Your name should be of this type: Nickname # 0000 in discord
Last edited by kirill240 on Thu Jun 29, 2017 6:56 pm, edited 1 time in total.
0 x

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: 103

Post by jamisco » Thu Jun 29, 2017 6:47 pm

kirill240 wrote:
Thu Jun 29, 2017 4:20 pm
jamisco wrote:
Thu Jun 29, 2017 4:15 pm
kirill240 wrote:
Thu Jun 29, 2017 12:31 pm
jamisco, Do you have a Skype or discord or Telegram or other? I could help you correct all the mistakes so as not to clog the topic and make the code more adequate and beautiful.
Yea, just go on discord and look up jamisco
Your name should be of this type: Nickname # 0000 in discord. Or please add me Kirill#0646
sent you a PM
0 x
Is it better to be feared or respected... please, is it too much to ask for both?

Post Reply