Hand Viewmodels: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Added categories to the page)
m (Removed div style from code blocks, minor cleanup/formatting)
Line 3: Line 3:


Put this file in your shared folder or shared game folder.
Put this file in your shared folder or shared game folder.
=== handviewmodel_shared.cpp ===
=== handviewmodel_shared.cpp ===
<div style="max-height:20em; overflow:auto;">
<source lang=cpp>
<source lang=cpp>
#include "cbase.h"
#include "cbase.h"
Line 35: Line 35:
END_NETWORK_TABLE()
END_NETWORK_TABLE()
</source>
</source>
</div>


I'd put something like this in your server/player.cpp or in your mod's server player.cpp below CreateViewModel.
I'd put something like this in your '''server/player.cpp''' or in your mod's server '''player.cpp''' below <code>CreateViewModel</code>.
 
=== server/player.cpp ===
=== server/player.cpp ===
<div style="max-height:20em; overflow:auto;">
<source lang=cpp>
<source lang=cpp>
void CBasePlayer::CreateHandModel(int index, int iOtherVm)
void CBasePlayer::CreateHandModel(int index, int iOtherVm)
Line 60: Line 59:
}
}
</source>
</source>
</div>


You should also add this to your server or mod's player.h below CreateViewModel.
You should also add this to your server or mod's '''player.h''' below <code>CreateViewModel</code>.
 
=== server/player.h ===
=== server/player.h ===
<div style="max-height:20em; overflow:auto;">
<source lang=cpp>
<source lang=cpp>
virtual void         CreateHandModel( int viewmodelindex = 1, int iOtherVm = 0 );
virtual void         CreateHandModel( int viewmodelindex = 1, int iOtherVm = 0 );
</source>
</source>
</div>


Now if you want this to actually work you're gonna need to call it.
Now if you want this to actually work you're gonna need to call it.
Call it sometime after CreateViewModel in either the BasePlayer or your mod's player spawn.
Call it sometime after <code>CreateViewModel</code> in either <code>CBasePlayer</code> or your mod's player spawn.
 
=== server/player.cpp ===
=== server/player.cpp ===
<div style="max-height:20em; overflow:auto;">
<source lang=cpp>
<source lang=cpp>
CreateHandModel();
CreateHandModel();
</source>
</source>
</div>


So now that the HandViewModel has been created we need to set its model.
So now that the <code>HandViewModel</code> has been created we need to set its model.
Put this in your mod's server's player's spawn sometime after the handmodel has been created.  
Put this in your mod's server's player's spawn sometime after the hand model has been created.  
 
=== server/your_mod/your_player.cpp ===
=== server/your_mod/your_player.cpp ===
<div style="max-height:20em; overflow:auto;">
<source lang=cpp>
<source lang=cpp>
GetViewModel(1)->SetModel("models/coolhandsfolder/cool_arms.mdl");
GetViewModel(1)->SetModel("models/coolhandsfolder/cool_arms.mdl");
</source>
</source>
</div>
 
Make sure your model has been precached first!
Make sure your model has been precached first!


Side note this might not work perfectly on Alien Swarm since it does weird stuff sometimes with bone merging.
{{note|This might not work perfectly on the {{As}} Alien Swarm branch since it does weird stuff sometimes with bone merging.}}


[[Category: Programming]][[Category: Tutorials]]
[[Category: Programming]][[Category: Tutorials]]

Revision as of 15:40, 7 March 2020

Have you ever wanted fancy c_ models like all those cool games? Well now you can!

Put this file in your shared folder or shared game folder.

handviewmodel_shared.cpp

#include "cbase.h"
#include "baseviewmodel_shared.h"

#if defined( CLIENT_DLL )
#define CHandViewModel C_HandViewModel
#endif

class CHandViewModel : public CBaseViewModel
{
	DECLARE_CLASS( CHandViewModel, CBaseViewModel );
public:
	DECLARE_NETWORKCLASS();
private:
};

LINK_ENTITY_TO_CLASS(hand_viewmodel, CHandViewModel);
IMPLEMENT_NETWORKCLASS_ALIASED(HandViewModel, DT_HandViewModel)

// for whatever reason the parent doesn't get sent 
// I don't really want to mess with the baseviewmodel
// so now it does
BEGIN_NETWORK_TABLE(CHandViewModel, DT_HandViewModel)
#ifndef CLIENT_DLL
SendPropEHandle(SENDINFO_NAME(m_hMoveParent, moveparent)),
#else
RecvPropInt(RECVINFO_NAME(m_hNetworkMoveParent, moveparent), 0, RecvProxy_IntToMoveParent),
#endif
END_NETWORK_TABLE()

I'd put something like this in your server/player.cpp or in your mod's server player.cpp below CreateViewModel.

server/player.cpp

void CBasePlayer::CreateHandModel(int index, int iOtherVm)
{
	Assert(index >= 0 && index < MAX_VIEWMODELS && iOtherVm >= 0 && iOtherVm < MAX_VIEWMODELS );

	if (GetViewModel(index))
		return;

	CBaseViewModel *vm = (CBaseViewModel *)CreateEntityByName("hand_viewmodel");
	if (vm)
	{
		vm->SetAbsOrigin(GetAbsOrigin());
		vm->SetOwner(this);
		vm->SetIndex(index);
		DispatchSpawn(vm);
		vm->FollowEntity(GetViewModel(iOtherVm), true);
		m_hViewModel.Set(index, vm);
	}
}

You should also add this to your server or mod's player.h below CreateViewModel.

server/player.h

virtual void	        CreateHandModel( int viewmodelindex = 1, int iOtherVm = 0 );

Now if you want this to actually work you're gonna need to call it. Call it sometime after CreateViewModel in either CBasePlayer or your mod's player spawn.

server/player.cpp

	CreateHandModel();

So now that the HandViewModel has been created we need to set its model. Put this in your mod's server's player's spawn sometime after the hand model has been created.

server/your_mod/your_player.cpp

	GetViewModel(1)->SetModel("models/coolhandsfolder/cool_arms.mdl");

Make sure your model has been precached first!

Note.pngNote:This might not work perfectly on the Alien Swarm Alien Swarm branch since it does weird stuff sometimes with bone merging.