Log in to reply
 

How could I bring this variable over?



  • using GTA;
    using System;
    using System.Windows.Forms;

    public class example : Script
    {
    public example()
    {
    Tick += OnTick;
    KeyDown += OnKeyDown;
    KeyUp += OnKeyUp;

        Interval = 10;
    }
    
    void OnTick(object sender, EventArgs e)
    {
    }
    
    void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.K)
        {
            Ped ped = World.CreatePed(GTA.Native.PedHash.Acult01AMO, Game.Player.Character.Position); //How to bring "ped" to the other block?
        }
        if (e.KeyCode == Keys.I)
        {
            ped.Delete(); //How would I get the "ped" variable to carry over to this statement?
        }
    }
    
    void OnKeyUp(object sender, KeyEventArgs e)
    {
    }
    

    }


  • MODERATOR

    using GTA;
    using System;
    using System.Windows.Forms;
    
    public class example : Script
    {
    	Ped ped = null;
    
    	public example()
    	{
    		Tick += OnTick;
    		KeyDown += OnKeyDown;
    		KeyUp += OnKeyUp;
    
    		Interval = 10;
    	}
    
    	void OnTick(object sender, EventArgs e)
    	{
    	}
    
    	void OnKeyDown(object sender, KeyEventArgs e)
    	{
    		switch (e.KeyCode)
    		{
    			case Keys.K:
    			{
    				ped = World.CreatePed(GTA.Native.PedHash.Acult01AMO, Game.Player.Character.Position);
    				break;
    			}
    			case Keys.I:
    			{
    				if (ped != null && ped.Exists())
    				{
    					ped.Delete();
    				}
    				break;
    			}
    		}
    	}
    
    	void OnKeyUp(object sender, KeyEventArgs e)
    	{
    	}
    }
    

    Make it a global variable.



  • @Jitnaught THANK YOU! This has opened a completely new door for scripting with me!!!



  • @NotCrunchyTaco I could be late to the party, but there are 2 ways as far as I know.

    1. The one which @Jitnaught provided is called "Global Variable" & the one you used is called "Local Variable".

    2. The second way would be to define it just out of the function, onkeyDown as below
      void OnTick(object sender, EventArgs e)
      {
      }

    Ped ped;

    void OnKeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.K)
    {
    ped = World.CreatePed(GTA.Native.PedHash.Acult01AMO, Game.Player.Character.Position); //How to bring "ped" to the other block?
    }
    if (e.KeyCode == Keys.I)
    {
    ped.Delete(); //How would I get the "ped" variable to carry over to this statement?
    }
    }

    Here is a link for more info.
    https://www.quora.com/What-is-the-difference-between-local-variable-and-global-variable-in-C

    and yeah, C# is defiantly one of the great languages to learn, goodluck.

    @Jitnaught If I am not wrong, you don't have to set Ped ped value as "null", cause until variable been assigned to any value, the instance remains null by default, right? I could be wrong though.


  • MODERATOR

    @ashishcw
    I'm pretty sure that if you don't assign it you will get an "unassigned variable" error.



  • @Jitnaught You would if it was a local variable but you're okay with global variables.

    However, it is good practice to always assign default values anyway, rather than hoping they default to the required state.


  • MODERATOR

    @LeeC2202 Ah ok, thanks for the info.



  • @ashishcw said in How could I bring this variable over?:

    void OnKeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.K)
    {
    ped = World.CreatePed(GTA.Native.PedHash.Acult01AMO, Game.Player.Character.Position); //How to bring "ped" to the other block?
    }
    if (e.KeyCode == Keys.I)
    {
    ped.Delete(); //How would I get the "ped" variable to carry over to this statement?
    }
    }

    This code also really need some sanity checks, like whether Ped even exists (which, btw, it's good to set it to 'null' to begin with) on Keys.I, and/or whether you haven't changed ped model since creating it (changed with a trainer, for example).



  • @Jitnaught Yes, that's rightly said by @LeeC2202, but it doesn't mean, you are wrong anywhere,
    also, in my script(though the ones I make are not a perfect or an ideal scripts, but still it does the job), rather than setting it's global value to null, I would rather set it as null, when I don't need that entity any longer. So something like,
    pedMarkAsNoLongerNeeded();
    ped = null;

    @meimeiriver Yes, that's very correct, but as he was looking for an example, I just put the code like that, else I would've put like below in my code

    void OnKeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.K)
    {
    if (ped != null)
    {
    ped.Delete();
    ped = null;
    ped = World.CreatePed(GTA.Native.PedHash.Acult01AMO, Game.Player.Character.Position); //How to bring "ped"
    }else
    {
    ped = World.CreatePed(GTA.Native.PedHash.Acult01AMO, Game.Player.Character.Position); //How to bring "ped"
    }
    to the other block?
    }
    if (e.KeyCode == Keys.I)
    {
    if(ped != null)//This will help before throwing, a System NullReferenceException : Object reference not set to an instance of an object.
    {
    pedMarkAsNoLongerNeeded();
    ped = null;

    @NotCrunchyTaco Try using MarkAsNolongerNeeded(); instead Delete(); for some realistic effect rather than just making ped disappeared in an air. I learned this from my mistake. Thanks to @stillhere :D
    //ped.Delete(); //How would I get the "ped" variable to carry over to this statement?
    }

    }
    }

    I could be wrong though, since as I said, I am just learning C#



  • @ashishcw I am still learning C# too. Thanks for the help. I would use MarkAsNoLongerNeeded but I need the ped to be frozen in place so when I execute that the ped just walks away.



  • @NotCrunchyTaco Afcourse, MarkasNolongerNeeded, clears the set entity from the memory, and thus, entities, will go back to their basic attribute/behavior.

    So if you want to freeze entity position before setting markasnolongerneeded, try this.

    Function.Call(Hash.FREEZE_ENTITY_POSITION, Entity(in your case, this will be ped's variable), bool value(set this to true))

    and later call Markasnolongerneeded

    I haven't tested it, but I think, this should work.

    P.S. Kindly make a note, once you set an entity to markasnolongerneeded, you won't be able to recall the same value/hashcode to memory again, this is equal to Ped.Delete() but just in more realistic way.
    Hope this helps.



  • @ashishcw Thanks man! :D


Log in to reply
 

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.