Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Baddie Script Review (https://forums.graalonline.com/forums/showthread.php?t=134268627)

i8bit 08-15-2013 02:39 PM

Baddie Script Review
 
1 Attachment(s)
I have attached my server's baddie script, and as discussed in the playerworld forums, I had questions about IOS scripts involving baddies.

I want to know how well/poor, organized, etc. this is coded in the goal of putting it in a mobile server.

Original Question Post:
http://forums.graalonline.com/forums...hp?t=134268621


What needs to be changed? Or does it totally need to be re-done.
I am not an expert GS2 coder as I switched from GML (Gamemaker language)
Constructive criticism would be nice :)

cbk1994 08-15-2013 06:38 PM

The whole thing seems to be a mess of different scheduled events. You should clean that up a lot. Also, the minimum timeout/scheduleEvent time on serverside is 0.1, not 0.05. You seem to be scheduling events even in areas where you don't need to wait (e.g. when adding experience, why not just call the function instead of scheduling it for 0.1 seconds in the future?)

I don't understand why you need a timeout checking if the baddy is dead every 0.1 seconds. Just check if it's dead when it gets attacked and handle it then.

There are also a lot of random errors like in onAttack where you loop through the players (with temp.p) but then use the player variable, which is going to either be unset or set to an arbitrary player.

I also noticed some typos like this.aggressive / this.agressive.

I didn't look very closely but there are a lot of big problems.

i8bit 08-15-2013 07:29 PM

Quote:

Originally Posted by cbk1994 (Post 1721753)
The whole thing seems to be a mess of different scheduled events. You should clean that up a lot. Also, the minimum timeout/scheduleEvent time on serverside is 0.1, not 0.05. You seem to be scheduling events even in areas where you don't need to wait (e.g. when adding experience, why not just call the function instead of scheduling it for 0.1 seconds in the future?)

I don't understand why you need a timeout checking if the baddy is dead every 0.1 seconds. Just check if it's dead when it gets attacked and handle it then.

There are also a lot of random errors like in onAttack where you loop through the players (with temp.p) but then use the player variable, which is going to either be unset or set to an arbitrary player.

I also noticed some typos like this.aggressive / this.agressive.

I didn't look very closely but there are a lot of big problems.


I understand.. Now that I look it over it is pretty unacceptable.

callimuc 08-15-2013 09:20 PM

also try to get familiar with the move() function, will help you a lot

i8bit 08-16-2013 01:31 AM

Quote:

Originally Posted by callimuc (Post 1721758)
also try to get familiar with the move() function, will help you a lot

Can you give me an example of using the move() function?

callimuc 08-16-2013 03:07 AM

Quote:

Originally Posted by i8bit (Post 1721764)
Can you give me an example of using the move() function?

using scripthelp:
TServerNPC.move(float, float, float, int) - moves the npc smoothly, parameters are delta x, delta y, time and options: cache type (0, 1-cache, 2-append) + blockcheck(4) + eventwhendone(8) + applydir(16)

example:
PHP Code:

function onCreated() {
  
this.image "block.png";
  
this.this.30;
  echo(
"Starting to move...");
    
//Move NPC by 2 tiles in the x, 2 tiles in the y within 0.5 secs
    //While the last parameter is 8 (check description above script example)
    //The event 'onMovementFinished()' will be called
  
move(220.58);
}
function 
onMovementFinished() {
  echo(
"Done moving!");


With some maths you can tweak around the delta x/y and have it move around the player easy.


Ps: This move example replaced something like this which does lag more on the server
PHP Code:

function onCreated() {
  
this.image "block.png";
  
this.this.30;
  echo(
"Starting to move...");
  
temp.time = (0.05/0.5); //steps divided by the time it should take
  
for (temp.i=0;temp.i<2;temp.i+=temp.time) {
    
this.+= temp.time;
    
this.+= temp.time;
  }
  echo(
"Done moving!");



Cubical 08-16-2013 12:17 PM

On an unrelated note you may want to create some getter and setter functions instead of doing things such as

PHP Code:

clientr.exp += this.expgain

It helps if you want to add some validation or a new variable to how it's calculated later on such as experience potions or checks to make sure they do not level past the max level. The main reason you would want to use getter and setter functions is if you need to go change something such as the flag name or whatever formula you use to calculate the experience you wont need to go back and modify every line of code that adds experience. I'm at work so if it doesn't make too much sense I can reexplain later if Chris Vimes hasn't already answered your questions

join the player to a class called player_functions or whatever you want to call it doing player.join("player_functions"); Then within that class do something similar to the following

PHP Code:

function addExp(amount){
  
clientr.exp += amount;


then whenever you need to add experience you can do

PHP Code:

player.addExp(8675039); 

Then if you want to add checks later you can just do the following in player_functions instead of going back and modifying 100 times in all your code

PHP Code:

public function addExp(amount){
  
temp.exptable = { 0102040,75100 };
  if (
clientr.exp amount <= temp.exptable[clientr.level]){
    
clientr.exp += amount;
  } else 
player.levelUp();
}

function 
levelUp(){
  
clientr.level++;
  
clientr.exp 0;


This also helps prevent unneeded code duplication

callimuc 08-16-2013 06:34 PM

Quote:

Originally Posted by Cubical (Post 1721769)
PHP Code:

function addExp(amount){
  
clientr.exp += amount;


...
PHP Code:

function addExp(amount){
  
temp.exptable = { 0102040,75100 };
  if (
clientr.exp amount <= temp.exptable[clientr.level]){
    
clientr.exp += amount;
  } else 
player.levelUp();
}

function 
levelUp(){
  
clientr.level++;
  
clientr.exp 0;



the addExp() function needs to be a public function

Cubical 08-16-2013 08:16 PM

whoops and fixed

MysticalDragon 08-16-2013 09:07 PM

To answer your question, it has to be completely redone. The code is terrible, and your abusing schduleevent as if it was a timeout :/. I might release delterias Baddie system on the forums soon. You could probably at least learn from it.

PHP Code:

//Can be Used for Baddies
public function addMobExperience(temp.mob_leveltemp.mob_base_exp) {
  
// Find XP multiplier
  
temp.curr_level this._get("pve_level");  //Player Level (clientr.pve_level)
  
temp.level_difference temp.mob_level temp.curr_level//Mob Level
  
temp.difference max(-5min(temp.level_difference5)); //Difference
  
temp.multiplier = (temp.difference/5); //Multiplier

  // Add experience  
  
addExperience(temp.mob_base_exp temp.multiplier);
}

//Can be used for Quests etc
public function addExperience(temp.amount) {
  
temp.amount *= 1;  //Incase you have events like double EXP
  
temp.curr_exp this._get("pve_exp");  //clientr.pve_exp
  
temp.curr_exp += temp.amount;
  
// Check if we reached a new level
  
if (temp.curr_exp >= this._get("pve_nextlevelexp")) {
  
/* Only used on Delteria
    // Add enchantment point when leveling up
    temp.enchantment_points = this._get("enchantment_points");
    this._set("enchantment_points", temp.enchantment_points + 1);
  */
    
temp.new_level this._get("pve_level") + 1//clientr.pve_level + 1;
   /* If you want them to still gain EXP for extra things like CMD Points after     max level (25)
    if (temp.new_level >= 26) {
      // reset experience points so they can get more enchantment points
      this._set("pve_exp", 0);
      return;
    }
  */
    
this._set("pve_level"temp.new_level); //clientr.pve_level = temp.new_level;
    
this._set("pve_exp"0); //clientr.pve_exp = 0;
    
this._set("pve_nextlevelexp"1000 temp.new_level); //clientr.pve_nextlevelexp = 1000 * temp.new_level

    //Floating Text to Display player leveling up
    
this.triggerclient("weapon""Player/DamageDisplay""text2",
      
player.random(01),
      
player.random( -.51),
      
"Level Up! (" SPC clientr.pve_level SPC ")",
      
25512800"blue"
    
);
    
this.updateProperties(); //Updat properties (only used on Delteria)
  
} else {
    
this._set("pve_exp"temp.curr_exp);
    
this.sendNotification("You gained" SPC  temp.amount SPC "experience!"1"expicon.png""");//Iphone Notification
  
}



i8bit 08-16-2013 11:08 PM

Quote:

The code is terrible
Well I mind as well give up.. lol

I really am trying. Fact of the matter is I am not a good programmer haha.

But I appreciate all the tips.

Perhaps I just need to re-learn GS2 from scratch, and broadin my knowledge of different functions.

Tricxta 08-16-2013 11:11 PM

Quote:

Originally Posted by i8bit (Post 1721774)
Well I mind as well give up.. lol

I really am trying. Fact of the matter is I am not a good programmer haha.

But I appreciate all the tips.

Don't be disheartened, it's rare for someone to master a language straight off the bat without some kind of code review. Just stick with it.

i8bit 08-17-2013 01:10 PM

For the baddie being hit, could I just use the default onwas.Hit() function as long as the player's ani is something_sword. Or trigger in manually?

Also, does the default onwas.Hit() work when a player hits a player?


All times are GMT +2. The time now is 05:51 AM.

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