So i was FINALLY able to make something that will help us use private methods & fields.
Get a Specific private field:
This code will let you know in what buildingPrivileges the player is in (so basically the tool cupboards where he is at), this is totally useless of course XD, but it's an example.
Get a Specific private method:Code:PLUGIN.Name = "test" PLUGIN.Title = "test" PLUGIN.Version = V(1, 0, 0) PLUGIN.Description = "test." PLUGIN.Author = "Reneb" PLUGIN.HasConfig = false function PLUGIN:Init() command.AddChatCommand( "test", self.Object, "PLUGIN:cmdTest" ) endfunction PLUGIN:cmdTest(player,cmd,args) -- So first you need to get the field out of BasePlayer type named buildingPrivlidges -- var = type:GetField("fieldname"),privatefield -- var is the variable that it will be saved in -- type is the type of the class -- fieldname is the private field name you want -- privatefield is rust.PrivateBindingFlag() localbuildingPrivileges = global.BasePlayer._type:GetField("buildingPrivlidges", rust.PrivateBindingFlag() ) -- Now we get the value off the player bldpriv = localbuildingPrivileges:GetValue(player)-- as buildingPrivlidges is a list you have to read it value by value for i=0, bldpriv.Count-1 do rust.SendChatMessage(player,"Found a Tool Cupboard, getting names of allowed users") locAllowed = bldpriv[i].authorizedPlayers for u=0, locAllowed.Count-1 do rust.SendChatMessage(player,locAllowed[u].username) end end end
Code:PLUGIN.Name = "test" PLUGIN.Title = "test" PLUGIN.Version = V(1, 0, 0) PLUGIN.Description = "test." PLUGIN.Author = "Reneb" PLUGIN.HasConfig = false function PLUGIN:Init() command.AddChatCommand( "test", self.Object, "PLUGIN:cmdTest" ) endfunction PLUGIN:OnServerInitialized() -- same way as GetField, but with GetMethod in here FallDamage = global.BasePlayer._type:GetMethod("OnPlayerLanded", rust.PrivateBindingFlag() ) endfunction PLUGIN:cmdTest(player,cmd,args) local damageAmount = 100 param = util.TableToArray( { damageAmount } ) -- setting the damageAmount as a Single (float) util.ConvertAndSetOnArray( param, 0, damageAmount, System.Single._type ) -- first argument of invoke is the Source -- second argument are all the arguments of the method in an array. -- here there is only 1 argument FallDamage:Invoke( player, param ) end
Private Methods & Fields
Discussion in 'Rust Development' started by Reneb, Jan 25, 2015.
-
Thanks!
-
You should also be able to Set a private field (awesome for airdrops )
it should look something like
Code:CPstartPos = global.CargoPlane._type:GetField("startPos", rust.PrivateBindingFlag() ) --but after that instead of GetValue it will be SetValue:local startPos = new( UnityEngine.Vector3._type, nil ) startPos.x = 1000 startPos.y = 100 startPos.z = -500 cargoplane = global.GameManager:CreateEntity("events/cargo_plane", new( UnityEngine.Vector3._type, nil ) , new( UnityEngine.Quaternion._type, nil ) ); if (cargoplane != null) { cargoplane:Spawn(true); CPstartPos:SetValue(cargoplane,startPos) }
None of the above was 100% tested, but it's for you guys to get a general idea of how everything works.
[DOUBLEPOST=1422223461][/DOUBLEPOST]lol i noticed my above post is half lua half C# XD
[DOUBLEPOST=1422225099][/DOUBLEPOST]Modify airdrop start & end
working on making the secondsToTake work, atm it can't
But with this you guys should be able to manage airdrops and set where they will drop the supply (well i'm sure as i tested it )
Code:PLUGIN.Title = "test" PLUGIN.Version = V(1, 0, 0) PLUGIN.Description = "temp" PLUGIN.Author = "Reneb" PLUGIN.HasConfig = falsefunction PLUGIN:Init() command.AddConsoleCommand("test.test", self.Object, "ccmdAirDrop") end function PLUGIN:OnServerInitialized() CPstartPos = global.CargoPlane._type:GetField("startPos", rust.PrivateBindingFlag() ) CPendPos = global.CargoPlane._type:GetField("endPos", rust.PrivateBindingFlag() ) CPsecondsToTake = global.CargoPlane._type:GetField("secondsToTake", rust.PrivateBindingFlag() ) CPsecondsTaken = global.CargoPlane._type:GetField("secondsTaken", rust.PrivateBindingFlag() ) CPdropped = global.CargoPlane._type:GetField("dropped", rust.PrivateBindingFlag() ) startPos = new( UnityEngine.Vector3._type, nil ) endPos = new( UnityEngine.Vector3._type, nil ) startPos.x = 1000 startPos.y = 200 startPos.z = 1000 endPos.x = 500 endPos.y = 200 endPos.z = 500 param = util.TableToArray( { 5 } ) util.ConvertAndSetOnArray( param, 0, 5, System.Single._type ) secondsToTake = param[0] endfunction PLUGIN:OnEntitySpawn(entity) print(tostring(entity)) if(entity:GetComponentInParent(global.SupplyDrop._type)) then print(tostring(entity:GetComponentInParent(global.SupplyDrop._type))) print(tostring(entity.transform.position)) end endfunction PLUGIN:ccmdAirDrop( arg ) if(cargoplane) then cargoplane:KillMessage() end cargoplane = global.GameManager.CreateEntity("events/cargo_plane", new( UnityEngine.Vector3._type, nil ) , new( UnityEngine.Quaternion._type, nil ) ); if (cargoplane ~= nil) then cargoplane:Spawn(true) CPstartPos:SetValue(cargoplane,startPos) CPendPos:SetValue(cargoplane,endPos) -- setting secondstotake CANT work --CPsecondsToTake:SetValue(cargoplane,secondsToTake) end end
Last edited by a moderator: Jan 25, 2015 -
Code:CodeLockField = global.CodeLock._type:GetField("code", rust.PrivateBindingFlag() ) KeyLockField = global.KeyLock._type:GetField("keyCode", rust.PrivateBindingFlag() ) CodeBoolField = global.CodeLock._type:GetField("hasCode", rust.PrivateBindingFlag() ) KeyBoolField = global.KeyLock._type:GetField("firstKeyCreated", rust.PrivateBindingFlag() ) local lock = BuildingBlock:GetSlot(global.Slot.Lock) local lock_type = lock and ( (lock:GetComponent("CodeLock") and "build/lock.code") or (lock:GetComponent("KeyLock") and "build/lock.key") ) local key = lock_type and ((lock_type == "build/lock.code" and CodeBoolField:GetValue(lock) and CodeLockField:GetValue(lock)) or ( lock_type == "build/lock.key" and KeyBoolField:GetValue(lock) and KeyLockField:GetValue(lock))) local pre_lock = buildingBlockBP.lock if pre_lock then local lock = global.GameManager.CreateEntity(pre_lock.type, newPos, newblock.transform.rotation) if pre_lock.type == "build/lock.code" then CodeLockField:SetValue(lock, pre_lock.key) CodeBoolField:SetValue(lock, true) else KeyLockField:SetValue(lock, tonumber(pre_lock.key)) KeyBoolField:SetValue(lock, true) end lock:SetParent(newblock2, "lock") lock:Spawn(true) end
-
But it's working for me:
Code:local loadinfo = new(global.LoadInfo._type, nil) loadinfo.msg = new(ProtoBuf.Entity._type, nil) loadinfo.msg.keyLock = new(ProtoBuf.KeyLock._type, nil) loadinfo.msg.keyLock.code = tonumber(pre_lock.key) lock:Load(loadinfo) KeyBoolField:SetValue(lock, true)
-
Exacly that's because it's in int32, not in single.
Int32 is the tonumber in lua.
But all movement speeds or distances are in Single and not in Int32. -