arrays in quake?

Discuss programming in the QuakeC language.
Post Reply
ceriux
Posts: 2230
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

arrays in quake?

Post by ceriux »

from my knowledge this is how your write normal arrays.

Code: Select all

islot[0] ="";
islot[1] =x;

eslot[0] ="";
eslot[1] =x;

stat[0] =x;
is it the same in quake? and can quake use two-dimensional arrays?

Code: Select all

islot[i,0] ="";
islot[i,1] =x;
if not how do you use arrays in quake? (darkplaces?)
Cobalt
Posts: 445
Joined: Wed Jun 10, 2009 2:58 am
Location: New England, USA
Contact:

Re: arrays in quake?

Post by Cobalt »

Good questions. BTW, what would be an example useage for arrays in QC?
ceriux
Posts: 2230
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Re: arrays in quake?

Post by ceriux »

Cobalt wrote:Good questions. BTW, what would be an example useage for arrays in QC?
i think the examples i posted is how they're written . however i may be wrong, that's why i'm asking?
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: arrays in quake?

Post by Baker »

See this FTEQW page:

http://fteqw.com/wiki/index.php?title=FTEQCC_FAQ

Code: Select all

float anarray[64];
for (i = 0; i < 64; i++)
    anarray[i] = somecomplexfunction(i);
blah = anarray[floor(random()*64)];
As far as I know, only using FTEQCC will you have arrays. FrikQCC or other non-fteqcc do not have arrays.

Note: It might insane to add something like .float something[<ARRAYSIZE>] in defs.qc ... remember that every entity will have that field. Might consume a rather large chunk of memory.
Last edited by Baker on Thu Jan 26, 2012 4:17 am, edited 1 time in total.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Arkage
Posts: 66
Joined: Thu Nov 19, 2009 4:17 pm

Re: arrays in quake?

Post by Arkage »

Can't you test then and see?

I remember reading something about having to wrap them in round brackets in FTEQCC.
OneManClan
Posts: 247
Joined: Sat Feb 28, 2009 2:38 pm
Contact:

Re: arrays in quake?

Post by OneManClan »

Cobalt wrote:Good questions. BTW, what would be an example useage for arrays in QC?
Here's an example of an array in (FTEQW) QuakeC:
http://forums.inside3d.com/viewtopic.ph ... it=payload

While the 'payload' code has (major) flaws, I believe the actual array part of the code is correct, and (IIRC) worked!
Jukki
Posts: 214
Joined: Wed Apr 07, 2010 4:59 am

Re: arrays in quake?

Post by Jukki »

Yes it worked. But it didnt do so well with huge functions and stuff like that.
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: arrays in quake?

Post by Spike »

with fteqcc:
.float foo[64];
defines a field array with 64 elements.

self.foo[y] = 5;
sets element 'y' in that array to 5.

As Arkage said, older versions of fteqcc require extra brackets for field arrays: self.(foo[y]) = 5; (which kinda illustrates how fields work internally). The current version doesn't need that, but does accepts the extra brackets if you want to be paranoid.

FTEQCC's arrays are single-dimension only, thus you need to do: [x+y*width] to get 2d functionality.
As Baker said, remember that this creates 64 fields and will increase your entity size and memory footprint by quite a bit, so if you're on an embedded system you may need to be more cautious.

If you're not using any opcode extensions (ie: no -TFTE) then both reading and writing arrays will be emulated with a function call, containing a binary search to get the element, which can be somewhat slow to access.
This means that you cannot define arrays inside another function (you also cannot pass an array from one function to another).
r00k
Posts: 1111
Joined: Sat Nov 13, 2004 10:39 pm

Re: arrays in quake?

Post by r00k »

example

Code: Select all

entity dm_spawns[16]; //plenty, i dont think many maps have more than 8 spawns

entity() SelectSpawnPoint =
{
	local	entity spot;
	local	entity thing;
	local	float  pcount;
	local float r;
	
	if (deathmatch)
	{
		spot = lastspawn;
		
		while (1)
		{
			r = rint ((random ()) * num_dm_spawns);									// choose a random % based on total number of spawns

			spot = dm_spawns[r]; 													// pick that one
			
			if (spot != world)														// Now scan in a 32 radius circle for anyone nearby.
			{
				if (spot == lastspawn)
					return lastspawn;
Cobalt
Posts: 445
Joined: Wed Jun 10, 2009 2:58 am
Location: New England, USA
Contact:

Re: arrays in quake?

Post by Cobalt »

Good example !

Darn, Darkplaces dont like : entity dm_spawns [16];

guess it wont work for an idea I had.


[quote="r00k"]example
ceriux
Posts: 2230
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Re: arrays in quake?

Post by ceriux »

so if i were to use arrays to define stats and inventory it would mean in a multiplayer game since all entities share these fields that each player would carry the same items and have the same stats? if so it seem's arrays would be okay for single player, but what if i wanted to do multiplayer ? how does prydon do these things?
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: arrays in quake?

Post by Baker »

cerium wrote:... how does prydon do these things?
Prydon source code: Prydon Source
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Arkage
Posts: 66
Joined: Thu Nov 19, 2009 4:17 pm

Re: arrays in quake?

Post by Arkage »

ceriux wrote:so if i were to use arrays to define stats and inventory it would mean in a multiplayer game since all entities share these fields that each player would carry the same items and have the same stats? if so it seem's arrays would be okay for single player, but what if i wanted to do multiplayer ? how does prydon do these things?
it should be the same as any other var that is unique to an entity.
So if you declare .string derp[4];
Then every entity should have a string array of 4 elements (not the same array).
So:
Player1.derp[0] = "Herp";
and
Player2.derp[0] = "not herp";
Don't hold the same thing.
ceriux
Posts: 2230
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Re: arrays in quake?

Post by ceriux »

right, but you can't really define player1,player2. it's simply player. unless the engine would handle the difference between players?

baker: so this is how darkplaces would use arrays? e.inv_6_4 = val;
Arkage
Posts: 66
Joined: Thu Nov 19, 2009 4:17 pm

Re: arrays in quake?

Post by Arkage »

I just used player1 and 2 as an example. All players are entity's and so every entity has that .herp array. You just have to find the entity for that player.
Unless I misunderstood what you meant?
Post Reply