Forum

.float vs. float

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

.float vs. float

Postby Mexicouger » Fri Jul 09, 2010 11:29 pm

I know the differences between a float and a .float.

I think How it goes is that a float is global(I don't know) their values can only be changed in defs.qc(Or whatever places it was defined). And .floats can be changed.

But what is the difference in this?

Code: Select all
in defs.qc
float BROKEN = 25;

now in some qc file. Is there a difference between doing this

if (self.health <= BROKEN)
{
//do stuff;
}
and

if (self.health <= 25)
{
//do stuff;
}


Or is it just For visual purposes(knowing why the Number is 25)
I would really like to know.
Thanks
User avatar
Mexicouger
 
Posts: 514
Joined: Sat May 01, 2010 10:12 pm

Postby GiffE » Sat Jul 10, 2010 12:41 am

.float is a field float. This means every entity has its own variable float value you can set, such as health. You use it like: self.mydotfloat = 1;

While a float is a global variable, it is not different per entity. An good example is the float time; This does not mean it cannot be changed, it just means its accessible across all functions.

These rules apply for any data-type you use as a .type or not. (.vector .entity .string)

In your example there is no difference in the code, however many use the first example as its more readable and if you have multiple tests like that it is significantly easier to just change the 1 variable than all of them.
GiffE
 
Posts: 170
Joined: Sun Oct 08, 2006 3:39 pm
Location: USA, CT

Postby frag.machine » Sat Jul 10, 2010 12:54 am

The reason is not exactly visual. Using constants makes code easier to read and to change (can you imagine someone using "4096" instead "IT_MYNEWWEAPON" in a mod and later discovering that needs to change every occurence of "4096" because it's the value of the already existing "IT_AXE" ? ).
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
User avatar
frag.machine
 
Posts: 2090
Joined: Sat Nov 25, 2006 1:49 pm

Postby Spike » Sat Jul 10, 2010 7:02 am

by convention, macros are upper case.
QC does not traditionally support macros, so instead of macos they're regular constants, but still upper case.
globals that are initialised are considered constant by default.
globals that are not initialised can be set freely (ie: self is such a variable global).

(I'll not point out that fields are actually globals too, if defined with global scope, and when a field is defined within a function, then its a regular local variable and doesn't reserve field space, hence the find builtin)
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Postby DukeInstinct » Sun Jul 11, 2010 12:17 am

You can change a float anywhere within it's scope as long as you didn't initialize it when you declared it.

In C++, you'd declare a constant by using the keyword "const" but in QuakeC just initializing a variable makes it a constant that cannot be changed.
Image
User avatar
DukeInstinct
 
Posts: 20
Joined: Sat Jul 10, 2010 11:20 pm
Location: DukeLand

Postby GiffE » Sun Jul 11, 2010 12:48 am

DukeInstinct wrote:You can change a float anywhere within it's scope as long as you didn't initialize it when you declared it.

In C++, you'd declare a constant by using the keyword "const" but in QuakeC just initializing a variable makes it a constant that cannot be changed.


Also worth noting AFAIK you cannot initialize a local variable.
GiffE
 
Posts: 170
Joined: Sun Oct 08, 2006 3:39 pm
Location: USA, CT

Postby Jukki » Sun Jul 11, 2010 4:42 pm

so can is his like .float needs entity (self,other etc) and float is like full games variable like rounds etc?

Sry i am dump dont speak english :D
Jukki
 
Posts: 214
Joined: Wed Apr 07, 2010 4:59 am


Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest