Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 08-06-2011, 02:53 PM
Adddeeee Adddeeee is offline
Registered User
Join Date: Aug 2011
Posts: 30
Adddeeee is on a distinguished road
Instance of a class?

Hi!

I'm new to GS2 and have a few (dumb) questions.

When it comes to classes and objects; is there any way to make an instance of a class? Like in Java you write something like:

Java:
PHP Code:
MyClass myObject = new MyClass(); 
Or should I look at classes in GS2 like a way to gather general functions?

I am a little bit confused on how the code structure should look like to be efficient.

Help is very much appreciated! Sorry for this n00b question though.

// Adddeeee
Reply With Quote
  #2  
Old 08-06-2011, 03:04 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Hey, and welcome to GS2!

I'm not very knowledgeable with Java, so I'm not entirely sure what your example is supposed to do, but in GS2 classes are just a way to gather functions in that can be joined to other NPCs.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #3  
Old 08-06-2011, 03:12 PM
Adddeeee Adddeeee is offline
Registered User
Join Date: Aug 2011
Posts: 30
Adddeeee is on a distinguished road
Ahaa, I see.

Well, me and my friend are working on a Shop system. We felt like doing something a little more difficult.

Although, it feels like the structure of our code isn't that good.

In java we would probably create a class named Item which would consist of several variables describing the specific item. (name price etc.).

I would then create several different instances of that class, since there would be severel different items.

But it seems that's not how you would do it in GS2?

Sorry for all the confusion !
Reply With Quote
  #4  
Old 08-06-2011, 03:21 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
In your case, you should probably use TStaticVars; like this:

PHP Code:
this.var = new TStaticVar("MyVar");
this.var.var1 1;
this.var.var2 2;
this.var.join("my_class"); 
You can then use the following code (even in other NPCs) to create a new "instance" of it:
PHP Code:
temp.var = new MyVar() 
Or, at least I think that's how those work. Didn't use them for quite a while, and only ever tried them on the clientside. var1 and var2 are just examples, and the last line is supposed to show you that you can also join classes to TStaticVars (which I've actually done in the past - works well!).
Reply With Quote
  #5  
Old 08-06-2011, 07:39 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Perhaps my Active Record system would be of help.

In your case you would create a DB-NPC called: Item

PHP Code:
function onCreated() { 

  
// DB Migrations 
  
this.migrations = { 
    {
"new""itemname:string:unique""description:string""price:float"}, // Creates Table 
    
{"add_index""itemname"},  // Adds Index to Table 
    
{"echo""Migrations Completed!"// Optional Message 
  
}; 
   
  
// Configuration 
   
  // Model Object's Specific Class 
  
this.model_class "activerecord_object_" this.name.lower(); 
   
  
// Model Functionality 
  
this.join("activerecord_model"); 
}

public function 
validations(obj) { 
  
// Model specific validations 
  
return true

class: activerecord_object_item

PHP Code:
public function setItemName(new_name) {
  
this.itemname new_name;
}

public function 
setItemDescription(new_desc) {
  
this.description new_desc;
}

public function 
setItemPrice(new_price) {
  
this.price new_price;
}

public function 
getItemName() {
  return 
this.itemname;
}

public function 
getItemDescription() {
  return 
this.description;
}

public function 
getItemPrice() {
  return 
this.price;
}

public function 
pretty() {
  return 
format("Name: %s Description: %s Price: %s"this.itemnamethis.descriptionthis.price); 

You can then create Item objects like this:

PHP Code:
function onCreated() {
  
// Creates Item Object
  
temp.item Item.init();

  
// Sets Item Variables
  
temp.item.setItemName("Sword");
  
temp.item.setItemDescription("It's sharp and will poke your eye out!");
  
temp.item.setItemPrice(100);

  
// Saves Item to DB
  
if (temp.item.save()) {
    echo(
"Item saved with ID " temp.item.id);
  }

and look them up and modify them like this:

PHP Code:
function onCreated() {
  
// Lookup all items
  
temp.items Item.all();
  for (
temp.itemtemp.items) {
    echo(
temp.item.pretty());
  }
  
// Lookup specific item
  
temp.sword Item.where("itemname = 'Sword'").get();
  if (
temp.sword != NULL) {
    echo(
temp.sword.pretty());
  }
  
// If you know the ID
  
temp.sword Item.find(1);
  if (
temp.sword != NULL) {
    
// Increase the price for good measure... (example)
    
temp.sword.setItemPrice(temp.sword.getItemPrice()+100);
    if (
temp.sword.save()) {
      echo(
temp.sword.pretty());
    }
  }

__________________
Quote:
Reply With Quote
  #6  
Old 08-07-2011, 01:32 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
If you're new to GScript I would strongly recommend that you ignore Crow's post until you're more experienced. In GScript, a very small percentage of your code actually involves creating objects like that (I've only done it a few times in 5+ years).

Be careful not to confuse Graal 'classes' with Java's. Classes in Graal are kind of unique—you can 'join' a class to an object, such as a player or NPC, and the code of that class is essentially copied and pasted into that object's script. You can join as many classes as you want to an object.

Unlike Java, Graal has no way to create your own object types except through hackish methods like what Crow described. Most of your code will go in weapon scripts and classes.

This post (ignore my old, terrible styling) may help a bit in understanding classes in GScript.

If you know JavaScript, I would try comparing Gscript to that rather than Java.
__________________
Reply With Quote
  #7  
Old 08-07-2011, 02:15 AM
salesman salesman is offline
Finger lickin' good.
salesman's Avatar
Join Date: Nov 2008
Location: Colorado
Posts: 1,865
salesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud of
If you're familiar with C, joining classes in GS2 is basically #include
__________________
Reply With Quote
  #8  
Old 08-07-2011, 10:46 AM
Adddeeee Adddeeee is offline
Registered User
Join Date: Aug 2011
Posts: 30
Adddeeee is on a distinguished road
Thank you all for your replies

Yepp, I figuered it wasn't "meant" to be like in Java. Yes it's very similar to C's #include, from now on I'll just think of it like that!

I'm a little confused regarding server- and clientside though. If I create a variable, let's call it his.test, on the serverside of an NPC; will that variable "exist" on the clientside aswell? Or should I look at serverside and clientside as to different "machines" so to say?

It feels like it's coming to me know! Although, I have a lot more to learn
Reply With Quote
  #9  
Old 08-07-2011, 10:52 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Adddeeee View Post
I'm a little confused regarding server- and clientside though. If I create a variable, let's call it his.test, on the serverside of an NPC; will that variable "exist" on the clientside aswell? Or should I look at serverside and clientside as to different "machines" so to say?
No, it won't exist on clientside if you create it serverside (there are some exceptions of synced variables, though). You absolutely should look at them as two machines, because they are. Serverside is handled by the NPC-server, clientside is handled by the player's computer.

Quote:
Originally Posted by cbk1994 View Post
There are essentially two different ways you script: on serverside, or on clientside. If a script is serverside, it means that the NPC-server interprets and performs the code. The advantage to this is that you know the code can't be tamperered with. If the code is clientside, it means that it is ran on the computer of the player. This has the advantage of being faster and not requiring communication with the server, but has the disadvantage of being less secure.

Since serverside is not performed for a specific player it cannot display interface graphics (HUDs, etc). It also can't handle player chat events, among others.

To communicate between the server and the client you use something called a trigger.
There's some more information here and here.
__________________
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 12:27 AM.


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