Xonotic Forums

Full Version: For prospective QC coders
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here is a code example for prospective QuakeC coders, as well as some "interview questions" for it:

Code:
.string classname;
.string netname;

void func()
{
    entity e;
    e = spawn();
    e.classname = "foo";
    e.netname = "This ";
    e = spawn();
    e.classname = "foo";
    e.netname = "is ";
    e = spawn();
    e.classname = "bar";
    e.netname = "not ";
    e = spawn();
    e.classname = "foo";
    e.netname = "working.\n";

    for(e = world; (e = find(e, classname, "foo")); )
        print(e.netname);
}

And now the questions:

  1. What does func() output when nothing else has been run yet?
  2. Write the for loop as an equivalent while loop.
  3. What, do you think, may the three arguments to find() mean? What does find() return?
  4. What happens when calling func() a second time?
    1. Why does it do that?
    2. Doesn't this indicate some sort of memory leak?
    3. How do you suggest fixing it?

If you can figure this out without knowing QuakeC, you may be a good match for our team! Smile
Okey, I'm probably completly wrong, I got only a very tiny experience with coding. and that was with Lua. But I'll try! Smile

1. Nothing.
2.
Code:
while (e = world; (e = find(e, classname, "foo")); )
     print(e.netname);
Oh, that's almost the same thing, I guess...
3. I think find does find what each variable is equal to or something, I don't know what it return though.

this was harder than i though -.-

4. no idea...

*ordering The C Programming Language (second edition) from amazon*
None are correct. I will add ONE solution:

TO QUESTION 1:

It outputs:
Code:
This is working.
(with newline at the end)
Lol, I misunderstood that... fuu, amazon have 20 day shipping. Sad
I only know normal C. I guess spawn() works like the C++ new() and puts a data object on the heap, and "entity e;" only puts a pointer on the stack. Looks like each heap element has a reference to the previously created element. So:

1) "This is working."
2)
Code:
e = world;
while(e = find(e, classname, "foo"))
        print(e.netname);
3) arg1=list start element; arg2=class member to compare with arg3. find() returns a reference to the next element in the list that matches the criteria given by 2+3.
4) "This is working."
"This is working."
4.1) Because the list start element is world, and the first call already spawned 4 entities without removing them.
4.2) Not necessarily. Since the spawned elements can always be retrieved by a find with "world" as starting point, they might be used by other functions. If it is not intended, further calls to func() will finally deplete the heap and further spawn() will fail.
4.3) If the entities are not used by other functions, func() should remove() them at the end.
(08-01-2012, 07:49 AM)machine! Wrote: [ -> ]Lol, I misunderstood that... fuu, amazon have 20 day shipping. Sad

QuakeC and C share a similar syntax, but you won't learn QC from a C book. In fact, you don't need to know any C to learn QC.
Okey, but I've already order it.

EDIT: 4. I guess it outputs:
Code:
This is not working
OOOOOOOOOOOOH GAMES GAMES GAMES

*grabs popcorn and watches closely*

p.s.: Good job Micha
(08-03-2012, 11:46 AM)Samual Wrote: [ -> ]p.s.: Good job Micha

Thanks Smile