From C++, To Lua, to C++ Again!
I've started on my quest to make a full C++ has started today. I've noticed that LEX2 via the project manager didn't copy over as clean as it should, so I had to make the project from scratch which wasn't a big deal (for me). I should look into a way of getting that fixed.
After setting the project up, I noticed a few errors I was getting after I commented out the LUA_GAME preprocessor. It was a quick fix, and development went under way after that.
For this project, I'll need things that's already available in Lua as part of the SDK. The high priorities are the first person player, animation manager, first person weapon (the pistol) and the Crawler's AI. As I said before, I don't want to convert SDK scripts to C++, but instead write the code myself. I'll look at the Lua stuff only if I get stuck.
Today I found out that not everything will work when you convert Lua code to C++. I decided on the player as I've written it for C++ with my actor class, then converted it to Lua because of problems. Now I want to but it back to C++ as a puppeteer. It took a few hours line by line converted the code. When it was all done, I ran into a few issues.
First off was HOW to spawn it? I could use a puppeteer script like I mentioned in my last blog, but the player should be spawned dead last. So the best method was to create the player in LEX2's OnPostMapLoad function.
// This function is called when the program finished loading a map.
void OnPostMapLoad()
{
Entity* puppet = WORLD->FindEntity("Player");
FPSPlayer* player = new FPSPlayer(puppet);
}
This function is called at the every end of the level loading process. This is mostly so we don't hear the weapon being picked up while the map is loading, or a spawn sound like the Vectronic Demo.
I was then running into null values that were driving me crazy. I went back and predefined all empty values as NULL and I was still getting a crash. It turns out, I didn't have the bool function returning the right value.
Next was the footstep sounds. As I said before, I copied my fully working Lua player script and I noticed that the footstep sounds were playing too fast. I defined the value lastfootsteptime as an int in the header as 0, but when I did a system print of that value, it'd always return as 1. the fix was to define the variable before the footstep function instead of in the header as so:
int lastfootsteptime = 0;
void FPSPlayer::UpdateFootsteps()
{
if (lastfootsteptime == NULL) { lastfootsteptime = 0; }
// If we're moving and not in the air, play a step sound.
if (input[0] != 0 || input[1] != 0)
{
float speed = entity->GetVelocity().xz().Length();
if (entity->GetAirborne() == false)
{
if (speed > MoveSpeed*0.5)
{
long t = Time::GetCurrent();
long repeatdelay = 500;
if (t - lastfootsteptime > repeatdelay)
{
lastfootsteptime = t;
PlayFootstepsSound();
}
}
}
}
}
Last, I had to fix a Gimbal lock with picked up objects. This was because all variables in Lua and C++ work differently. So when going the lines one by one, I stored rotation values in a Vec3, and not a Quat.
After the first day of my summer game, I got a player that walks around, jumps, can crouch under tight spaces with working sounds! I just got a few things to tweak before going to the next thing which will probably be the animation manager.
- 4
0 Comments
Recommended Comments
There are no comments to display.