Forum

More code migraines from lack of quakeC lore

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

More code migraines from lack of quakeC lore

Postby TargetPractice » Thu Nov 08, 2007 5:11 am

I've run into yet another hive of bugs with my efforts at coding. I think the error results from my lack of understanding of how to use trace_ent, how to check for a flag, and how to bond strings. Not to mention general rustiness (I've been focused on other matters).

The game seems to be ignoring my command to activate the code entirely, despite my invoking it with an impulse. FrikQCC claims that the code has 'no errors'.

The code in question(shortened):

void() Scan =
{
local vector src;
makevectors(self.v_angle);
src = self.origin + v_forward*10;
src_z = self.absmin_z + self.size_z * 0.7;//Stolen from axe code
traceline(src, src+v_forward*2048, FALSE, self);

//Yes, max range is 2048.
if ((trace_fraction == 1) || (trace_ent.classname == "world") || (trace_ent.flags & FL_MONSTER)) //Potential error
return;
//Has to hit a monster- first two check to see if it hit something, third checks to see if it wants to kill you.


string maxhealth, monstname, paininneck; //Strings to be incorporated into the centerprint later.

//This is a giant IF loop; the full loop lists ALL the monsters in quake. Trust me when I say that I'm saving you time by showing the first example.
//No ELSE statements; frikQCC complained for some reason, and quieted when I removed them. I'll try reinserting them later.
if (trace_ent.classname == "monster_army")
{
monstname = "Soldier";//Name from manual
maxhealth = "30";//From code, initial health
}

// paininneck = (monstname, "\n" + ftos(trace_ent.health) + "/" + maxhealth + "\n");


centerprint(self, monstname/* + "\n" + ftos(trace_ent.health) + "/" + maxhealth + "\n"*/);//Commented out code here to try to get the thing to quit complaining about improper centerprint use.
centerprint(self, "\n");
};


//Inserted between 12 and 255 in-code
if (self.impulse == 13)
Scan ();

I think these are the only alterations to the code. Thank you for any aid you offer, in advance.
TargetPractice
 
Posts: 17
Joined: Thu Nov 30, 2006 9:24 pm

Postby Sajt » Thu Nov 08, 2007 5:47 am

You can't call centerprint twice like that, the second one will override it and just print an invisible newline...

Just skip the "\n" printing, because centerprints don't need the newline at the end like sprint/bprint does.

Also, + isn't a string concatenator in QuakeC. Quake originally had no string concatenation support (every string was constant). What you can do is redefine centerprint in defs.qc as:

void(entity e, string s, ...) centerprint = #whatever;

This should allow you to pass one or more strings into centerprint and they will basically be concatenated by the engine centerprint() implementation.

centerprint(self, monstname, "\n", ftos(trace_ent.health), "/", maxhealth);

But you can only use 8 arguments in a call to a builtin function, so watch out. A more portable alternative without varargs (which requires FrikQCC) is:

void(entity e, string a) centerprint = #whatever;
void(entity e, string a, string b) centerprint2 = #whatever;
void(entity e, string a, string b, string c) centerprint3 = #whatever;
...
void(entity e, string a, string b, string c, string d, string e, string f, string g) centerprint7 = #whatever;

Also, if you were to call ftos() twice it would bugger up (requiring strzone probably) but it's a good thing you aren't... Actually, it looks like you are already aware of this, as you are storing maxhealth in a string...
F. A. Špork, an enlightened nobleman and a great patron of art, had a stately Baroque spa complex built on the banks of the River Labe.
Sajt
 
Posts: 1215
Joined: Sat Oct 16, 2004 3:39 am

Postby Spike » Thu Nov 08, 2007 12:08 pm

(trace_ent.flags & FL_MONSTER)

You probably want to logical-not (!) that too.



and yeah, string+string is meant to be a syntax error, except that frikqcc silently ignores them because they could be really really evil hacks that otherwise work. FTEQCC generates errors. :)

Small note:
void(...) centerprint = #whatever;
The above works for any qcc (when compiled its the same as the multiple versions of centerprint, so works with any engine too). But note that while you can do it like that in any qcc, you loose type checking.
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Postby TargetPractice » Thu Nov 08, 2007 2:57 pm

Okay, let's see if I understand what to do correctly:

1)Peel off the extra newline centerprint.
2)Fiddle with defs.qc to create concatenated string lines(I'll use the latter technique; it may be less elegant, but it's more universal)
3)Negate the FL_Monster check to its correct application
4)Alter my code to perfect the effects intended

I found out about ftos() when I was glancing through the wiki.quakesrc.org for code functions I could use; I saw it, took a look at its description, and learned about the bug. The other reason I handled maxhealth like that is because I failed to observe any "self.maxhealth = wakka" lines attached to the monsters' code, and so decided to store them in the main function of the new code (makes it easier to transplant into another mod).

Would it be possible to overload centerprint thusly?

void(entity e, string a) centerprint = #whatever;
void(entity e, string a, string b) centerprint = #whatever;
void(entity e, string a, string b, string c) centerprint = #whatever;
//Repeat for all 8 variants

Thanks for the aid.
I never thought hearing pained, fearful screams could make one feel so... uplifted.

What?
TargetPractice
 
Posts: 17
Joined: Thu Nov 30, 2006 9:24 pm

Postby Urre » Thu Nov 08, 2007 3:26 pm

Note that the "whatever" is actually a specific builtin number, you'll need to look it up (dpmod source is one place to find those). Doing what you said won't work, it will probably complain about re-defining a builtin or something. They need individual names. And because of that, I'd recommend using the centerprint(entity e, string s, ...) technique, since that allows virtually infinite strings to concatenate into a single call, as well as the flexibility to choose the amount of parameters to feed it.
I was once a Quake modder
User avatar
Urre
 
Posts: 1109
Joined: Fri Nov 05, 2004 2:36 am
Location: Moon

Postby TargetPractice » Fri Nov 09, 2007 12:33 am

I am now aware of the error of trying to overload centerprint() like I suggested; frikQCC complained when I tried it.

I've pulled the appropriate max health for each critter from the appropriate creature QC files.

And now I have the function working. I had to make the code recognize Cthon and Shubby, but it works.

Now to form a tutorial. Thanks, all!
I never thought hearing pained, fearful screams could make one feel so... uplifted.

What?
TargetPractice
 
Posts: 17
Joined: Thu Nov 30, 2006 9:24 pm


Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest