Forum

arrays in quake?

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

arrays in quake?

Postby ceriux » Wed Jan 25, 2012 8:12 pm

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?)
User avatar
ceriux
 
Posts: 2223
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Re: arrays in quake?

Postby Cobalt » Thu Jan 26, 2012 1:22 am

Good questions. BTW, what would be an example useage for arrays in QC?
User avatar
Cobalt
 
Posts: 445
Joined: Wed Jun 10, 2009 2:58 am
Location: New England, USA

Re: arrays in quake?

Postby ceriux » Thu Jan 26, 2012 3:11 am

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?
User avatar
ceriux
 
Posts: 2223
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Re: arrays in quake?

Postby Baker » Thu Jan 26, 2012 4:15 am

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 ..
User avatar
Baker
 
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: arrays in quake?

Postby Arkage » Thu Jan 26, 2012 4:17 am

Can't you test then and see?

I remember reading something about having to wrap them in round brackets in FTEQCC.
User avatar
Arkage
 
Posts: 66
Joined: Thu Nov 19, 2009 4:17 pm

Re: arrays in quake?

Postby OneManClan » Thu Jan 26, 2012 4:34 am

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:
viewtopic.php?f=2&t=4251&hilit=payload

While the 'payload' code has (major) flaws, I believe the actual array part of the code is correct, and (IIRC) worked!
OneManClan
 
Posts: 243
Joined: Sat Feb 28, 2009 2:38 pm

Re: arrays in quake?

Postby Jukki » Thu Jan 26, 2012 5:19 am

Yes it worked. But it didnt do so well with huge functions and stuff like that.
Jukki
 
Posts: 214
Joined: Wed Apr 07, 2010 4:59 am

Re: arrays in quake?

Postby Spike » Thu Jan 26, 2012 10:15 am

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).
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: arrays in quake?

Postby r00k » Thu Jan 26, 2012 5:07 pm

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;
r00k
 
Posts: 1110
Joined: Sat Nov 13, 2004 10:39 pm

Re: arrays in quake?

Postby Cobalt » Thu Jan 26, 2012 7:05 pm

Good example !

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

guess it wont work for an idea I had.


[quote="r00k"]example
[code]
User avatar
Cobalt
 
Posts: 445
Joined: Wed Jun 10, 2009 2:58 am
Location: New England, USA

Re: arrays in quake?

Postby ceriux » Fri Jan 27, 2012 7:48 am

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?
User avatar
ceriux
 
Posts: 2223
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Re: arrays in quake?

Postby Baker » Fri Jan 27, 2012 8:09 am

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 ..
User avatar
Baker
 
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: arrays in quake?

Postby Arkage » Fri Jan 27, 2012 6:20 pm

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.
User avatar
Arkage
 
Posts: 66
Joined: Thu Nov 19, 2009 4:17 pm

Re: arrays in quake?

Postby ceriux » Fri Jan 27, 2012 7:39 pm

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;
User avatar
ceriux
 
Posts: 2223
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Re: arrays in quake?

Postby Arkage » Fri Jan 27, 2012 8:47 pm

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?
User avatar
Arkage
 
Posts: 66
Joined: Thu Nov 19, 2009 4:17 pm


Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest