PDA

View Full Version : Attaching Staticmeshes to Player?



danimal'
11-11-2009, 06:10 PM
Can you attach Staticmeshes to the player? Note I don't mean Skeletalmeshes, that's a different beast (as in the attachment classes). What I'm trying to do is: I've spawned a custom staticmesh in-game and I want to attach it to my character.

I know in the editor you can Base one mesh on another, but I can't figure out anything similar looking through the code. Any example class or if anyone outright knows the function(), it would be appreciated!

danimal

chrustec
11-11-2009, 06:34 PM
hmmm, for one static meshes don't move, they are static. You'd want to look at using an InterpActor for this (a dynamic mesh) If this is a piece of Armour or something then maybe it needs to have some sort of ref node attached to the player mesh prior to export and then using unrealscript tell the mesh type to attach to that ref node - or perhaps you could have those pieces already attached to the character but hidden when the character spawns and then use unreal script to toggle them from a bHidden state (I couldn't say how to do this at this time as I am not sure myself but might be some food for thought) I would look into ref nodes and multiple objects for export through ActorX as a starting point though. I hope this helps ya out :)

Xaklse
11-11-2009, 07:05 PM
To attach any generic actor to another:

AttachedActor.SetBase(RootActor);

danimal'
11-11-2009, 07:09 PM
Wewt thank you! Yes I know what static means, it's sort of why I would prefer using them over skeletalmeshes with a physics asset tbh. Appreciate the responses, off to try Xaklse's suggestion.

danimal

musichopper
11-11-2009, 07:13 PM
This is a very good thing to be able to do since it is how you will allow the player to pick up objects in the world to manipulate them. Like picking up a light crate and moving it on top of something else to help you jump to reach something up high etc.

Solid Snake
11-11-2009, 07:32 PM
If you wish to just attach a static mesh onto a skeletal component on either bones or sockets, then use the relevant AttachComponent functions instead. It's wasteful on resources to attach actors when just attaching components would do.

In my game, I am currently just creating new StaticMeshComponent's or SkeletalMeshComponent's and then attaching them onto the player's SkeletalMeshComponent because I only required the visuals.

danimal'
11-11-2009, 07:50 PM
Well because I'm sadistic, I'm trying to attach a Constrained actor to my player. Who knows if this will ever work. I may have to just do it Whizzle style and create my own player not based off of a Pawn.

I sort of don't understand components, where do they fit in the world of bones and sockets?

Artist trying to code is always sort of laughable, am I in the ballpark?

danimal

Solid Snake
11-11-2009, 08:46 PM
Components in this case StaticMeshComponent and SkeletalMeshComponent. They represent the mesh rendering side of things. In older versions of Unreal Engine, these used to be bound with Actor's directly (and you switched between different modes of rendering an actor). In Unreal Engine 3, a component method was used so that an actor could control specifically how it wanted to be rendered. If you need sprites together with a skeletal mesh you just added those components in.

Bones in SkeletalMeshes are sort of self explanatory. Sockets is effectively a defined point that is relative to a skeletal mesh bone. Sockets can have a name which makes them easier to use as well. It makes code a little more stream lined as I could just say "Attach this weapon on the LeftHand socket" and then let the artist decide where the LeftHand socket is actually located on the skeletal mesh.

Component based design is similar to Unity in this regard, although I should say Unity is like UE3 since UE3 came out before Unity :)

danimal'
11-11-2009, 09:05 PM
Well you tried! A bit beyond me, I'll go dig around documentation to try to understand better. Mind giving a super simple example of how using the AttachComponent system you would attach ItemX to PlayerY? I learn mostly by example myself. Appreciate the response.

danimal

Solid Snake
11-12-2009, 12:29 AM
If you have UT3, try looking up the UTWeaponAttachment stuff. That one is a pretty classic example.

danimal'
11-12-2009, 03:50 AM
I'm seriously not getting something. I spawn something like so:


Hook = Spawn(class'MyMod.GrappleHook', , '', Instigator.Location);


It happily spawns wherever I am at the moment, makes sense. I then want to attach the bloody thing to my player. I try to do something like:


Hook.SetBase(UTPlayerController);


It compiles, doesn't complain at me (like if I tried attaching it to a Pawn), but does absolutely nothing. What am I doing wrong? All I want to do is spawn my custom class and attach it to my player :/

danimal

Angel_Mapper
11-12-2009, 04:34 AM
Have you tried attaching it to the Pawn? iirc the PlayerControllers (the actual actors) don't follow the Pawns around.

schizoslayer
11-12-2009, 05:59 AM
I'm seriously not getting something. I spawn something like so:


Hook = Spawn(class'MyMod.GrappleHook', , '', Instigator.Location);


It happily spawns wherever I am at the moment, makes sense. I then want to attach the bloody thing to my player. I try to do something like:


Hook.SetBase(UTPlayerController);


It compiles, doesn't complain at me (like if I tried attaching it to a Pawn), but does absolutely nothing. What am I doing wrong? All I want to do is spawn my custom class and attach it to my player :/

danimal

The PlayerController doesn't move around. You want to attach it to the Pawn which is the physical representation of the player.

danimal'
11-12-2009, 01:34 PM
Yah I've tried this, still nothing. Is it because the thing I'm trying to attach is a KAsset? To be clear, I made a custom KAsset (which has a skeletal mesh), and am trying to attach it to my character (a derivative of UTPawn). In brief:

Custom KAsset (just using the flag for now to ensure it's not a mesh problem on my end):

class GrappleHook extends KAssetSpawnable

DefaultProperties
{
Begin Object Name=KAssetSkelMeshComponent
Animations=None
SkeletalMesh=SkeletalMesh'CTF_Flag_IronGuard.Mesh. S_CTF_Flag_IronGuard'
PhysicsAsset=PhysicsAsset'CTF_Flag_IronGuard.Mesh. S_CTF_Flag_IronGuard_Physics'
bHasPhysicsAssetInstance=true
End Object
}

From my weapon:


Hook = Spawn(class'MyMod.GrappleHook', , '', Instigator.Location);
Hook.SetBase(Instigator);

It spawns the flag (again I'm just using it as a placeholder), but absolutely positively does not attach it. I've tried UTPawn, Owner, Instigator, etc etc, it compiles fine but never attaches it. Argh?! What am I doing wrong :/

danimal

danimal'
11-12-2009, 04:40 PM
Bumpity b/c I just edited my previous response, should have put it down here ;) I've tried Pawn (see post directly above), it doesn't seem to work :/

danimal

mikepurvis
11-12-2009, 05:08 PM
Have you tried attachComponent ?

danimal'
11-12-2009, 05:59 PM
Have you tried attachComponent ?

Yah, but I'm not sure I understand how to use it properly. My KAsset has a component (KAssetSkelMeshComponent) in the default properties (see above), but I'm not sure I'm using the syntax right from inside the weapon after I spawn it. I tried
UTPawn.AttachComponent(Hook.KAssetSkelMeshComponen t) and a few other variations, but the compiler yells at me that I make no sense :/

Can you give an example of the proper syntax/format? I wouldn't think attaching something to the player would be this taxing :/ And I still don't understand why Base isn't working for me. Sigh!

danimal

danimal'
11-14-2009, 03:30 AM
For any poor soul who tries to do this, this is what I *finally* figured out about attaching skeletal meshes to the player (pawn):

Define up top in your class:

var SkeletalMeshComponent yourmesh;
//"yourmesh" just serves as a reference to SkeletalMeshComponent

Then call during your function:

Pawn.mesh.AttachComponent(yourmesh, 'bone_name_here');
//note for me since I'm calling from my gun, I actually use:
//Instigator.mesh.AttachComponent(yourmesh, 'b_Spine');
//In english we're saying: attach to the mesh of my pawn, the thing we defined as "yourmesh", at the //bone named 'b_Spine' on the player mesh


Down below in your default properties:


//this is where we're going to create the component itself as shown below (this wasn't intuitive to me //that we're essentially creating the component simply by defining it in the defaults)
//we're also going to define what skeletal mesh we want to attach to our player
defaultproperties
{
Begin Object class=SkeletalMeshComponent name=attachMesh
SkeletalMesh=SkeletalMesh'Your_Mesh_Reference_Here '
End Object
yourMesh=attachMesh
}

The huge mistake I made was that I thought about it wrong. I was trying to spawn a mesh with Spawn() and then to attach it. That's completely wrong, there's no spawning involved. All you do is create the component (in default properties), define the mesh you want to attach, then use the above string to actually call it in a function. Hopefully this prevents someone else spending the inordinate amount of time I did :/

danimal

pk3
12-17-2009, 08:28 AM
Can I use AttachComponent(yourmesh, 'bone_name_here'); with an Actor instead of a Pawn?

musilowski
01-30-2010, 06:08 AM
Bump. So to be certain, I just need to declare a new StaticMeshComponent in default properties, then assign it to a component variable and then attach it in PostBeginPlay or some such?

How can I get random rotations and overlay meshes which align correctly?

ViiK
03-01-2010, 07:54 AM
Can I use AttachComponent(yourmesh, 'bone_name_here'); with an Actor instead of a Pawn?

Yes, at least it worked for me on KActorSpawnable object.
The only issue I can't resolve is that attached component is an SkeletalMesh with specified physics set, collision works but bones dosn't move. So it behaives as a static mesh rather than a skeletal.

axelzellalex
06-23-2010, 08:24 PM
so if you want to attach a mesh it has to be skeletal?
your code has worked for me - i used the flag thing too

but now im trying to change it for a static mesh, with not much luck

(edit)
yes you can, never mind
thanks danimal! =)

Winwarproductions
06-24-2010, 03:41 PM
Just made a Creating a malee system in UDK's Kismet. Also a creating enemy pawns as well, to follow and attack.

UDK Tutorial Malee Attack in Kismet Part1
http://www.youtube.com/watch?v=rM7EbIC42VE

UDK Tutorial Malee Attack in Kismet Part1
http://www.youtube.com/watch?v=-gX9KCEVHOk

UDK Tutorial Finishing Malee Attack And Starting Enemies in Kismet
http://www.youtube.com/watch?v=PKycVeGGikg


My Portfolio
http://michaeljcollinsdreamart.com
or
http://winwarproductions@yahoo.com

Alfier
01-27-2011, 12:17 PM
I have a similar problem, but instead of attach the spawned skeletal mesh to Player's pawn, I want to attach it to another skeletal mesh that is already attached to Pawn. That's like attach a skeletal mesh to player's weapon.

In other words what I want to do is:

Begin game & spawn player's pawn -> spawn weapon1 -> attach weapon1 to pawn's socket -> spawn weapon2 -> attach weapon2 to weapon1 socket

So far, I was able to spawn and attach successfully only Weapon1, while I've lot of problems with Weapon2.

That's what I tried:

In MyPawn.uc

function AddDefaultInventory()
{

Weapon1=Spawn(class'MYWeapon1,self);
Weapon1.AttachWeaponTo(Mesh, Weapon_socket1);

Weapon1.AddSecondWeapon();

}

In Weapon1.uc

class MYWeapon1 extends MYWeapon1Group;


var MYWeapon2 Weapon2;
var name Weapon_socket2;

function AddSecondWeapon()
{

Weapon2=Spawn(class'MYWeapon2',self);
//Weapon2.AttachWeaponTo(Mesh, Weapon_socket2);

/*When I try to use AttachWeaponTo function it returns me a type mismatching error in parameter 1 (I don't get why though), so I went to an AttachComponentToSocket instead*/

Weapon2.AttachComponentToSocket(Mesh, Weapon_socket2);

}

In MYWeapon2Group.uc

class MYWeapon2Group extends UDKWeapon;

var name Weapon2AttachmentSocketName;

function AttachComponentToSocket(ActorComponent Component, name SocketName)
{

local MYWeapon1 V;

V = MYWeapon1(Instigator.Weapon);

/*Here is where the problem is...
whatever of the following lines I use, it returns the error "Unrecognized AttachComponentToSocket in class MYWeapon2"*/
//V.Mesh.AttachComponentToSocket(Mesh,Weapon2Attachm entSocketName);
Component.AttachComponentToSocket(Mesh,Weapon2Atta chmentSocketName);

}
DefaultProperties
{
Begin Object Class=SkeletalMeshComponent Name=SkeletalMeshComponent1
CollideActors=false
AlwaysLoadOnClient=true
AlwaysLoadOnServer=true
bUseAsOccluder=FALSE
bUpdateSkelWhenNotRendered=false
bIgnoreControllersWhenNotRendered=true
bOverrideAttachmentOwnerVisibility=true
bAcceptsDynamicDecals=FALSE

End Object


Components.Add(SkeletalMeshComponent1)
Mesh=SkeletalMeshComponent1

Weapon2AttachmentSocketName=Weapon_socket2
}


I could easily solve this problem attaching Weapon2 directly to player's pawn, but that's not what I want.
Any idea?

Alfier
01-28-2011, 04:47 AM
Sorry for bump but I need to solve this problem asap

Mougli
01-28-2011, 07:28 AM
The error messages are quite explicit.

You get a type mismatch because you're not passing the proper kind of parameter. Look at the function's declaration and see what kind of value you need to pass.

The second error means you're calling a function on a object that doesn't define the said function. Look at where this function is defined.

I can't tell you as I don't have access to my code right now.

Alfier
01-28-2011, 08:31 AM
You get a type mismatch because you're not passing the proper kind of parameter. Look at the function's declaration and see what kind of value you need to pass.

Yes, the function wants a SkeletalMeshComponent to be passed, but what I'm passing is the skeletal mesh component of my custom weapon class, so I don't know what's wrong...

For the second error, it says that i'm calling a function on a object that doesn't define the said function, but the object is a subclass of UDKWeapon. That means UDKWeapon doesn't have an AttachComponentToSocket function? And if it's the case, what and where should I put this function?

Blade[UG]
01-28-2011, 11:38 AM
AttachComponentToSocket is in the SkeletalMeshComponent

Alfier
01-28-2011, 11:47 AM
;27893836']AttachComponentToSocket is in the SkeletalMeshComponent

Ok, thank you, but unfortunately I'm dumb and still can't think of a way to solve the problem...

It's enough to copy and past

native final function AttachComponentToSocket(ActorComponent Component, name SocketName);
from SkeletalMeshComponent.uc to my custom class?

Blade[UG]
01-28-2011, 11:55 AM
No, you need to call the function IN the Weapon Mesh.

if your Weapon's Mesh is stored in "Mesh", then: Mesh.AttachComponentToSocket()

Alfier
01-28-2011, 03:38 PM
;27893865']No, you need to call the function IN the Weapon Mesh.

if your Weapon's Mesh is stored in "Mesh", then: Mesh.AttachComponentToSocket()

I already tried this way, and this is the error I get


Unrecognized member AttachComponentToSocket in class mesh component

I tried a lot of ways, even stupid things, nothing worked...

Mougli
01-28-2011, 03:42 PM
Weapon.Mesh is a MeshComponent

AttachComponentToSocket() is a member of SkeletalMeshComponent.

You need to typecast Mesh into a SkeletaMeshComponent:

SkeletalMeshComponent(Mesh).AttachComponentToSocke t()

Alfier
01-28-2011, 03:49 PM
Weapon.Mesh is a MeshComponent

AttachComponentToSocket() is a member of SkeletalMeshComponent.

You need to typecast Mesh into a SkeletaMeshComponent:

SkeletalMeshComponent(Mesh).AttachComponentToSocke t()

That's my code


function AttachComponentToSocket(ActorComponent Component, name SocketName)
{

local Weapon1 V;


V = Weapon1(Instigator.Weapon);

V.SkeletalMeshComponent(Mesh).AttachComponentToSoc ket(Component,SocketName);

}

and this is the error


Unrecognized member SkeletalMeshComponent in class Weapon1

What I'm trying to do is call from class Weapon1 the function to spawn Weapon2 and attach it to Weapon1. What I'm doing wrong?

Mougli
01-28-2011, 03:54 PM
...
It's SkeletalMeshComponent(V.Mesh).AttachComponentToSoc ket()

You should read that: http://udn.epicgames.com/Three/UnrealScriptReference.html#Converting object references among classes

Alfier
01-28-2011, 04:16 PM
...
It's SkeletalMeshComponent(V.Mesh).AttachComponentToSoc ket()

You should read that: http://udn.epicgames.com/Three/UnrealScriptReference.html#Converting object references among classes

Asd ok thank you, I simply can't remember everything I read (I remember very few of the things I read though lol). That problem is solved, but another one comes out...

I need to attach weapon2 to weapon1, so following the code I first have to define the Weapon1 instigator, so weapon 2 can attach to.


simulated function AttachWeaponTo( SkeletalMeshComponent MeshCpnt, optional Name SocketName )
{

local Weapon1 V;

V = Weapon1(Instigator.Weapon);

SkeletalMeshComponent(V.Mesh).AttachComponentToSoc ket(MeshCpnt,SocketName);
}

This way I get no errors, but the code still doesn't work and in log V is marked as empty. Searching for instigator i couldn't find a way to have it to work with weapon instead of pawn...

Blade[UG]
01-28-2011, 11:02 PM
if V is None, then Instigator.Weapon is not a Weapon1.

There's no reason to typecast to your custom weapon here, anyway.


SkeletalMeshComponent(Instigator.Weapon.Mesh).Atta chComponentToSocket(MeshCpnt, SocketName);

Alfier
01-29-2011, 03:36 AM
;27894880']if V is None, then Instigator.Weapon is not a Weapon1.

There's no reason to typecast to your custom weapon here, anyway.


SkeletalMeshComponent(Instigator.Weapon.Mesh).Atta chComponentToSocket(MeshCpnt, SocketName);

Still won't work :( Log's telling me

ScriptWarning: Accessed None 'Weapon'
referring to instigator weapon


I can't think of a reason for this...
This is the code, I hope that someone can help me to solve this mess.

function AddDefaultInventory()
{

Weapon1=Spawn(class'MYWeapon1',self);
Weapon1.AttachWeaponTo(Mesh, Weapon_Right);

Weapon1.AddWeapon();

}



function AddWeapon()
{
Weapon2=Spawn(class'MYWeapon2',self);
Weapon2.AttachWeaponTo(SkeletalMeshComponent(Mesh) , Elsa_Joint);

}


simulated function AttachWeaponTo( SkeletalMeshComponent MeshCpnt, optional Name SocketName )
{

super.AttachWeaponTo(MeshCpnt, SocketName);

SkeletalMeshComponent(Instigator.Weapon.Mesh).Atta chComponentToSocket(MeshCpnt,SocketName);
}

Alfier
01-29-2011, 02:26 PM
I just tried this other way


function AddDefaultInventory()
{
Weapon1=Spawn(class'MYWeapon1',self);
Weapon1.AttachWeaponTo(Mesh, Weapon_Right);

Weapon2=Spawn(class'MYWeapon2',self);
if(Weapon2 != none) `log("Weapon2 SPAWNED !!!!!!");

SkeletalMeshComponent(Weapon2.Mesh).AttachComponen tToSocket(SkeletalMeshComponent(Weapon1.Mesh),Weap on2_Joint);

}

putting this function directly into MyPawn.uc
Now thanks to the log I know for sure that Weapon2 has been spawned, but still it won't appear nor attach to weapon1...
I really am stuck at this, not even this that should be the simplest way to achieve the result works...

Blade[UG]
01-29-2011, 06:48 PM
what are you trying to attach a weapon to another weapon?

Alfier
01-30-2011, 04:45 AM
;27896588']what are you trying to attach a weapon to another weapon?

Yes, that's exactly what I want to do (I said that 10 posts ago though :p). Think of it like a weapon with various interchangeable components. I need to attach weapon 2 directly to weapon 1 for the simple reason that the socket position changes from the various types of weapon 1 (into a type of weapon 1 it could be at the top, into another at the bottom).

So are you trying to tell me that I simply can't attach a weapon to another weapon? >_< If that's so, it sucks, but I guess I can simply change the parent class of the custom weapon to attach, so it doesn't extends UDKWeapon anymore, right?

Edit: I REALLY am a dumbass...

function AddDefaultInventory()
{
Weapon_Right='Weapon_Right';
Weapon2_Joint='Weapon2_Joint';

Weapon1=Spawn(class'MYWeapon1',self);
Weapon1.AttachWeaponTo(Mesh, Weapon_Right);

Weapon2=Spawn(class'MYWeapon2',self);
Weapon2.AttachWeaponTo(SkeletalMeshComponent(Weapo n1.Mesh), Weapon2_Joint);

}

I forgot to assign value to the sockets variables, no wonder it didn't work...

Thank you guys :D I really appreciate your help!

Blade[UG]
01-30-2011, 09:15 AM
You might want to have a look at AttachWeaponTo(), I'm not sure that it supports this sort of usage.

I meant to ask "WHY are you trying to attach one weapon to another weapon?" .. when you could just be using individual SkeletalMeshComponents, most likely.

Alfier
01-30-2011, 03:26 PM
;27897550']You might want to have a look at AttachWeaponTo(), I'm not sure that it supports this sort of usage.
The code now works, so I guess it supports this sort of usage.

;27897550']
I meant to ask "WHY are you trying to attach one weapon to another weapon?" .. when you could just be using individual SkeletalMeshComponents, most likely.
Why not? There is some sort of problem extending from weapon instead that doing it from SkeletalMeshComponent? Something like performance issues?

Blade[UG]
01-30-2011, 05:13 PM
well, unless you're re-writing the rest of the weapon system, there's no way to operate two weapon items at once :)

Alfier
01-31-2011, 04:00 AM
;27898224']well, unless you're re-writing the rest of the weapon system, there's no way to operate two weapon items at once :)

I see... well, I guess it's enough to change the class' parent from UDKWeapon to SkeletalMeshComponent, hoping that nothing will go wrong lol
Thank you!