![]() |
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:
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 |
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. |
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 ! |
In your case, you should probably use TStaticVars; like this:
PHP Code:
PHP Code:
|
Perhaps my Active Record system would be of help.
In your case you would create a DB-NPC called: Item PHP Code:
PHP Code:
PHP Code:
PHP Code:
|
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. |
If you're familiar with C, joining classes in GS2 is basically #include
|
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 :) |
Quote:
Quote:
|
| 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.