Since early 2002, Shee Labs has been a group of individuals working to produce free game and media downloads. With ever-fluctuate staffing, Shee Labs continues working behind the scenes on various exciting projects for a bewildering array of areas. Shee Labs ceased to exist in September 2009.
This tutorial is part of a series of three will show you how to create complex materials, and how to program a weapon which can alter them, and a physics gun.
Posted by ambershee on Feb 28th, 2008
Basic Server Side Coding.
This is the second part of the advanced weapons tutorial. You should have a custom mesh, and a custom material all set up ready for some coding - if you don't, you may want to step back and read the first part of the tutorial before you get started. Once again, let's start with the power cube.
Let's create a class called PowerCube. We're going to want it to be a static mesh with physics properties. We're in luck, there's already a class which has just that for us - kActor. Even better, there's a class derived from that called kActorSpawnable, which we can create while the game is running if we want to - perfect.
Best of all, we don't need to write any real code - everything we want is in the default properties, and we just need to do some tweaking. We set bWakeOnLevelStart to true, meaning that when the game begins, the object will start to act under world physics. We don't want magically floating boxes that unpredictably fall back down to earth. We also add a static mesh component. I set the mesh to 'TutorialStuff.PowerCube' for the time being. And that's it!
This is where things get a little tricker. We know this is going to be difficult, so how about we think about what we need before we start?
We want a weapon, so extending the Instagib Rifle could be a good start as it already has mesh and doesn't have a weapon group, which saves us a lot of hassle. The weapon needs to also contain material and colour information, so that we can change the visible appearance of the power cube when we want to. Well, that gives us some direction! I hope you remembered your colour choices and materials from the last part of the tutorial, because they're mighty handy right now.
Next up, we need some logic. We need to be able to both colourise and apply new materials to our power cube. Let's write a function to do each.
We'll start with recolouring, as this should be nice and easy. I decided that our firing function should decide what colour we want the cube to be, so this function takes that colour, as well as the actor that has been shot by the weapon. We quickly check that our actor is indeed a power cube, and if it is, we create and set a new material instance from the material it already had, and find the PowerColour parameter - then we change it with our chosen colour.
Materials wise, we only have two options and we're using the boolean 'Hard' to decide which one we need, so we'll give that function just the actor that the weapon hit, and decide the rest. We work out whether the weapon is currently set to hard or not, then create and set a new material instance based on that again, this time looking for the BaseTexture and RippleEffect parameters from before, and applying different textures to them. Very snazzy.After we've done that, we then make sure that the materials are the right colour, so we call recolour with the correct colour.
Only one thing left! Our weapon use what colour we're firing, and what material we're firing - but it can't actually fire properly! We need a StartFire() function.
In this case, we just lifted and modified the CalcWeaponFire() function in the Weapon class. It doesn't deal any damage, or have any fancy effects, but it does send out a trace, and determine what we hit, which is enough for us. I decided that our first fire mode would cycle through the available colours, and our second would toggle the different materials.
To summarise what is commented in the code, we send out a trace and determine what we have hit. If we have hit a power cube, and our fire mode is the first, we change the colour, otherwise if the fire mode is not the first, we change the material. Additionally, we nicked and left in some code from CalcWeaponFire() that handles subsequent hits and portals. We'll leave that in for now because you never know, it might be useful later.
You might want to give that a good read through, and make sure you understand exactly what is going on. It's fortunately not too complicated at the end of the day.
So we're finished! Well, not quite. We have power cubes, and we have a very basic weapon that will change them. What we don't have are cubes and weapons in the game. I wrote a quick summon function that I can call from the console to drop one straight into the game for testing. I decided to use the Weapon Replacement mutator that is already in the game to test, but you could easily write your own mutator to replace the default inventory - just like we did in Unreal Learning #1. You can access the console in the game, by default pressing the tab button. Typing in the name of the function will allow it to happen - this is what the exec function is for. Note that because the function is in the weapon code, it will only happen when the weapon is drawn!
We're definitely now done! Compile the code, and give it a try. You should be able to spawn a power cube using the console command (doesn't that look odd but funky in game?), and use the super shock rifle (ChargeRifle) primary fire to change it's colour, and secondary colour to change it's material.
In the final part of this tutorial, we will look at how the power cube physics can be altered - and we'll add a physics gun to let us throw it around.
Until next time, happy modding!