Page 1 of 4

Those Simple "Source Programming Questions" Thread...

PostPosted: Sat Jun 02, 2012 10:42 am
by stoopdapoop
I hate having really small simple questions that are hard to google. They're too small to make your own thread about, but they're too annoying to find the answer to without asking for help. It be nice if there was a nice relaxing place where people could ask quick questions.

Hopefully this thread will become the place to ask such questions.

and I've got two to start off with.

In C++, is there a standard way to format a string (printf style) but instead of printing it to console, have it return a string or char array?

Something like the fictional "format" function here.
Code: Select all
int number = 5;
std::string coolText = format("the magic number is %i!!", number);

coolText would hold "the magic number is 5!!"

My other question is what does it mean when a struct is initialized with {0};? I sometimes see code like...
Code: Select all
CoolStruct myCoolStruct = {0};
and I'm not exactly sure what it means. I can guess what it does, but I'd rather know for sure.

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Sat Jun 02, 2012 10:56 am
by dark0r
stoopdapoop wrote:In C++, is there a way to format a string (printf style) but instead of printing it to console, have it return a string or char array?

Something like the fictional "format" function here.
Code: Select all
int number = 5;
std::string coolText = format("the magic number is %i!!", number);

coolText would hold "the magic number is 5!!"

In Source, there's a class called CFmtStr (see public/tier1/fmtstr.h). You can use it like:
Code: Select all
int number = 5;
CFmtStr coolText("the magic number is %i!!", number);

You can then pass coolText to any function which needs a const char * and it'll accept it. However if you want to print the value of coolText to console, you need to do:
Code: Select all
Msg("Hey, console! %s", coolText.Access());

Note the .Access() call.

For just normal C++, see:
http://stackoverflow.com/questions/1917 ... ng-and-int

stoopdapoop wrote:My other question is what does it mean when a struct is initialized with {0};? I sometimes see code like...
Code: Select all
CoolStruct myCoolStruct = {0};
and I'm not exactly sure what it means. I can guess what it does, but I'd rather know for sure.

That initializes all of the members of CoolStruct to 0 (i.e. it writes 0 to every bit in memory for that struct). It's the same as:
Code: Select all
CoolStruct myCoolStruct; // this can be initialised to anything right now, the memory isn't cleared
memset(&myCoolStruct, sizeof(myCoolStruct), 0); // clear the memory for myCoolStruct

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Sat Jun 02, 2012 11:13 am
by stoopdapoop
ah, perfect, thanks for the quick replies.

and yeah, I figured that's what {0}; did, but thanks for the confirmation.

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Sat Jun 02, 2012 5:26 pm
by zombie@computer
as for your first question, use sprintf

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Sat Oct 27, 2012 5:13 am
by LordDz
I have another little problem..
I want to attach a model to an npc_citizen when they spawn, but I can't find the entity to use to attach a model.

The code I have now, that doesn't work.

CBaseViewModel backmodel;
void CNPC_Citizen::Precache()
{
backmodel.SetModel("models/PG_props/pg_army/pg_army_backpack");
int citizenbackAttachment = LookupAttachment( "chest" );
GetAttachment( citizenbackAttachment , Should I use this function and what do I type here to attach it to the citizen? );
...
}

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Sat Oct 27, 2012 5:43 am
by Armageddon
Yeah how do I give the player a player model and then have that animate when he moves and uses things? And how do I have an entity that will freeze him and have the model play a certain animation for a moment? And a trigger that has a property to put in a camera name and when you touch the trigger the camera will change to that camera? It's a real hassle to use the output system on a trigger_multiple every time.

Thanks in advance.

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Sat Oct 27, 2012 11:59 pm
by Gambini
LordDz wrote:I have another little problem..
I want to attach a model to an npc_citizen when they spawn, but I can't find the entity to use to attach a model.

The code I have now, that doesn't work.

CBaseViewModel backmodel;
void CNPC_Citizen::Precache()
{
backmodel.SetModel("models/PG_props/pg_army/pg_army_backpack");
int citizenbackAttachment = LookupAttachment( "chest" );
GetAttachment( citizenbackAttachment , Should I use this function and what do I type here to attach it to the citizen? );
...
}


I don´t know if you want a map solution but you can attach anything to the origin of any attachpoint of a npc with the setparentattachment input. You can also do it by using a comma in its parent, for example PARENT: npcname,anim_attachment_RH but that last one will maintain an offset that I´m not sure how to solve.

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Sun Oct 28, 2012 7:41 am
by zombie@computer
Gambini wrote:
LordDz wrote:I have another little problem..
I want to attach a model to an npc_citizen when they spawn, but I can't find the entity to use to attach a model.

The code I have now, that doesn't work.

CBaseViewModel backmodel;
void CNPC_Citizen::Precache()
{
backmodel.SetModel("models/PG_props/pg_army/pg_army_backpack");
int citizenbackAttachment = LookupAttachment( "chest" );
GetAttachment( citizenbackAttachment , Should I use this function and what do I type here to attach it to the citizen? );
...
}


I don´t know if you want a map solution but you can attach anything to the origin of any attachpoint of a npc with the setparentattachment input. You can also do it by using a comma in its parent, for example PARENT: npcname,anim_attachment_RH but that last one will maintain an offset that I´m not sure how to solve.

If he doesn't, he only has to look up the code for setparentattachment. Shouldn't be that hard.

Armageddon wrote:Yeah how do I give the player a player model and then have that animate when he moves and uses things? And how do I have an entity that will freeze him and have the model play a certain animation for a moment? And a trigger that has a property to put in a camera name and when you touch the trigger the camera will change to that camera? It's a real hassle to use the output system on a trigger_multiple every time.

Thanks in advance.
The player model is defined somewhere (#define somewhere). Just replace the name with your own. Animations work automatically as long as you use the correct animation names.

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Sun Oct 28, 2012 12:09 pm
by LordDz
zombie@computer wrote:
Gambini wrote:
LordDz wrote:I have another little problem..
I want to attach a model to an npc_citizen when they spawn, but I can't find the entity to use to attach a model.

The code I have now, that doesn't work.

CBaseViewModel backmodel;
void CNPC_Citizen::Precache()
{
backmodel.SetModel("models/PG_props/pg_army/pg_army_backpack");
int citizenbackAttachment = LookupAttachment( "chest" );
GetAttachment( citizenbackAttachment , Should I use this function and what do I type here to attach it to the citizen? );
...
}


I don´t know if you want a map solution but you can attach anything to the origin of any attachpoint of a npc with the setparentattachment input. You can also do it by using a comma in its parent, for example PARENT: npcname,anim_attachment_RH but that last one will maintain an offset that I´m not sure how to solve.

If he doesn't, he only has to look up the code for setparentattachment. Shouldn't be that hard.

Armageddon wrote:Yeah how do I give the player a player model and then have that animate when he moves and uses things? And how do I have an entity that will freeze him and have the model play a certain animation for a moment? And a trigger that has a property to put in a camera name and when you touch the trigger the camera will change to that camera? It's a real hassle to use the output system on a trigger_multiple every time.

Thanks in advance.
The player model is defined somewhere (#define somewhere). Just replace the name with your own. Animations work automatically as long as you use the correct animation names.


*Snip* I'll try a compile and see what happens.

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Tue Oct 30, 2012 5:29 am
by Armageddon
Thanks Z@C! All I had to do was search the model name. Now I need the model to show up in first person view, and I need to disable the mouse while in-game. Any ideas?

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Wed Oct 31, 2012 4:08 pm
by LordDz
With some small hacks, I managed to get it to work.
Image

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Fri Nov 02, 2012 1:00 pm
by Armageddon
So got the last things working, now I have a rather odd bug, when you are in a point_viewcontrol you don't take any damage? Also is there an easy way ti enable sprinting without the suit? Or removing the flashlight.

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Fri Nov 02, 2012 4:50 pm
by stoopdapoop
Arma, You're not even trying. Sprint and Flashlight code is plain as day in Hl2player.cpp

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Sat Nov 03, 2012 9:02 am
by LordDz
Your best way of understanding the Hl2 code:
F12
Right click + References.
Right click + Call Hierarchy

Re: Those Simple "Source Programming Questions" Thread...

PostPosted: Sun Nov 04, 2012 7:33 pm
by Ackart
So I've got a bit of a strange issue.

I've been trying to import the base zombie and zombie NPC from the HL2 codebase into the AS codebase so I can have a familiar NPC platform to jump off of. Everything seems to be working, even things I couldn't really care about (headcrab detachment and spawning, for instance) - however, zombies simply will not do damage to players when they attack. The attack animation is called, but from what I can tell, ClawAttack just isn't getting called.

I even went as far as to rip the MeleeAttack right out of the asw_drone_advanced NPC and tacked it into the base zombie but to no avail - even the msges that I've thrown into the functions aren't called - it's as if it's just not calling the ClawAttack or MeleeAttack function at all.

It's been a long time since I've touched source and I have never even looked at the AS codebase for more than five minutes, so I'm sure I'm missing something. Any points would be appreciated. I'll try and post the source later tonight - I don't have access to the files right now.