[Help needed] Weapon randomising and some other questions
Author
Message
ATOS Productions
Joined: Mon Aug 27, 2012 7:21 pm Posts: 31
[Help needed] Weapon randomising and some other questions
The main question is about a "weapon randomising system" originally coded by clunatic. The idea of how it should work is quite simple: you buy a device from the buying menu (i.e. "Random Battle Rifle"), which spawns a gun from a list (i.e. , "Battle Rifle MK1, Battle Rifle MK2, Battle Rifle MK-X, Battle Rifle MK-Infinity") , then deletes itself. The problem is that although one of the listed guns spawn and the spawner device deletes itself, the spawned gun, instead of being in the actor's inventory, is simply created. (I hope you understood this all...I still sometimes have problems writing/speaking english)
Here's the code (gun names changed and comment marked with * by me) :
Code:
function Create(self) self.Parent = nil self.Module = "doesntmatter.rte" --change this to your module name self.ARTable = {"Battle Rifle MK1","Battle Rifle MK2","Battle Rifle MK-X","Battle Rifle MK-Infinity"} --alter these tables to list your weapon names end
function Update(self) if self.ID == self.RootID then --check if fake gun is being held self.Parent = nil --and set parent to nil if it's not being held else local actor = MovableMan:GetMOFromID(self.RootID) --if fake gun is being held, get the id of the actor holding it if MovableMan:IsActor(actor) then --check that the actor is actually an actor (and alive) self.Parent = ToActor(actor) --then assign the actor to self.Parent for later use. end end local Type = nil --create a variable for the fake gun's type if self.PresetName == "Random Battle Rifle" then Type = self.ARTable end
local choice = math.floor(math.random(1,#Type)) --choose a number between 1 and the total number of names in the table local Gun = CreateHDFirearm(Type[choice],self.Module) --create the HDFirearm, based on: * --Type: the table for the weapon type --Choice: the random number that just got chosen --self.Module: the module name, just to make sure the right weapon gets spawned
if self.Parent == nil then --if fake gun is not being held Gun.Pos = self.Pos --set real gun position to the fake gun's position MovableMan:AddMO(Gun) --spawn the real gun self.ToDelete = true --delete the fake gun else --if fake gun is being held self.Parent:AddInventoryItem(Gun) --add real gun to the actor holding it self.ToDelete = true --delete the fake gun end end
*I think the problem in this line, (I'm a Lua idiot, warn you) but I don't know how to add weapon to actor's inventory, so I haven't tried anything.
Other questions:
[1] Is it possible to change a sprite of a HDFirearm (I mean, your actor is holding a gun, you select a pie menu button and your gun's sprite changes) [2] Is it possible to change the muzzle offset the same way? [3] The same thing on being one-handed
Wed Apr 30, 2014 5:02 pm
clunatic
Joined: Fri Nov 23, 2012 5:42 am Posts: 143
Re: [Help needed] Weapon randomising and some other questions
Jeez, a man can't even go inactive for a couple of months without his code being accused of not working! What's the world coming to
In any case, I just tested it to be double sure, but that code works just fine (assuming it's implemented correctly and the names all match). It was simply written for the previous version of cortex command! The ability to add guns to actors inventory by clicking directly on them was only added in build 30.
Fortunately, updating it to work with build 30 is ridiculously simple. All it needs is a slight delay to make sure the script doesn't get run until a potential actor is holding it and a switch to previous weapon command given to the actor.
So, here's an updated version of my testing code intended to work with a base version of ronin.rte:
(Note: Even if you're not going to use them, there is little reason to remove the other types of tables from the code.)
Ini code (dont forget to change the spaces to tabs!):
Code:
AddDevice = HDFirearm CopyOf = Glock PresetName = Random Pistol Description = A random pistol ScriptPath = Ronin.rte/RandomiseWeapons.lua
AddDevice = HDFirearm CopyOf = AK-47 PresetName = Random Assault Rifle Description = A Random Assault Rifle ScriptPath = Ronin.rte/RandomiseWeapons.lua
AddDevice = HDFirearm CopyOf = Kar98 PresetName = Random Rifle Description = A Random Rifle ScriptPath = Ronin.rte/RandomiseWeapons.lua
AddDevice = HDFirearm CopyOf = Pumpgun PresetName = Random Shotgun Description = A Random Shotgun ScriptPath = Ronin.rte/RandomiseWeapons.lua
Lua code:
Code:
function Create(self) self.Parent = nil --create a variable for the parent and set it to nil self.Module = "Ronin.rte" --change this to your module name --alter these tables to list your weapon names: self.PistolTable = {"Desert Eagle", "Peacemaker", "Glock"} self.ARTable = {"AK-47","M16"} self.RifleTable = {"Kar98","M1 Garand"} self.ShotgunTable = {"Sawed-off shotgun","Pumpgun","Spas 12"} --you can also add more tables, or remove tables you don't want, if adding more tables be sure to make a new elseif for the type selection. self.Timer = Timer() --make timer self.Timer:Reset() --reset timer end
function Update(self) if self.Timer:IsPastRealMS(1) then --check timer. God knows why 1 MS is enough. if self.ID == self.RootID then --check if fake gun is being held self.Parent = nil --and set parent to nil if it's not being held else local actor = MovableMan:GetMOFromID(self.RootID) --if fake gun is being held, get the id of the actor holding it if MovableMan:IsActor(actor) then --check that the actor is actually an actor (and alive) self.Parent = ToActor(actor) --then assign the actor to self.Parent for later use. end end local Type = nil --create a variable for the fake gun's type if self.PresetName == "Random Pistol" then --check the fake gun's name and assign Type to the correct table Type = self.PistolTable elseif self.PresetName == "Random Assault Rifle" then Type = self.ARTable elseif self.PresetName == "Random Rifle" then Type = self.RifleTable elseif self.PresetName == "Random Shotgun" then Type = self.ShotgunTable end
local choice = math.floor(math.random(1,#Type)) --choose a number between 1 and the total number of names in the table local Gun = CreateHDFirearm(Type[choice],self.Module) --create the HDFirearm, based on: --Type: the table for the weapon type --Choice: the random number that just got chosen --self.Module: the module name, just to make sure the right weapon gets spawned
if self.Parent == nil then --if fake gun is not being held Gun.Pos = self.Pos --set real gun position to the fake gun's position MovableMan:AddMO(Gun) --spawn the real gun self.ToDelete = true --delete the fake gun else --if fake gun is being held local GunName = Gun.PresetName self.Parent:AddInventoryItem(Gun) --add real gun to the actor holding it if ToAHuman(self.Parent).EquippedItem.PresetName ~= GunName then self.Parent:GetController():SetState(Controller.WEAPON_CHANGE_PREV,true); --switch to previous weapon end self.ToDelete = true end end end
To your other questions: No, to all of them. There are various complicated ways of doing those things, no easy ways.
Re: [Help needed] Weapon randomising and some other questions
Ignoring the functionality, which I assume Clunatic's got fixed up, you can cut off all of the elseifs and make it far easier to extend by putting the various weapon tables in a table and checking that. See the script:
Code:
function Create(self) self.Parent = nil --create a variable for the parent and set it to nil self.Module = "Ronin.rte" --change this to your module name --alter these tables to list your weapon names: self.WeaponTable = { ["Random Pistol"] = {"Desert Eagle", "Peacemaker", "Glock"} ["Random Assault Rifle"] = {"AK-47","M16"} ["Random Rifle"] = {"Kar98","M1 Garand"} ["Random Shotgun"] = {"Sawed-off shotgun","Pumpgun","Spas 12"} } self.Timer = Timer() --make timer self.Timer:Reset() --reset timer end
function Update(self) if self.Timer:IsPastRealMS(1) then --check timer. God knows why 1 MS is enough. if self.ID == self.RootID then --check if fake gun is being held self.Parent = nil --and set parent to nil if it's not being held else local actor = MovableMan:GetMOFromID(self.RootID) --if fake gun is being held, get the id of the actor holding it if MovableMan:IsActor(actor) then --check that the actor is actually an actor (and alive) self.Parent = ToActor(actor) --then assign the actor to self.Parent for later use. end end local Type = nil --create a variable for the fake gun's type --Select the weapon based on the fake gun's name if self.WeaponTable[self.PresetName] ~= nil then Type = self.WeaponTable[self.PresetName]; end
local choice = math.floor(math.random(1,#Type)) --choose a number between 1 and the total number of names in the table local Gun = CreateHDFirearm(Type[choice],self.Module) --create the HDFirearm, based on: --Type: the table for the weapon type --Choice: the random number that just got chosen --self.Module: the module name, just to make sure the right weapon gets spawned
if self.Parent == nil then --if fake gun is not being held Gun.Pos = self.Pos --set real gun position to the fake gun's position MovableMan:AddMO(Gun) --spawn the real gun self.ToDelete = true --delete the fake gun else --if fake gun is being held local GunName = Gun.PresetName self.Parent:AddInventoryItem(Gun) --add real gun to the actor holding it if ToAHuman(self.Parent).EquippedItem.PresetName ~= GunName then self.Parent:GetController():SetState(Controller.WEAPON_CHANGE_PREV,true); --switch to previous weapon end self.ToDelete = true end end end
By the way, a lot of the weapon choosing stuff could be cut down into two lines this way, though it does get kinda difficult to read. Also, there should probably be some error checking for if the gun name doesn't exist in the table (in either this version or clunatic's version) cause right now it'll spew out some error message if a mistake was made. An easy way would be to make Type default to the pistol table or something.
Code:
local Gun = nil; --No need for a local Type variable now, just define gun here so we can use it later --Select the weapon based on the fake gun's name if self.WeaponTable[self.PresetName] ~= nil then Gun = CreateHDFirearm(self.WeaponTable[self.PresetName][math.floor(math.random(1, #self.WeaponTable[self.PresetName])), self.Module); end
Wed Apr 30, 2014 8:23 pm
ATOS Productions
Joined: Mon Aug 27, 2012 7:21 pm Posts: 31
Re: [Help needed] Weapon randomising and some other questions
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum