Xonotic Forums
For prospective QC coders - Printable Version

+- Xonotic Forums (https://forums.xonotic.org)
+-- Forum: Creating & Contributing (https://forums.xonotic.org/forumdisplay.php?fid=10)
+--- Forum: Xonotic - Development (https://forums.xonotic.org/forumdisplay.php?fid=12)
+--- Thread: For prospective QC coders (/showthread.php?tid=3324)



For prospective QC coders - divVerent - 08-01-2012

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


RE: For prospective QC coders - machine! - 08-01-2012

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*


RE: For prospective QC coders - divVerent - 08-01-2012

None are correct. I will add ONE solution:

TO QUESTION 1:

It outputs:
Code:
This is working.
(with newline at the end)


RE: For prospective QC coders - machine! - 08-01-2012

Lol, I misunderstood that... fuu, amazon have 20 day shipping. Sad


RE: For prospective QC coders - Micha - 08-01-2012

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.


RE: For prospective QC coders - Mr. Bougo - 08-01-2012

(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.


RE: For prospective QC coders - machine! - 08-01-2012

Okey, but I've already order it.

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



RE: For prospective QC coders - Samual - 08-03-2012

OOOOOOOOOOOOH GAMES GAMES GAMES

*grabs popcorn and watches closely*

p.s.: Good job Micha


RE: For prospective QC coders - Micha - 08-03-2012

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

Thanks Smile