Jump to content

A few questions on c++/how JA is coded...


Dusty

Recommended Posts

Well, I said a few, but only one have I that I can think of off the top of my head:

 

I see weird things in the code I don't understand really at all, in simple things like If statements.'

 

A big one is "if ( functionname() ) { body of statements }":

 

How do you say "if" a function call? It doesn't say "if ( functionname() = 1 )", but just that, seeming like an incomplete sentence. What is the if statement testing in this case? Is it just checking if the function returns some kind of value at all or some default value that's implied?

 

Also, I think I just subliminally answered my own question, but what's with if/switch statements that say for their condition things like "activeDefense()" or "!activeDefense?" How can I "activeDefense()" or "not-activeDefense()"? Are they boolean functions or something where you can do that I guess maybe?

 

 

Link to comment

I'm not a coder, but the scripting we use is very similar to C#.

 

In scripting I might do something like that to see if a function and it's subsequent code has been executed since it is crucial to the function I'm threading. If not, return; or run func to grab my information in a global array to pass between several functions that are threaded and waiting for a flag, notify or specific information.

Link to comment

The syntax is

if(booleanExpression) {...}
If the boolean expression evaluates to true, the block is executed. that is, in

if(true) f();
if(false) g();
f gets executed and g does not.

 

Comparisons (==) return a Boolean value - two things are either equal or they're not. But you never need to compare two Booleans - they're already either true or false. Instead of x == true you might as well right x, and instead of x == false you can write !x.

 

Also, C (and by extension C++) loves implicitly converting variables. Like int to bool - 0 is false, everything else is true. So if(42) evaluates to if(true).

 

By the way,

if ( functionname() = 1 )
is illegal. = is for assignment, while == is comparison. So that means "assign 1 to the result of this function call". You can't assign a function return value a different value.

 

Assigning a value to a variable on the other hand is totally valid and returns the assigned valid, so if(a = 1) will set a to 1 and return 1, which evaluates to true. But that's rarely what you want.

 

Also, I think I just subliminally answered my own question, but what's with if/switch statements that say for their condition things like "activeDefense()" or "!activeDefense?" How can I "activeDefense()" or "not-activeDefense()"? Are they boolean functions or something where you can do that I guess maybe?

That's mostly a question of naming your functions and variables in a sensible manner. Which Raven did not do in this case, it's hard to tell when activeDefense returns true. (When you have an active defense?)
Link to comment

Mr Wonko gave a really good answer, but I thought I'd just explain in short:

 

if ( function() )

{//stuff}

 

means function() is called, evaluated and returns a value to the if statement it was called from, if the value is 1 or greater, then it excutes the stuff in {} otherwise the value is false and the stuff in brackets is skipped.

 

The activeDefense() stuff is very similar.  If the condition is !activeDefense() it would require that activeDefense return a value of false in order to execute.  I've always thought ! a function is a bit confusing, and have preferred to say something like if (activeDefense == false).  But that's just me.

JKG Developer

Link to comment

Everything seems answered. But here's some slightly different words for the same answers that might be useful if it's still a bit hard to understand:

 

I think @mrwonko misunderstood you a bit when it came to your not-activeDefence() question, Darth Futuza answered it in his post though.

You're asking how it is possible to do !activeDefence(), as in how do you not a function call?

Remember that if statements are looking for a boolean expression, so you can have a function call that returns a boolean value. Let's say activeDefence() returns true, but i want an if statement to happen if it is false. Then I can do if(!activeDefence()). Because it will evaluate my expression, which calls the activeDefence() function, and that results in the true or false value it returns(in my example, it could return non-boolean values instead). And the ! is therefore being applied to the return value of that function.

mrwonko touched on the topic of proper naming. A functions name should make it clear what the function does, and if it can, what you can expect as the return value. I agree that the activeDefence name isn't really clear enough here. IsDefenceActive would be a good name. Yes or no question.

 

@Darth Futuza you had a ; after the function call in your example. That's not correct.

I don't mean to nitpick, but since it's a topic with a novice asking for help, I think we should try to avoid giving him future bugs in his code =]

 

Now I shall summon... hmmm, let's try.... @Didz to give another version of the explanation. Because we clearly can't have enough of these.

Link to comment

@Darth Futuza you had a ; after the function call in your example. That's not correct.

I don't mean to nitpick, but since it's a topic with a novice asking for help, I think we should try to avoid giving him future bugs in his code =]

But but but 'twas pseudo code!  Aww fine, I suppose I'll edit it. 

 

And I shall summon @@eezstreet!  

 

We should all write a collaborated basic c++ introduction to programming openjk book.  One day we will make lots of money off of poor college students who take game programming courses and have to read our text book. > =]

Asgarath83 and Dusty like this

JKG Developer

Link to comment

Don't worry I was joking.  But it IS about the only way I can think of being able to legally make money off of JKA.

This is drifting into offtopic, but allow me to counter: Creating a great JKA mod can improve your portfolio and give you experience, leading to better jobs that make you more money. I'm pretty sure I primarily have JKA and its community to thank for where I am today, without modding I probably wouldn't have gotten into coding.
eezstreet, Scooper, Stoiss and 1 other like this
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...