Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Barebone Baddy System Event Based (https://forums.graalonline.com/forums/showthread.php?t=134270606)

MysticalDragon 03-09-2018 02:06 AM

Barebone Baddy System Event Based
 
2 Attachment(s)
On IOS Servers using timeouts is really bad, so its recommended to avoid them when possible.

Here is just a bare-bone version of something that can be turned into a baddy system. Instead of the traditional timeouts it uses scheduleEvents and is event based with using flags like players.

Both classes attached.


PHP Code:

ExampleLevel NPC
const INIT_STATE "float";
const 
GLOBAL_STATE "float";

function 
onCreated() {
  
this.join("ai_module");
  
this.join("ai_variables");
  
this.savelocally true;
  
_set("maxhealth"100);
  
temp.maxHealth this._get("maxhealth");
  
this._set("health"temp.maxHealth);
  
// this.globalState = GLOBAL_STATE ;
  // this.enterState(this.globalState);

  
changeState(INIT_STATE);
}

function 
ai_enter_float() {
  
this.ani "idle";
  
this.aiPeriod 1;
}

function 
ai_execute_float() {
  
//logic here move?
  
this.ani "move";



kittygirl765 03-09-2018 11:37 PM

Oh, this is actually a good starting place for a state machine (especially for gscript2, where making ai isn't easily done natively or ends up becoming spaghetti code)

Now I need to make an ai_execute_firing_my_lasers() function

MysticalDragon 03-11-2018 10:43 PM

Quote:

Originally Posted by kittygirl765 (Post 1742196)
Oh, this is actually a good starting place for a state machine (especially for gscript2, where making ai isn't easily done natively or ends up becoming spaghetti code)

Now I need to make an ai_execute_firing_my_lasers() function


PHP Code:

function ai_enter_float() { 
  
this.ani "idle"
  
this.aiPeriod 1


function 
ai_execute_float() { 
  
temp.pl this.acquireTarget(); //you can do findnearestplayers to see if there is a target
  
if (temp.pl != nil) {
    
this.ani "alert";
    
changeState("attack");
    return;
  }
  
//logic here move? 
  
this.ani "move"
}

function 
ai_enter_attack() {
  
this.aiPeriod 0.4 this.attack_speed;
}

function 
ai_execute_attack() {  
   
temp.target this.acquireTarget();
  if (
temp.target == nil) {
    
changeState("float");
    return;
  }
  
attack(temp.target);
}

function 
attack(temp.target) {
  
temp.pos_x this.0.5 vecxthis.dir ) * 1.5;
  
temp.pos_y this.vecythis.dir ) * 1.5;
  
temp.angle getangletemp.target.temp.pos_xtemp.target.temp.pos_y );
  
temp.speed 1;
  
setshootparamsthistemp.dmgparams);
  
shoot(
     
temp.pos_xtemp.pos_y0,
     
temp.angle0.1temp.speed,
     
this.ani_arrownull
   
);
}

function 
acquireTarget() {
  
// Add aggro to players near entity
  
temp.targets findnearestplayers(this.xthis.y);
  return 
temp.targets[0];



fowlplay4 03-13-2018 04:07 PM

there's nothing wrong with using a regular timeout.

polling (timeout, scheduleevent) is what's to be avoided if there's an applicable event (onplayerenters, onplayerhit, etc.) that can eliminate the need for a constantly running script.

the code would work exactly the same with a regular timeout!!!! unless u want to make inefficient baddies that tick every <0.05 sec.

PHP Code:

function onPlayerEnters() {
  
setTimer(this.aiPeriod);
}

function 
onTimeout() {
  if (
players.size() <= 0)
    return;
  
ai_execute();

  
setTimer(this.aiPeriod);
}

function 
enterState(temp.state) {
  if (
this.("ai_enter_"temp.state) != nil)
    
this.("ai_enter_"temp.state)();
}

function 
executeState(temp.state) {
  if (
this.("ai_execute_"temp.state) != nil)
    
this.("ai_execute_"temp.state)();
}

function 
exitState(temp.state) {
  if (
this.("ai_exit_"temp.state) != nil)
    
this.("ai_exit_"temp.state)();
}

function 
changeState(temp.state) {
  if (
this.currentState == temp.state) return;

  
this.exitState(this.currentState);

  
this.previousState this.currentState;
  
this.currentState temp.state;

  
this.enterState(this.currentState);

  
setTimer(this.aiPeriod);
}

function 
ai_execute() {
  
this.executeState(this.globalState);
  
this.executeState(this.currentState);



MysticalDragon 03-13-2018 06:43 PM

very nice, I just always had issues regarding latency when it comes to using timeouts with new Unity Engine with Graal. Sometimes when using timeouts NPCs would skip ticks and seems as if the NPC is hopping. I also noticed polling scheduleEvents hurt majorly as well.

Kamaeru 03-16-2018 11:32 AM

Quote:

Originally Posted by fowlplay4 (Post 1742203)
the code would work exactly the same with a regular timeout

That's what I was thinking but as a noob coder I wasn't going to say it

MysticalDragon 03-16-2018 07:25 PM

I don't think you could say anything as you never worked on the Unity Client before. Unity client is more touche then the C++ Basic client. Last 4 months I been focusing on with Merlin on eliminating lag. And the number 1 culprit has been timeouts. And I'm not even talking about polling timeouts.

Kamaeru 03-17-2018 03:15 PM

Quote:

Originally Posted by MysticalDragon (Post 1742216)
stuff

What I mean to say is, I have read a few different sources talking about timeouts and why you shouldn't use them, and a few sources seemed to imply that using scheduleevent() was more optimal. But in my limited personal experience I found that timeout and scheduleevent are the exact same thing.

MysticalDragon 03-17-2018 05:36 PM

Quote:

Originally Posted by Kamaeru (Post 1742218)
What I mean to say is, I have read a few different sources talking about timeouts and why you shouldn't use them, and a few sources seemed to imply that using scheduleevent() was more optimal. But in my limited personal experience I found that timeout and scheduleevent are the exact same thing.

Well yes that is actually very true. However a timeout is continuous (always running, unless you add conditions for a return) while a scheduleEvent is being called when needed. I've seen people do things like setTimer(0) - Not even sure it works, but in my opinion it doesn't look clean at all.

Kamaeru 03-18-2018 05:36 AM

Quote:

Originally Posted by MysticalDragon (Post 1742219)
Well yes that is actually very true. However a timeout is continuous (always running, unless you add conditions for a return) while a scheduleEvent is being called when needed. I've seen people do things like setTimer(0) - Not even sure it works, but in my opinion it doesn't look clean at all.

You can use onTimeout() too lmao

MysticalDragon 03-18-2018 07:58 AM

Did you misread what I said? I never said you couldn't I said why i preferred the method I chose.

Kamaeru 03-18-2018 07:45 PM

Quote:

Originally Posted by MysticalDragon (Post 1742223)
Did you misread what I said? I never said you couldn't I said why i preferred the method I chose.

The irony is that if you think I misread what you said then you clearly misread what I said

MysticalDragon 03-18-2018 08:01 PM

Quote:

Originally Posted by Kamaeru (Post 1742224)
The irony is that if you think I misread what you said then you clearly misread what I said

You can use onTimeout() too lmao (not sure about the lmao part, nothing was even funny?)

In my post i clearly said you could do something like setTimer(0) (that is using onTimeout)

Crow 03-20-2018 07:49 PM

It's laughable that anybody would still want to make use of GScript even though somebody had the bright idea to actually write new client software.

Twinny 04-02-2018 11:21 AM

Holy **** sauce there's actually a new client!?


All times are GMT +2. The time now is 11:19 AM.

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