Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   New Scripting Engine (GS2) (https://forums.graalonline.com/forums/forumdisplay.php?f=153)
-   -   Learning GS2 :D (https://forums.graalonline.com/forums/showthread.php?t=68473)

killerogue 08-31-2006 04:41 PM

Learning GS2 :D
 
I was problem solving with Chicken yesterday and he was teaching me about each aspect of GS2. So I learned alot really. Here's a couple things I put together with Chicken_l33t.

Simple Light Switch

PHP Code:

function onCreated()
{
this.Number_1 0;
this.Number_2 0;
if (
this.Number_1 this.Number_2 0);
this.value 0;
}
function 
onPlayertouchesme()
{
if (
this.value 0);
this.value 1;
player.chat "On!";
}
else 
{
this.value 0;
player.chat "Off!";


Chat Text Changer

PHP Code:

function onCreated()
{
this.StringArray "GS1 sux.","GS2 is awesome.","blah";
}
function 
onPlayerchats()
{
this.value int(random(0,2));
player.chat this.StringArray[this.value];


:D How'd I do for a beginner?

E: Eew wasn't in PHP. My bad. :]

KuJi 08-31-2006 04:57 PM

One of my irl friends wanted to learn @ 3 AM yesterday (around the time you imed me :o).

He made a staff npc or so with a buncha commands in like 20-30 minutes. :P

Skyld 09-01-2006 12:05 AM

PHP Code:

this.test "foo","bar","baz"

... is incorrect. You should use this for arrays:
PHP Code:

this.test = {"foo""bar""baz"}; 

Also, you need to put if checks as a block. This is incorrect:
PHP Code:

if (this.foo);
bar(); 

This is correct:
PHP Code:

if (this.foo)
{
  
bar();


Also, you forgot to close your brackets on a function.
PHP Code:

function onFoo()
{
  
//


Also, you can just do this.value = !this.value; to make a toggle for this.value being true or false.
PHP Code:

function onWeaponFired()
{
  
this.enabled = !this.enabled;

  if (
this.enabled)
  {
    
//
  
}
    else
  {
    
//
  
}


Also, don't use = inside an if statement, since = is assignment. You are probably overwriting the value by using = inside an if. Use == instead since it doesn't do anything but compare:
PHP Code:

this.value 3;

if (
this.value == 3)
{
  
//


Also, it's onPlayerTouchsMe(), not onPlayerTouchesMe().

Sorry. ^^

zephirot 09-01-2006 12:55 AM

Skyld, teach me how to script :(.

killerogue 09-01-2006 01:36 AM

Quote:

Originally Posted by Skyld
PHP Code:

this.test "foo","bar","baz"

... is incorrect. You should use this for arrays:
PHP Code:

this.test = {"foo""bar""baz"}; 


What's wrong with me using
PHP Code:

this.StringArray = {"foo","bar","baz"}; 

Quote:

Also, you need to put if checks as a block. This is incorrect:
PHP Code:

if (this.foo);
bar(); 


Don't quite understand why this is necessary? I'm not trying to make an event or call a function or whatever it is. I simply wanted to change the players chat. These were two different scripts. :[

Quote:

This is correct:
PHP Code:

if (this.foo)
{
  
bar();



How does is this relevant to what I was trying to do? You'd have to show me Skyld as from reading it it doesn't seem it applies to anything I was aimnig for. :(

Quote:

Also, you forgot to close your brackets on a function.
PHP Code:

function onFoo()
{
  
//



Err, I thought I did. That's my mistake then. Very careless of me I agree with you.

Quote:

Also, you can just do this.value = !this.value; to make a toggle for this.value being true or false.
PHP Code:

function onWeaponFired()
{
  
this.enabled = !this.enabled;

  if (
this.enabled)
  {
    
//
  
}
    else
  {
    
//
  
}



I haven't quite gotten to learning these more advanced ways of putting things. Hence me putting "Learning GS2". :(

Quote:

Also, don't use = inside an if statement, since = is assignment. You are probably overwriting the value by using = inside an if. Use == instead since it doesn't do anything but compare:
PHP Code:

this.value 3;

if (
this.value == 3)
{
  
//


Also, it's onPlayerTouchsMe(), not onPlayerTouchesMe().

Sorry. ^^
Alas another careless mistake on my part. But I'm learning new things and basically using caveman style GS2. The basic of basic commands here. :/

killerogue 09-01-2006 01:41 AM

Here I rewrote it zeroing in on all problems. Be happy Skyld and leave me and my caveman scriptign alone. >:[

PHP Code:

function onCreated()
{
this.value 0;
}
function 
onPlayerTouchsMe()
{
if (
this.value 0);
this.value 1;
player.chat "On!";
}
else
{
this.value 0;
player.chat "Off!";



Moondeath_2 09-01-2006 05:10 AM

Meow. I feel like teaching and stuff, if anyone is paying attention to this forum and wishes to aim me for some scripting help. I'll try to be as of best help as possible even considering it's late, and i'm sick.


Edit: Come onnnnnn i'm bored >_>

zephirot 09-01-2006 03:26 PM

Well, I added you :p, as soon as you lgo on AIM, I will bug you.

_Z3phyr_ 09-01-2006 05:02 PM

Quote:

this.enabled = !this.enabled;
I like that as a switch.

What I previously used was
PHP Code:

this.enabled this.enabled

but that's more simple.

Draenin 09-01-2006 05:45 PM

Quote:

Originally Posted by killerogue
What's wrong with me using
PHP Code:

this.StringArray = {"foo","bar","baz"}; 


Because that format is mainly used for arrays, which are a little different than a plain variable.

This is a lot more efficient, though: (Not to mention, it should work.)
PHP Code:

function onCreated()
{
this.value 0;
}
function 
onPlayerTouchsMe()
{
if (
this.value 0);
this.value 1;
player.chat "On!";
}
else
{
this.value 0;
player.chat "Off!";


The else case is a bit unnecessary, though, as the variable this.value will stay at 0 unless you change it, which you have done in your if case.

JkWhoSaysNi 09-01-2006 08:18 PM

there are a couple of minor problems with your code there, problems that will stop it executing the way you expect it to.

PHP Code:

...
if (
this.value 0); //here
this.value 1;
player.chat "On!";
}
else {
... 

Firstly, after an if should be a { not a ;.
Secondly, comparision is done with == not =.

= is used to set values and can be read as "becomes". == can be read as "is equal to", so that line of code should be this:
PHP Code:

if (this.value == 0) { 


jami_jamie 09-11-2006 06:58 PM

Quote:

Originally Posted by JkWhoSaysNi
Secondly, comparision is done with == not =.

= is used to set values and can be read as "becomes". == can be read as "is equal to", so that line of code should be this:
PHP Code:

if (this.value == 0) { 


We already picked up on that during this thread. Dont repeat it. xD

Chicken_l33t 09-12-2006 03:32 AM

Now I look like a noob, lol. Anyway good work with the fixing of it killer.

ApothiX 09-12-2006 02:42 PM

Quote:

Originally Posted by Draenin
Because that format is mainly used for arrays, which are a little different than a plain variable.

This is a lot more efficient, though: (Not to mention, it should work.)
PHP Code:

function onCreated()
{
this.value 0;
}
function 
onPlayerTouchsMe()
{
if (
this.value 0);
this.value 1;
player.chat "On!";
}
else
{
this.value 0;
player.chat "Off!";


The else case is a bit unnecessary, though, as the variable this.value will stay at 0 unless you change it, which you have done in your if case.

If you're going to use PHP tags, please format your code so it's readable. x_x

Also, there are a few errors, such as the semi-colon after the if statement, and the fact that you are missing a curly-brace at the end (which would have been easily distinguishable if you had formatted your code ;))

Fixed (Differentish) version:

PHP Code:

function onCreated() {
  
this.value false;  // Kind of redundant, values get reset when the NPC is created serverside anyway.
}

function 
onPlayerTouchsMe() {
  
this.value = !this.value;

  if(
this.value) {
    
player.chat "On";
  } else {
    
player.chat "Off";
  }



ApothiX 09-12-2006 02:43 PM

Quote:

Originally Posted by jami_jamie
We already picked up on that during this thread. Dont repeat it. xD

It wasn't fixed in Draenin's example. Don't reply to the thread if you don't read it ;)


All times are GMT +2. The time now is 09:17 AM.

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