Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Chat customizer! (https://forums.graalonline.com/forums/showthread.php?t=134263092)

Emera 05-03-2011 06:39 PM

Chat customizer!
 
So i finally figure how to trigger serverside! Its opened a lot more options to me with GS2. Anyway, heres what i came up with. Hope you like it. Its only very simple but to me, its a huge step!

PHP Code:

//Scripted by Emera. Chat Customizer!
function ActionServerSide() {
  if ( 
params[0] == "chat" ) {
    
player.chat(params[1]);
  }
}
//#CLIENTSIDE
function onPlayerChats() {
if (
player.chat == player.chat) {
player.chat "Emera: "@player.chat;
}
}

for ( 
temp.allplayers ) {
  
with (temp.p) {
    
triggerServer"gui"this.name"chat"message );
  }


It starts of the players chat with the message in the script. You can change it from "Emera: " by typing other messages in. There is no on / off yet because i haven't got to that stuff yet. Thanks guys.

fowlplay4 05-03-2011 06:54 PM

Issues:

1. You shouldn't have code outside of functions, code placed like that is executed every time an event occurs.
2. You're comparing player.chat to itself, which is effectively the same as doing if (true) which is pointless.
3. player.chat is a variable not a function.
4. The way your loop is setup on the client-side you're sending (playercount+ircbots) triggers to the server which is really bad.
5. Code isn't indented properly.

PHP Code:

//Scripted by Emera. Chat Customizer!
function ActionServerSide() {
  if ( 
params[0] == "chat" ) {
    
player.chat(params[1]); // Issue 3: player.chat is a variable not a function.
  
}
}
//#CLIENTSIDE


function onPlayerChats() {
// Issue 5: Code isn't indented properly.
if (player.chat == player.chat) { // Issue 2: Comparing player.chat to itself, will always evaluate be true.
player.chat "Emera: "@player.chat;
}
}

// Issue 1: Code outside of a function.
for ( temp.allplayers ) { // Issue 4: Will send (playercount + ircbots) amount of triggerservers. You should only need to send one.
  
with (temp.p) {
    
triggerServer"gui"this.name"chat"message );
  }


Re-arranged and fixed issues:

PHP Code:

function ActionServerSide() { 
  if ( 
params[0] == "chat" ) { 
    for (
temp.pallplayers) {
      
with (temp.p) {
        
player.chat params[1];
      }
    }
  } 
}
 
//#CLIENTSIDE

function onPlayerChats() { 
  
triggerserver("gui"this.name"chat"player.chat);


I believe the above is what you were aiming for.

Emera 05-03-2011 06:57 PM

Oo Thanks for your help but thats way over my head.

fowlplay4 05-03-2011 07:08 PM

Quote:

Originally Posted by Emera (Post 1647358)
Oo Thanks for your help but thats way over my head.

Updated and labeled the problem areas.

Emera 05-03-2011 07:14 PM

Thanks i see know!
Its all about simplicity.

Astram 05-03-2011 10:01 PM

Dude you can just do.
PHP Code:

//#CLIENTSIDE
function onPlayerChats()
  {
  
temp.chat player.chat;
  
player.chat player.nick@": "@temp.chat;
  } 

Yes, its that simple

cbk1994 05-03-2011 10:16 PM

Quote:

Originally Posted by Astram (Post 1647384)
Dude you can just do.
PHP Code:

//#CLIENTSIDE
function onPlayerChats()
  {
  
temp.chat player.chat;
  
player.chat player.nick@": "@temp.chat;
  } 

Yes, its that simple

PHP Code:

//#CLIENTSIDE
function onPlayerChats() {
  
player.chat player.nick ": " player.chat;



fowlplay4 05-03-2011 10:26 PM

Quote:

Originally Posted by cbk1994 (Post 1647387)
PHP Code:

//#CLIENTSIDE
function onPlayerChats() {
  
player.chat player.nick ": " player.chat;



If this was all the original script was supposed to do, Wow.

DustyPorViva 05-03-2011 10:31 PM

I think changing the player's chat calls onPlayerchats() so you might end up with:
"Emera: Emera: Emera: Emera: Emera: Emera: Emera: test"

I may be mistaken though.

Crow 05-03-2011 10:52 PM

Quote:

Originally Posted by DustyPorViva (Post 1647389)
I think changing the player's chat calls onPlayerchats() so you might end up with:
"Emera: Emera: Emera: Emera: Emera: Emera: Emera: test"

I may be mistaken though.

Changing the player's chat will not invoke the event again.

Emera 05-05-2011 07:30 PM

Oh it really that simple?

Emera 05-05-2011 07:33 PM

PHP Code:

 //#CLIENTSIDE 
function onPlayerChats() { 
  
player.chat player.nick ": " player.chat


This doesn't work. I tried it out in a separate script to test it out and i can't see my nickname at the start of the chat. Also, how can i turn my original script on / off using onWeaponFired() ?

Tricxta 05-05-2011 11:01 PM

Have a variable in it:
PHP Code:

function onWeaponFired()
{
  
this.on=1-this.on;
  if (
this.on==1)
  {
    
//weapon is now on
  
}
  else 
  {
    
//weapon is now off
  
}



xAndrewx 05-05-2011 11:04 PM

Quote:

Originally Posted by Emera (Post 1647769)
PHP Code:

 //#CLIENTSIDE 
function onPlayerChats() { 
  
player.chat player.nick ": " player.chat


This doesn't work. I tried it out in a separate script to test it out and i can't see my nickname at the start of the chat. Also, how can i turn my original script on / off using onWeaponFired() ?

HTML Code:

//#CLIENTSIDE
function onWeaponFired() {
  this.on = !this.on;
  player.chat = "Nicknames" SPC (this.on? "on":"off");
}

function onPlayerChats() {
  if (this.on == false) return;
  player.chat = player.nick @ ":" SPC player.chat;
}


cbk1994 05-05-2011 11:19 PM

Quote:

Originally Posted by xAndrewx (Post 1647843)
code

You should use PHP tags for posting code—it looks a lot nicer :).

Mark Sir Link 05-05-2011 11:23 PM

shouldn't be evaling a bool like that,

if (this.on) is sufficient

xAndrewx 05-06-2011 08:22 AM

Quote:

Originally Posted by Mark Sir Link (Post 1647847)
shouldn't be evaling a bool like that,

if (this.on) is sufficient

ohh- why? =o

Mark Sir Link 05-06-2011 09:08 AM

Quote:

Originally Posted by xAndrewx (Post 1647921)
ohh- why? =o

it depends on the compiler but I imagine in Graal's case, it's loading the variable (boolean), comparing the operator, then loading the global (TRUE), adding an extra step or two

it's true for variables that aren't obviously booleans (like possibly even "on") that evaluating like that makes the intention much clearer, but you could try naming the var something like "isOn", "isActive", etc

xAndrewx 05-06-2011 05:50 PM

ois, ty

Emera 05-06-2011 06:05 PM

Thnks andy. :)

WhiteDragon 05-06-2011 06:20 PM

Quote:

Originally Posted by Mark Sir Link (Post 1647924)
it depends on the compiler but I imagine in Graal's case, it's loading the variable (boolean), comparing the operator, then loading the global (TRUE), adding an extra step or two

it's true for variables that aren't obviously booleans (like possibly even "on") that evaluating like that makes the intention much clearer, but you could try naming the var something like "isOn", "isActive", etc

To get technical, GS2 doesn't have booleans (true is a compile-time constant for 1). The if statement accepts an integer (coercing anything else to an integer), and executes the first branch for any non-zero integer, and the second otherwise.

Some useful snippits supporting this:
PHP Code:

temp."test" == "test";
echo(
temp.x.type()); // type 0, an integer
echo(temp.x// 1

// ---- //

echo(true.type()); // type -1, because true is rewritten at compile-time, meaning the real expression is (1).type(), which evaluates to -1

// ---- //

if (2) {
  echo(
"success"); // it echos, meaning that the first branch executes for any non-zero integer





Checking something like if (temp.x == 1) would be useful in the case that temp.x can be 2 or 3.


However, in the case that temp.x will only ever be 0 or 1, it would be a useless operation. Look at how it evaluates:

When temp.x = 1:
NPC Code:
if (temp.x == 1)      =>     if (1 == 1)     =>     if (1)



When temp.x = 0:
NPC Code:
if (temp.x == 1)      =>     if (0 == 1)     =>     if (0)   



So in this case that temp.x is either 0 or 1, that == 1 is redundant.



My styling recommendation would be to do if (temp.x) in the case that you assuming temp.x is limited to 0 and 1, but if (temp.x == 1) in any other case.

The message being that we don't need to restrict our styling to GS2's type system, because it is not strongly enforced and we can be more expressive by breaking these bounds.



Note: Technically, GS2 doesn't even have integers, it only has floats. But that's a headache for another day...


All times are GMT +2. The time now is 01:00 AM.

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