Solved If XYZ then

Discussion in 'Rust Development' started by PaiN, Jun 19, 2015.

  1. So i'm about to finish a plugin but there is a small "issue"...
    i have this line
    Code:
    if player.transform.position.x == self.Config.Test.X and player.transform.position.y == self.Config.Test.Y and player.transform.position.z == self.Config.Test.Z then
    print("Something")
    
    and it is not working.. I'm on the xyz that i have to be
    Is there other way to do this position checking ?
     
  2. Wulf

    Wulf Community Admin

    Are your config entries ints or strings?
     
  3. I'm his brother so i will give u his config and he will read your reply through his phone.
    This is the plugin's config.
    Code:
    self.Config.Warp1 = self.Config.Warp1 or {}
    self.Config.Warp1.X = self.Config.Warp1.X or "503"
    self.Config.Warp1.Y = self.Config.Warp1.Y or "503"
    self.Config.Warp1.Z = self.Config.Warp1.Z or "503"
    
     
  4. Wulf

    Wulf Community Admin

    Try the code below. Pretty sure the issue is that you're comparing an int with a string, which won't work.
    Code:
    self.Config.Warp1 = self.Config.Warp1 or {}
    self.Config.Warp1.X = self.Config.Warp1.X or 503
    self.Config.Warp1.Y = self.Config.Warp1.Y or 503
    self.Config.Warp1.Z = self.Config.Warp1.Z or 503
     
  5. Done
    Hmm do i have to check every (x) secs ? To see if there is someone at these cords ?
    Bcs i tried it with myself but nothing happens

    I will try to use the tonumber function
    [DOUBLEPOST=1434724985][/DOUBLEPOST]Tried the tonumber function but it is still not working i have no idea on what i'm doing wrong

    Some of the code that might help you
    I tried without the tonumber function too btw
    Code:
    function PLUGIN:LoadDefaultConfig()
    self.Config.Warp1 = self.Config.Warp1 or {}
    self.Config.Warp1.X = tonumber(self.Config.Warp1.X) or 503
    self.Config.Warp1.Y = tonumber(self.Config.Warp1.Y) or 503
    self.Config.Warp1.Z = tonumber(self.Config.Warp1.Z) or 503
    self:SaveConfig()
    end--and this is what i'm trying to do
    if player.transform.position.x == self.Config.Warp1.X and player.transform.position.y == self.Config.Warp1.Y and player.transform.position.z ==  self.Config.Warp1.Z then
    print("debug")
    Maybe its because of the Minus that self.Config.Warp1.X has ?
    Code:
    self.Config.Warp1.X = self.Config.Warp1.X or -1465
    self.Config.Warp1.Y = self.Config.Warp1.Y or 10
    self.Config.Warp1.Z = self.Config.Warp1.Z or 334
    
     
    Last edited by a moderator: Jun 19, 2015
  6. If you are trying to use positions and match them. this is a somewhat wrong way of doing it since the positions are a float and the result can be something like

    My example is using C#

    Code:
    if(player.transform.position.x == self.Config.Wrap1.x)
    //in actual value
    if (124.124 == 124.00)
    The best solution would be to store the Config position into a UnityEngine.Vector3(x, y, z) and then use the Vector3.Distance(Vector3 a, Vector3 b); to get the distance between the two positions.

    Code:
    var configPos = new Vector3(config.x, config.y , config.z);if(Vector3.Distance(player.transform.position, configPos) < 1)
    {
        //Do Something
    }
     
  7. I will give it a try in some minute ..

    I have to tell also that i dont know what these Vectors3 are.. some good coders are using them and i'm like wtf..
    [DOUBLEPOST=1434805051,1434792741][/DOUBLEPOST]
    Code:
    local configPos = Vector3(self.Config.Warp1.X, self.Config.Warp1.Y, self.Config.Warp1.Z)
    if(Vector3.Distance(player.transform.position, configPos) < 1) then
    print("something")
    
    error
    Code:
    [Oxide] 4:01 PM [Error] Failed to call hook 'Test' on plugin 'Test'
    File: test.lua Line: 24 attempt to call global 'Vector3' (a nil value):
      at NLua.Lua.ThrowExceptionFromError (Int32 oldTop) [0x00000] in <filename unkn
    own>:0
      at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System
    .Type[] returnTypes) [0x00000] in <filename unknown>:0
      at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (ob
    ject,object[],System.Exception&)
      at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invoke
    Attr, System.Reflection.Binder binder, System.Object[] parameters, System.Global
    ization.CultureInfo culture) [0x00000] in <filename unknown>:0
    
     
    Last edited by a moderator: Jun 20, 2015
  8. Vector3 is just XYZ
     
  9. Vector3 is not a method to call. Read Zuen's code example again.
    You want to create a new Vector3 struct.
     
    Last edited by a moderator: Jun 20, 2015
  10. I made a new Vector3 struct and did the same thing like u told me
    I'm so lost i dont know where is the problem ..
    also maybe my translation is bad ..
    Can some1 tell me what "var" from C# means in LUA ?
    My code :
    Code:
    local configPos = new Vector3(self.Config.Warp1.X, self.Config.Warp1.Y , self.Config.Warp1.Z )
    if(Vector3.Distance(player.transform.position, configPos) <1) then
    rust.BroadcastChat("test")
    else
    rust.BroadcastChat("test")
    
    I tried also
    Code:
    local configPos = new UnityEngine.Vector3(self.Config.Warp1.X, self.Config.Warp1.Y , self.Config.Warp1.Z )
    if(Vector3.Distance(player.transform.position, configPos) <1) then
    rust.BroadcastChat("test")
    else
    rust.BroadcastChat("test")
    
    Error:

    http://screenshot.sh/m7ra7bLxQzD2J
     
    Last edited by a moderator: Jun 20, 2015
  11. I'm not familiar with using LUA at all, sorry that I am not more help but Vector3 is basically used for the position of an object in the world space.

    I believe you are just having some syntax issues right now.
     
  12. Var in c# is just a compiler determined strong typed variable, the compiler automatically attributes the proper type on compile. If you assign an int to it it will be an int (and it will be locked as an int and typechecked as one). There is no equivalent in lua afaik, just use the proper type instead.
     
  13. ^ What he said... so replace the var with Vector3