Data Realms Fan Forums
http://45.55.195.193/

Get a guns projectile / Get a projectiles gun
http://45.55.195.193/viewtopic.php?f=73&t=46194
Page 1 of 1

Author:  The5 [ Fri Aug 04, 2017 9:10 am ]
Post subject:  Get a guns projectile / Get a projectiles gun

So back the day I used to grab my projectiles by iterating over all MOs (on HD.IsActivated) and picking the Projectile preset closest to the guns muzzle.
Same for the parent gun, grab the closest preset with the right name upon projectile creation.

Do we have a more elegant way meanwhile?

Further, can I pass/fetch data between two scripts without making it global?
Say my weapon got a self._myProperty added on creation.
I now acquire the gun from within the projectiles script.
Will I be able to access it with parentGun._myProperty? (As in, does the MO returned by GetMOFromID still hold the properties of my Lua-object?)

I'll test the later as soon as I get to it.

Author:  4zK [ Fri Aug 04, 2017 10:51 am ]
Post subject:  Re: Get a guns projectile / Get a projectiles gun

As of build 30-something there's this neat new feature for MOSRs called NumberValue, which can be accessed by other scripts. Before this feature you could still use values like Mass and Sharpness (provided that they weren't important), but they were very limited in the amount of values to read.

Basically you can assign any real number value to a string, which can then be read as long as you can access the MOSR. Furthermore, it's important in many cases that you specify the class of the MO as MOSRotating or a sub-class of MOSRotating (e.g. HDFirearm) in order for the script to work, meaning it can't be used with stuff like MOPixel or MOSParticle.

The functions are as follows;
Code:
MO:GetNumberValue(string) -- return number value
MO:SetNumberValue(string, value)
MO:NumberValueExists(string) -- return boolean

-- example

MO = ToMOSRotating(particle);
if MO:NumberValueExists("counter") then
    MO:SetNumberValue("counter", MO:GetNumberValue("counter") + 1);
end

Number values are zero by default, and the NumberValueExists function will return as false on a zero value.

I use these functions all the time in my global scripts mod, take a look at the scripts if you're interested.

What comes to accessing your projectiles by looking at nearby MOs, that sounds really inefficient. I think a better way would be to spawn the projectile through a script in the gun and use a dummy projectile as the "real" round.

Author:  The5 [ Fri Aug 04, 2017 12:14 pm ]
Post subject:  Re: Get a guns projectile / Get a projectiles gun

Ah right!
When digging trough some mods I noticed sharpness is accessed everywhere, I see now!
So the lua object attributes are not accessible outside the script, but we got functions to push and fetch new attributes to the underlying c++ objects, neat!

The projectile querying is actually what I find to be used in other mods as well!
But yes, it is inefficient to loop over all entries and compare distances.
In my current case it's only missile launcher type of weapons, so it does not happen frequently, like a rapid fire weapon would.
But possibly the impact would be notable on such type of guns?


Spawning a projectile in the guns script would work, but introduce all the complexity of spawning, aligning, rotating, accelerating and ... I assume more ... into that script.
Certainly less error prone and surely more efficient still.
I don't quite like to bypass the builtin projectile spawning mechanics and then provide proxy projectiles for the AI to use the weapon correctly, but if that is how its done.

And thanks for the link to your scripts!
It's a bit troublesome to find mods that use the most recent functionality.^

EDIT:
You need to cast whatever GetObjectValue returns back into the appropriate type b4 it is usable.

Code:
      self._properties:SetNumberValue("targetID", self._target.RootID)
      self._properties:SetNumberValue("ownerID", self._owner.RootID)
      
      self._properties:SetObjectValue("targetOBJ", self._target)
      self._properties:SetObjectValue("ownerOBJ", self._owner)
      
      print(self._properties:GetNumberValue("targetID")) --returns ID
      print(self._properties:GetNumberValue("ownerID")) --returns ID
   
      print(self._properties:GetObjectValue("targetOBJ")) --returns Soldier, AHuman
      print(self._properties:GetObjectValue("ownerOBJ")) --returns Soldier, AHuman

      print(self._properties:GetObjectValue("targetOBJ").RootID) --returns nil
      print(self._properties:GetObjectValue("ownerOBJ").RootID) --returns nil

      print(ToMovableObject(self._properties:GetObjectValue("targetOBJ")).RootID) --cast to MO! Else returns nil
      print(ToMovableObject(self._properties:GetObjectValue("ownerOBJ")).RootID) --cast to MO! Else returns nil


Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/