Quantcast

Jump to content

» «
Photo

[C#] spawning a random weapon pickup and checking whether it's been picked up

7 replies to this topic
xanman2331
  • xanman2331

    Player Hater

  • Members
  • Joined: A week ago
  • Australia

#1

Posted A week ago

I am making my first mod and I have only really started learning C# in the last few days, so I apologise if the problem is something very obvious. I just want to check whether the pickup has been collected and I don't understand what's wrong (nor can I find any other topic on these forums that helps me). The error I am getting says "Error CS1503 Argument 2: cannot convert from 'GTA.Pickup' to 'GTA.Native.InputArgument'". Thanks for your help and I can provide more code if needed.
 
Pickup randWeapon = World.CreatePickup(weapons[rnd.Next(0, weapons.Count)], player.Position + (player.ForwardVector * 5), player.Rotation, 0, 1);
{
    Function.Call(Hash.HAS_PICKUP_BEEN_COLLECTED, randWeapon);

 


OfficerJohnson
  • OfficerJohnson

    Crackhead

  • Members
  • Joined: 07 Jan 2018
  • United-States

#2

Posted A week ago

I am making my first mod and I have only really started learning C# in the last few days, so I apologise if the problem is something very obvious. I just want to check whether the pickup has been collected and I don't understand what's wrong (nor can I find any other topic on these forums that helps me). The error I am getting says "Error CS1503 Argument 2: cannot convert from 'GTA.Pickup' to 'GTA.Native.InputArgument'". Thanks for your help and I can provide more code if needed.
 

Pickup randWeapon = World.CreatePickup(weapons[rnd.Next(0, weapons.Count)], player.Position + (player.ForwardVector * 5), player.Rotation, 0, 1);
{
    Function.Call(Hash.HAS_PICKUP_BEEN_COLLECTED, randWeapon);

Determine what HAS_PICKUP_BEEN_COLLECTED takes as an input argument. It's not accepting your Pickup object. Maybe it's an enum? If the function is supposed to take an object of type Pickup, review what is considered a pickup under SHV.NET.

OfficerJohnson
  • OfficerJohnson

    Crackhead

  • Members
  • Joined: 07 Jan 2018
  • United-States

#3

Posted A week ago

Okay. So the Pickup class has enums defined for pickups. Your code seems fine. Perhaps World.CreatePickup is not taking your randomization of the weapon as a valid argument? Try adding a single pickup type as the first argument and see if it gives you the error still. You may have to use a loop or some other method (maybe switch statement) to do that part.

Bob_74
  • Bob_74

    Crackhead

  • Members
  • Joined: 05 Jan 2018
  • None

#4

Posted A week ago Edited by Bob_74, A week ago.

 

I am making my first mod and I have only really started learning C# in the last few days, so I apologise if the problem is something very obvious. I just want to check whether the pickup has been collected and I don't understand what's wrong (nor can I find any other topic on these forums that helps me). The error I am getting says "Error CS1503 Argument 2: cannot convert from 'GTA.Pickup' to 'GTA.Native.InputArgument'". Thanks for your help and I can provide more code if needed.
 
Pickup randWeapon = World.CreatePickup(weapons[rnd.Next(0, weapons.Count)], player.Position + (player.ForwardVector * 5), player.Rotation, 0, 1);
{
    Function.Call(Hash.HAS_PICKUP_BEEN_COLLECTED, randWeapon);

 

 

Do you use ScriptHookVDotNet?

 

If so, it is not how pickup are created:

World.CreatePickup(PickupType type, Vector3 position, Vector3 rotation, Model model, int value)

The first parameter is the type of pickup the player will collect, it is an enum as OfficerJohnson said.

You also need to specify a valid Model or else your pickup will not be created.

 

Going back to your problem.

Oddly, Pickups don't react the same way as other entities like Peds, Props or Vehicles when you have to pass it to a native. I don't know why, I tried to pass randWeapon.Handle since natives works with handles but it didn't work.

Pickups also have the isCollected property but I had no success with it either...

 

The only working way I've found to detect if the pickup has been collected is to check ObjectExists():

// Creating Pickup
randWeapon = World.CreatePickup(PickupType.WeaponMinigun, Game.Player.Character.GetOffsetInWorldCoords(new Vector3(0f, 2f, 0f)), new Model("w_mg_minigun"), -1);


// Checking every Tick:
if randWeapon != null)
	if (!randWeapon.ObjectExists())
		UI.Notify("~g~Pickup has been collected!");

xanman2331
  • xanman2331

    Player Hater

  • Members
  • Joined: A week ago
  • Australia

#5

Posted A week ago

 

 

I am making my first mod and I have only really started learning C# in the last few days, so I apologise if the problem is something very obvious. I just want to check whether the pickup has been collected and I don't understand what's wrong (nor can I find any other topic on these forums that helps me). The error I am getting says "Error CS1503 Argument 2: cannot convert from 'GTA.Pickup' to 'GTA.Native.InputArgument'". Thanks for your help and I can provide more code if needed.
 
Pickup randWeapon = World.CreatePickup(weapons[rnd.Next(0, weapons.Count)], player.Position + (player.ForwardVector * 5), player.Rotation, 0, 1);
{
    Function.Call(Hash.HAS_PICKUP_BEEN_COLLECTED, randWeapon);

 

 

Do you use ScriptHookVDotNet?

 

If so, it is not how pickup are created:

World.CreatePickup(PickupType type, Vector3 position, Vector3 rotation, Model model, int value)

The first parameter is the type of pickup the player will collect, it is an enum as OfficerJohnson said.

You also need to specify a valid Model or else your pickup will not be created.

 

Going back to your problem.

Oddly, Pickups don't react the same way as other entities like Peds, Props or Vehicles when you have to pass it to a native. I don't know why, I tried to pass randWeapon.Handle since natives works with handles but it didn't work.

Pickups also have the isCollected property but I had no success with it either...

 

The only working way I've found to detect if the pickup has been collected is to check ObjectExists():

// Creating Pickup
randWeapon = World.CreatePickup(PickupType.WeaponMinigun, Game.Player.Character.GetOffsetInWorldCoords(new Vector3(0f, 2f, 0f)), new Model("w_mg_minigun"), -1);


// Checking every Tick:
if randWeapon != null)
	if (!randWeapon.ObjectExists())
		UI.Notify("~g~Pickup has been collected!");

This fixed the problem thanks :)


xanman2331
  • xanman2331

    Player Hater

  • Members
  • Joined: A week ago
  • Australia

#6

Posted A week ago

I have a couple of other questions: How would I go about writing the random weapon's name as a subtitle on screen? and (separate function) how do I set the coordinates I want something to spawn at?

 

I have been using Vector3 for spawn location but it says I have a problem with value tuples (Error CS8179 Predefined type 'System.ValueTuple`3' is not defined or imported, or is declared in multiple referenced assemblies), as well as converting (int, int, int) to Vector3 (Cannot implicitly convert type '(int, int, int)' to 'GTA.Math.Vector3')

Random rnd = new Random();            
GTA.Math.Vector3 spawnLoc1 = (rnd.Next(-3500, 7200), rnd.Next(-3300, 4000), 0);

Again I can provide more code if you need


OfficerJohnson
  • OfficerJohnson

    Crackhead

  • Members
  • Joined: 07 Jan 2018
  • United-States

#7

Posted A week ago

I have a couple of other questions: How would I go about writing the random weapon's name as a subtitle on screen? and (separate function) how do I set the coordinates I want something to spawn at?
 
I have been using Vector3 for spawn location but it says I have a problem with value tuples (Error CS8179 Predefined type 'System.ValueTuple`3' is not defined or imported, or is declared in multiple referenced assemblies), as well as converting (int, int, int) to Vector3 (Cannot implicitly convert type '(int, int, int)' to 'GTA.Math.Vector3')

Random rnd = new Random();            
GTA.Math.Vector3 spawnLoc1 = (rnd.Next(-3500, 7200), rnd.Next(-3300, 4000), 0);
Again I can provide more code if you need

For the first part, IIRC, UI.ShowSubtitle(). You need that.

As for the second part, see if there is a function for converting Vector3toInt. Yeah. We can't be casting 3-point parameters to a single int. vector3 needs an x,y and z value.

Bob_74
  • Bob_74

    Crackhead

  • Members
  • Joined: 05 Jan 2018
  • None

#8

Posted A week ago Edited by Bob_74, A week ago.

I have been using Vector3 for spawn location but it says I have a problem with value tuples (Error CS8179 Predefined type 'System.ValueTuple`3' is not defined or imported, or is declared in multiple referenced assemblies), as well as converting (int, int, int) to Vector3 (Cannot implicitly convert type '(int, int, int)' to 'GTA.Math.Vector3')
Random rnd = new Random();            
GTA.Math.Vector3 spawnLoc1 = (rnd.Next(-3500, 7200), rnd.Next(-3300, 4000), 0);

Again I can provide more code if you need

Vector3 is defined by three float values.

You need to cast the float type on your int numbers:

GTA.Math.Vector3 spawnLoc1 = (float)(rnd.Next(-3500, 7200), (float)rnd.Next(-3300, 4000), 0f);

The float notation in C# is made using an f after the number (ie: 0.0f). Otherwise it is considered as a int or a double if the number has a decimal separator (ie: 0.0).

  • xanman2331 likes this




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users