Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Instance of a class? (https://forums.graalonline.com/forums/showthread.php?t=134264123)

Adddeeee 08-06-2011 02:53 PM

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

xXziroXx 08-06-2011 03:04 PM

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.

Adddeeee 08-06-2011 03:12 PM

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 !

Crow 08-06-2011 03:21 PM

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!).

fowlplay4 08-06-2011 07:39 PM

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());
    }
  }



cbk1994 08-07-2011 01:32 AM

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.

salesman 08-07-2011 02:15 AM

If you're familiar with C, joining classes in GS2 is basically #include

Adddeeee 08-07-2011 10:46 AM

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 :)

cbk1994 08-07-2011 10:52 AM

Quote:

Originally Posted by Adddeeee (Post 1662353)
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 (Post 1579584)
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.


All times are GMT +2. The time now is 02:56 PM.

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