Data Realms Fan Forums
http://forums.datarealms.com/

Magazine Swapping
http://forums.datarealms.com/viewtopic.php?f=73&t=31346
Page 1 of 1

Author:  BrainChild [ Tue Jun 26, 2012 9:11 am ]
Post subject:  Magazine Swapping

Hey.

I have been trying to write some code that changes the magazines of all the same weapons on a specific team when they have a specific item in their inventory



So the code is suppose to go through all the actors on the map if the item it's attached to is created (adding another if to check it stays team specific?)
It then checks if the actor has a specific weapon.
It saves the weapon the actor is holding into a local variable "gun" then changes the magazine of that weapon.
Problem is if he's not holding the weapon we checked for with the "HasOBject" then the wrong weapons magazine gets swapped

So how would I go about searching for the weapon in his inventory and then swapping the magazine?
How do I keep the swapping team specific

Thanks in Advance

Author:  4zK [ Tue Jun 26, 2012 1:35 pm ]
Post subject:  Re: Magazine Swapping

Doesn't that first scan for the shotgun in the actors' inventory, then index the variable gun as their held item? It mustn't be certain they're actually holding the weapon.

Swapping the magazine when the weapon is held wouldn't be a problem, I think it could look something like this:

Making the swapping team-specific depends on what swaps the magazines. If it is, for example, something shot by a teammate, you can try adding this:


I haven't tested this though. I hope it helps (works)! :)

Author:  BrainChild [ Tue Jun 26, 2012 6:03 pm ]
Post subject:  Re: Magazine Swapping

Well it is definitely trying to change the magazine but the problem seems to be that you have to hold the object (a explosive in my case) to run the script but when you hold the object then the object isn't the shotgun and thus does nothing.

When I run the script it gives a error : :6: attempt to index local 'gun' (a nil value)

Is there a way that I can go from actor to actor and then cycle through his inventory until I find the weapon that I'm looking for and then just swap the magazines?

Author:  4zK [ Tue Jun 26, 2012 6:42 pm ]
Post subject:  Re: Magazine Swapping

I'm not entirely sure if the magazine can be swapped inside the actor's inventory, but try this.

And can you please tell me what exactly makes the magazines swap? It can be crucial to how the script works.

Author:  BrainChild [ Tue Jun 26, 2012 7:12 pm ]
Post subject:  Re: Magazine Swapping

4zK wrote:
And can you please tell me what exactly makes the magazines swap? It can be crucial to how the script works.


Oh sorry, you buy a item, a container of sorts that upgrades the weapon, so for instance you buy a bullet container for the shotgun that swaps the magazine of the shotgun for an upgraded magazine with more rounds, it should get applied to all the weapons (that the container works on). Future weapons should also be upgraded if the container is still in one of the actors inventories. There are a few smaller technicalities.

I read on the wiki about SwapNextInventory and SwapPrevInventory except that I don't know how to use them, is there a way that I can use those commands with a loop to go through a actors inventory, checking all the weapons he has and then swap the magazines?

Author:  Azukki [ Tue Jul 03, 2012 3:32 pm ]
Post subject:  Re: Magazine Swapping

I believe it takes a moment in-game for the actor to actually switch weapons using one of those two functions; you won't be able to cycle through all of their weapons in one create function.

It's a little more complicated and you'll be able to see the actors rapidly cycling through their inventories, (which will look weird) but it does sound doable.

The only problem about that for which I can imagine no solution is knowing when you've cycled through an actor's inventory once entirely.
I suppose since redundancy of setting the next mag to be used isn't a problem, you could just swap weapons ten or so times, change the mag if it's the right weapon, and you'd probably get all of them.

Author:  BrainChild [ Thu Jul 05, 2012 2:18 pm ]
Post subject:  Re: Magazine Swapping

I have found a workaround, seeing as I don't know how to even write the code for something that goes through the actors inventory

So plan B:
Code/Example how I would go about removing something from an actors inventory or adding something to an actors inventory?

Also thanks Azzuki one of your older mods gave me the idea ^_^

EDIT: Seriously Azzuki your mod saved my bacon, thank you very much.
I'm going to use a pie slice on the weapon, how do I get thethe actor in a variable so I can check if he has an item?
If you place something in a actors inventory does it go in his hands?

Author:  Azukki [ Thu Jul 05, 2012 6:04 pm ]
Post subject:  Re: Magazine Swapping

So in a gun's pie menu script, you're wanting to reference the actor holding the gun?
That's actually easier than referencing the gun itself! It's just "actor" or whatever you have in the parenthesis on the respective function line.

And yes, if you add something to an actor's inventory, it is also equipped, I believe.

Author:  BrainChild [ Fri Jul 06, 2012 9:35 am ]
Post subject:  Re: Magazine Swapping

So I created a Pie Slice with my Lua attacked and no error when I run CC but as soon as I use the Pie menu it says there is an error with my Lua

The code:


It says line 3 attemp to index local 'gun' (a nil value)
What did I do wrong?

Author:  Arcalane [ Fri Jul 06, 2012 6:43 pm ]
Post subject:  Re: Magazine Swapping

Your check is a little too simplistic, I think.

Code:
function ChangeMag(actor)
   local gun = ToAHuman(actor).EquippedItem;
   if gun ~= nil then
      local gun = ToHDFirearm(gun);
      gun:SetNextMagazineName(New Magazine");
      gun:Reload();
   end
end

Author:  BrainChild [ Fri Jul 06, 2012 9:40 pm ]
Post subject:  Re: Magazine Swapping

Arcalane I owe you, seriously it took me a week to get the code for this to work
Thank you and thanks to all the help from everyone.

Just one small thing, how do I add and remove items from an actors inventory?
The wiki has a AddInventoryItem but I'm not sure how to use it.

Author:  Arcalane [ Fri Jul 06, 2012 10:58 pm ]
Post subject:  Re: Magazine Swapping

BrainChild wrote:
Arcalane I owe you, seriously it took me a week to get the code for this to work


:roll: I got that from the Coalition Missile Launcher... it's what I use for all the 40K recomp ammo switching.

Note that the sixth line forces an immediate reload of the weapon, so you may (or may not) want to remove it. Just a heads up.

Author:  BrainChild [ Sat Jul 07, 2012 7:43 pm ]
Post subject:  Re: Magazine Swapping

Arcalane wrote:
:roll: I got that from the Coalition Missile Launcher... it's what I use for all the 40K recomp ammo switching.


O.O now I feel stupid, didn't even notice it could switch. Thanks anyway

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