Those Simple "Source Programming Questions" Thread...

Grab your favourite IDE and tinker with the innards of game engines

Those Simple "Source Programming Questions" Thread...

Postby stoopdapoop on Sat Jun 02, 2012 10:42 am

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.
I'm Brown
Image
User avatar
stoopdapoop
Veteran
Veteran
 
Joined: Sun Aug 21, 2005 2:14 am
Location: Ann Arbor, MI

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

Postby dark0r on Sat Jun 02, 2012 10:56 am

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
dark0r
Been Here A While
Been Here A While
 
Joined: Sat Mar 25, 2006 10:16 am
Location: de_hell

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

Postby stoopdapoop on Sat Jun 02, 2012 11:13 am

ah, perfect, thanks for the quick replies.

and yeah, I figured that's what {0}; did, but thanks for the confirmation.
I'm Brown
Image
User avatar
stoopdapoop
Veteran
Veteran
 
Joined: Sun Aug 21, 2005 2:14 am
Location: Ann Arbor, MI

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

Postby zombie@computer on Sat Jun 02, 2012 5:26 pm

as for your first question, use sprintf
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

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

Postby LordDz on Sat Oct 27, 2012 5:13 am

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? );
...
}
User avatar
LordDz
May Contain Skills
May Contain Skills
 
Joined: Mon Sep 01, 2008 12:28 pm
Location: Hammer Crash Logs

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

Postby Armageddon on Sat Oct 27, 2012 5:43 am

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.
User avatar
Armageddon
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Sun Dec 14, 2008 5:53 am

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

Postby Gambini on Sat Oct 27, 2012 11:59 pm

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.
User avatar
Gambini
Sir Post-a-lot
Sir Post-a-lot
 
Joined: Mon Oct 20, 2008 1:52 am
Location: Buenos Aires, Argentina.

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

Postby zombie@computer on Sun Oct 28, 2012 7:41 am

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.
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

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

Postby LordDz on Sun Oct 28, 2012 12:09 pm

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.
User avatar
LordDz
May Contain Skills
May Contain Skills
 
Joined: Mon Sep 01, 2008 12:28 pm
Location: Hammer Crash Logs

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

Postby Armageddon on Tue Oct 30, 2012 5:29 am

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?
User avatar
Armageddon
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Sun Dec 14, 2008 5:53 am

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

Postby LordDz on Wed Oct 31, 2012 4:08 pm

With some small hacks, I managed to get it to work.
Image
User avatar
LordDz
May Contain Skills
May Contain Skills
 
Joined: Mon Sep 01, 2008 12:28 pm
Location: Hammer Crash Logs

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

Postby Armageddon on Fri Nov 02, 2012 1:00 pm

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.
User avatar
Armageddon
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Sun Dec 14, 2008 5:53 am

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

Postby stoopdapoop on Fri Nov 02, 2012 4:50 pm

Arma, You're not even trying. Sprint and Flashlight code is plain as day in Hl2player.cpp
I'm Brown
Image
User avatar
stoopdapoop
Veteran
Veteran
 
Joined: Sun Aug 21, 2005 2:14 am
Location: Ann Arbor, MI

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

Postby LordDz on Sat Nov 03, 2012 9:02 am

Your best way of understanding the Hl2 code:
F12
Right click + References.
Right click + Call Hierarchy
User avatar
LordDz
May Contain Skills
May Contain Skills
 
Joined: Mon Sep 01, 2008 12:28 pm
Location: Hammer Crash Logs

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

Postby Ackart on Sun Nov 04, 2012 7:33 pm

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.
User avatar
Ackart
Member
Member
 
Joined: Tue Jun 10, 2008 9:32 pm
Location: Austin, TX
Next

Return to Programming

Who is online

Users browsing this forum: No registered users

cron