Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Difference Java/C++ <-> GS2 (https://forums.graalonline.com/forums/showthread.php?t=78421)

Darklux 01-21-2008 02:26 PM

Difference Java/C++ <-> GS2
 
Hey,

Iam thinking of learning GS2.

I had Java and C++ in School and already some in University, so I just wanted to ask where the main differences lie.

- Lux

Skyld 01-21-2008 03:02 PM

My C/C++ and Java are both quite scratchy, so forgive me if this is not totally accurate, but from what I remember:

C/C++ is statically typed (in terms of variable data types), GScript uses variant types which adapt to the type of data automatically.

Classes are very different in GScript to Java. Obviously in C classes do not exist, and I cannot say about C++ since I don't know enough about it. In GScript typically you implement class-like functionality using object joins, which are dynamic. You can "invent" class-like functionality in GScript by using these joinable class scripts, and also by using function prototyping and such.

You don't think about allocation in GScript, since that is all done by the engine, whereas C/C++ (and maybe Java to an extent), you must set aside space to work with before you work with it.

GScript uses built-in event-handling functions quite heavily to handle user/player interaction, whereas this functionality would not usually be implemented by default in C/C++.

Scripts in GScript work quite heavily on an instance basis, i.e. an NPC, weapon script, GANI script, etc, which means that extra functionality like trigger and such are added to allow communication between these instances. Similar to Java's object instances, I guess.

Flow control is pretty much identical (break, continue, return), as are many of the syntax structures (for, while, do, if), functions are defined in much the same way (with the function keyword instead of the expected datatype), and functions are called in the same way. Variable scoping isn't really vastly different to Java, not sure about C/C++ though without checking.

GScript is quite a lot more like ECMAScript/JavaScript in terms of structure though.

cbk1994 01-21-2008 03:13 PM

One thing I like about GS2 that Java doesn't have (but I would not want Java to have; would makes things incredibly complicated.

You can do something like this in GS2:
PHP Code:

function onFoo()
{
  if ( 
isFooing )
  {
    return;
  }
  echo( 
"No, you are not fooing ..." );


Whereas in Java you would need to do something like this to get out of a function easily:

PHP Code:

public boolean onFoo()
{
  if ( 
isFooing )
  {
    return 
false;
  }
  
System.err.println"You are not fooing ..." );
  return 
false;


Makes it much easier to leave functions when you don't need to specify everything.

Also, when comparing strings you CAN use ==.

PHP Code:

if ( foo == no )
{
  echo( 
"not foo" );


vs.

PHP Code:

if ( foo.equalsno ) )
{
  
System.err.println"not foo" );


Also, I find it nice that you can handle conversions much easier- the system does it for you! What I mean is that an int is a double is a float, etc, where in Java you need to do conversions int.readDouble() ...

Just a few things that are different in Java vs GS2

Tolnaftate2004 01-21-2008 09:55 PM

C/C++ struct functionality can be replicated.
PHP Code:

function somestruct() {
  
temp.var = new TStaticVar();
  
with (temp.var) {
    
// definitions
  
}
  return 
temp.var;
}
somestruct(); 

vs.
PHP Code:

typedef struct {
  
// definitions
somestruct;
somestruct A

C++ classes, however, cannot be fully replicated (though I intended to request something that would... And then my computer died).

Classes in GS2 can be thought of as a mixture of #include and a class declaration. Joining a class gives access to its code and in ambiguous situations, should be called like class::func();.

I know jack about java, though. Sorry if this isn't a good explanation, it's very hard to type on an iPod.

cbk1994 01-21-2008 10:33 PM

Quote:

Originally Posted by Tolnaftate2004 (Post 1370997)
I know jack about java, though. Sorry if this isn't a good explanation, it's very hard to type on an iPod.

You wrote that whole post on an iPod ... ?

I couldn't type that much in a day on my iPhone >_<

Inverness 01-21-2008 11:10 PM

Why do we need C++ classes again?

Tolnaftate2004 01-22-2008 07:04 AM

Quote:

Originally Posted by Inverness (Post 1371029)
Why do we need C++ classes again?

Data hiding, encapsulation.

@ cbkbud... My plane home was delayed; I had nothing better to do. :p

Novo 01-22-2008 11:20 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1371154)
Data hiding, encapsulation.

I did replicate Java's class system... Although data-hiding isn't really possible ( all variables are public by restriction ). None-the-less, I pretend that it isn't public.

I posted it on the forums some time ago:
http://forums.graalonline.com/forums...ad.php?t=77835

( Java class system doesn't include multiple inheritance, although that is little unimportant with the advent of join's. )

Additionally, GS2 isn't type-casted in the traditional sense... I find that non-typed languages are harder to work with: They force us to follow good coding practices.

Tolnaftate2004 01-22-2008 07:31 PM

Quote:

Originally Posted by Novo (Post 1371169)
I did replicate Java's class system... Although data-hiding isn't really possible ( all variables are public by restriction ). None-the-less, I pretend that it isn't public.

Right now, one could make member functions "protected/private" with some clever thinking and the right code. Variables are a different case.

Inverness 01-22-2008 11:33 PM

Maybe private variables in Graal should be done like in Python where if the variable name starts with _ then its private.

this._varname == private
this.varname == public

That seems like the most painless way to me.

cbk1994 01-23-2008 01:07 AM

Quote:

Originally Posted by Inverness (Post 1371274)
Maybe private variables in Graal should be done like in Python where if the variable name starts with _ then its private.

this._varname == private
this.varname == public

That seems like the most painless way to me.

I would rather see thisr. or even this.protected.

Inverness 01-23-2008 01:42 AM

thisr. will not happen considering you would need thisor. and stuff like that and it would just become a giant pain. Something like this.private. might be better however having subvariables for variables that aren't objects is kinda weird and I don't think it would be good to go into that.

I still think my suggestion is best.

cbk1994 01-23-2008 03:03 AM

Quote:

Originally Posted by Inverness (Post 1371325)
thisr. will not happen considering you would need thisor. and stuff like that and it would just become a giant pain. Something like this.private. might be better however having subvariables for variables that aren't objects is kinda weird and I don't think it would be good to go into that.

I still think my suggestion is best.

What is thisor exactly?

Novo 01-23-2008 03:15 AM

I think the best solution for private variables is just treating them as private. The convention you use for denoting a private variable could help you.

I basically treat all variables as private / local, and use clearly-defined functions to modify them.

Tolnaftate2004 01-23-2008 03:36 AM

My solution was similar to some method in actionscript (I'd guess addmember). You provide the variable's name and the names of the get/set functions.

PHP Code:

function someclass() {
  
temp.var = TStaticVar();
  
with (temp.var) {
    
this.rvalue = function (obj) {
      return 
this.(@ obj);
    };
    
this.lvalue = function (obj) {
      return 
this.(@ obj).link();
    };
    
this.addmember("counter","rvalue","lvalue");
    
this.counter = -1;
  }


So depending on whether the member is the r- or lvalue, either function is called. It can be easily modified to allow for specific data types, variable protection, etc., and doesn't break backwards compatibility. The dummy/standard functions are given here.

Anyway, that's enough for derailing this thread, methinks.

Inverness 01-23-2008 05:58 AM

Quote:

Originally Posted by Novo (Post 1371356)
I think the best solution for private variables is just treating them as private. The convention you use for denoting a private variable could help you.

I basically treat all variables as private / local, and use clearly-defined functions to modify them.

Yea I do it about the same way. I see private variables as more of a way of making the script idiot-proof so people other than yourself don't mess it up.
Quote:

Originally Posted by cbkbud (Post 1371351)
What is thisor exactly?

Well if you used thisr. to reference private variables in the object, then what about when you're in a with (object) {} block and want to get a private variable from the calling object? Normal variables would be accessed by thiso. but you would need something thisor. for the private variables of the parent object and that is just unnecessary.

Tolnaftate2004 01-24-2008 09:28 PM

Quote:

Originally Posted by Inverness (Post 1371418)
Normal variables would be accessed by thiso. but you would need something thisor. for the private variables of the parent object and that is just unnecessary.

This circumvents the privacy.

im in ur scope accezzin ur private variablez.

Inverness 01-25-2008 12:46 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1371722)
This circumvents the privacy.

im in ur scope accezzin ur private variablez.

The original object is still executing the code, not the object in the with () block, so its not circumventing it.

I remember Stefan saying quite along time ago that something like thisr. would not be such a good idea so any argument is pointless.

Tolnaftate2004 01-25-2008 03:47 AM

Quote:

Originally Posted by Inverness (Post 1371753)
The original object is still executing the code, not the object in the with () block, so its not circumventing it.

thisor.var = thisr.var; or thisr.var = thisor.var; in a with block would be circumventing it.

e: I think you may have misunderstood Stefan, too.
The last post wasn't meant to single you out, but after I posted, I realized how much of a pain it would have been to get my point understood.

Inverness 01-25-2008 04:16 AM

I never said anything about using thisr. in the with block, which obviously wouldn't be allowed. I think thats bad form anyhow. It would be better to use Twinny's suggestion and have the this.private. section of the object for private variables.

Codein 01-28-2008 11:50 AM

Quote:

Originally Posted by Inverness (Post 1371799)
I never said anything about using thisr. in the with block, which obviously wouldn't be allowed. I think thats bad form anyhow. It would be better to use Twinny's suggestion and have the this.private. section of the object for private variables.

Or, like in C++ Classes, being able to define the private and public variables like so:

PHP Code:

public:
char ani;
char name[20];

private:
int health;
float xy


Twinny 01-28-2008 01:16 PM

And with that, you have proven that you don't keep up with the future improvement subforum. *slaps*

cbk1994 01-29-2008 12:24 AM

Quote:

Originally Posted by Codein (Post 1372396)
Or, like in C++ Classes, being able to define the private and public variables like so:

PHP Code:

public:
char ani;
char name[20];

private:
int health;
float xy


Seems like that would kinda suck if we had to start doing int, etc.


Just like at the beginning something like

PHP Code:

private gansta;
private 
gP gangsta.newParadise();

// in another block of code

with theThing )
{
  
gP.addGangsta"Snappy Bro" );
}

// would not work 


Twinny 01-29-2008 03:48 AM

Quote:

Originally Posted by cbkbud (Post 1372459)
blah

You missed the point too! >_<

http://forums.graalonline.com/forums...ad.php?t=71199

Tolnaftate2004 01-29-2008 04:37 AM

Quote:

Originally Posted by Twinny (Post 1372495)

It seems many of us disagree with that idea.

cbk1994 01-29-2008 05:43 AM

Quote:

Originally Posted by Twinny (Post 1372495)

Can I still do something like

PHP Code:

= new Gangsta();
gP g.newGangstasParadise(); 

... please?

EDIT:

And I really would think something like this would work
PHP Code:

private tVar n// noone can see
protected tVar n// read only
public tVar n// All rights; is this by default if nothing is said.

// even could have
static tVar n// Cannot be changed -- I thought there was a way to do this, but I can't remember it. 


napo_p2p 01-29-2008 08:07 AM

Quote:

Originally Posted by cbkbud (Post 1372508)
And I really would think something like this would work
PHP Code:

private tVar n// noone can see
protected tVar n// read only
public tVar n// All rights; is this by default if nothing is said. 


Me too :). Or something similar.

Quote:

Originally Posted by cbkbud (Post 1372508)
PHP Code:

// even could have
static tVar n// Cannot be changed -- I thought there was a way to do this, but I can't remember it. 


I think you can do:
PHP Code:

const tVar n


cbk1994 01-30-2008 01:39 AM

Quote:

Originally Posted by napo_p2p (Post 1372511)
Me too :). Or something similar.


I think you can do:
PHP Code:

const tVar n


Yes, something like

PHP Code:

const tVar n

sounds like what I heard. Personally I've never used it though.

Codein 02-02-2008 01:26 PM

Quote:

Originally Posted by cbkbud (Post 1372459)
Seems like that would kinda suck if we had to start doing int, etc.

Wasn't really what I was getting at. That example I gave was C++. Using protected:, public: and private: was my idea.

Inverness 02-02-2008 02:20 PM

I don't believe doing things like declaring variables private/public/protected with keywords will work in GS2.

This is because GS2 has no support for such things. const is not real value type because the compiler is merely reading it and replacing instances of it in the script with its value on compile time. After the script is compiled it no longer exists. Its about the same thing as C++'s #define.

As I said, GS2 has no support for such things for it would not be possible to do it without lots of changes that are unnecessary. That is why I suggested the Python method where variables (or functions) that have a name starting with _ are private:
PHP Code:

function onCreated() {
  
this.publicvar 5;
  
this._privatevar 7;
}
function 
_privatefunc() {
}
function 
notreallyprivatefunc() {
}
public function 
publicfunc() {


I believe thats a better option than the this.private. prefix.

cbk1994 02-02-2008 03:35 PM

I like this.private.foo, but maybe it could be accessed within the script with just this.foo?

The idea of _var and _func sounds really bad to me, would make code look messy in my opinion.

Codein 02-02-2008 07:13 PM

Quote:

Originally Posted by cbkbud (Post 1373277)
I like this.private.foo, but maybe it could be accessed within the script with just this.foo?

The idea of _var and _func sounds really bad to me, would make code look messy in my opinion.

this.private seems a bit long winded. If anything, an underscore would be best. If we're talking in terms of "messy" coding as in unreadable, I'm sure a protected variable using underscores would be more noticeable than a this.protected prefix.

Chompy 02-02-2008 07:27 PM

Quote:

Originally Posted by Codein (Post 1373308)
this.private seems a bit long winded. If anything, an underscore would be best. If we're talking in terms of "messy" coding as in unreadable, I'm sure a protected variable using underscores would be more noticeable than a this.protected prefix.

but, underscores also mean translation if I recall ;o
PHP Code:

if (_(this.var)) 



All times are GMT +2. The time now is 07:58 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.