Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-09-2018, 02:06 AM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is a jewel in the roughMysticalDragon is a jewel in the rough
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
Barebone Baddy System Event Based

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";

Attached Files
File Type: txt ai_module.txt (1.0 KB, 1043 views)
File Type: txt ai_variables.txt (881 Bytes, 1036 views)
__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]




Last edited by MysticalDragon; 03-09-2018 at 02:20 AM..
Reply With Quote
  #2  
Old 03-09-2018, 11:37 PM
kittygirl765 kittygirl765 is offline
tapatapatapa
kittygirl765's Avatar
Join Date: Dec 2001
Location: Caught in a tree
Posts: 70
kittygirl765 is on a distinguished road
Send a message via ICQ to kittygirl765
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
__________________
I <3 UN
I <3 Shadow Strip Entertainment City Shangri-La
I <3 gscript2
Reply With Quote
  #3  
Old 03-11-2018, 10:43 PM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is a jewel in the roughMysticalDragon is a jewel in the rough
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
Quote:
Originally Posted by kittygirl765 View Post
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];

__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]




Last edited by MysticalDragon; 03-11-2018 at 11:01 PM..
Reply With Quote
  #4  
Old 03-13-2018, 04:07 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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);

__________________
Quote:
Reply With Quote
  #5  
Old 03-13-2018, 06:43 PM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is a jewel in the roughMysticalDragon is a jewel in the rough
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
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.
__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]




Last edited by MysticalDragon; 03-13-2018 at 07:38 PM..
Reply With Quote
  #6  
Old 03-16-2018, 11:32 AM
Kamaeru Kamaeru is offline
G2k1
Kamaeru's Avatar
Join Date: Dec 2001
Posts: 1,040
Kamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud of
Quote:
Originally Posted by fowlplay4 View Post
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
__________________
3DS friendcode: 1118-0226-7975
Reply With Quote
  #7  
Old 03-16-2018, 07:25 PM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is a jewel in the roughMysticalDragon is a jewel in the rough
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
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.
__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]




Last edited by MysticalDragon; 03-16-2018 at 11:50 PM..
Reply With Quote
  #8  
Old 03-17-2018, 03:15 PM
Kamaeru Kamaeru is offline
G2k1
Kamaeru's Avatar
Join Date: Dec 2001
Posts: 1,040
Kamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud of
Quote:
Originally Posted by MysticalDragon View Post
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.
__________________
3DS friendcode: 1118-0226-7975
Reply With Quote
  #9  
Old 03-17-2018, 05:36 PM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is a jewel in the roughMysticalDragon is a jewel in the rough
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
Quote:
Originally Posted by Kamaeru View Post
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.
__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]



Reply With Quote
  #10  
Old 03-18-2018, 05:36 AM
Kamaeru Kamaeru is offline
G2k1
Kamaeru's Avatar
Join Date: Dec 2001
Posts: 1,040
Kamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud of
Quote:
Originally Posted by MysticalDragon View Post
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
__________________
3DS friendcode: 1118-0226-7975
Reply With Quote
  #11  
Old 03-18-2018, 07:58 AM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is a jewel in the roughMysticalDragon is a jewel in the rough
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
Did you misread what I said? I never said you couldn't I said why i preferred the method I chose.
__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]



Reply With Quote
  #12  
Old 03-18-2018, 07:45 PM
Kamaeru Kamaeru is offline
G2k1
Kamaeru's Avatar
Join Date: Dec 2001
Posts: 1,040
Kamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud ofKamaeru has much to be proud of
Quote:
Originally Posted by MysticalDragon View Post
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
__________________
3DS friendcode: 1118-0226-7975
Reply With Quote
  #13  
Old 03-18-2018, 08:01 PM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is a jewel in the roughMysticalDragon is a jewel in the rough
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
Quote:
Originally Posted by Kamaeru View Post
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)
__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]



Reply With Quote
  #14  
Old 03-20-2018, 07:49 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
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.
__________________
Reply With Quote
  #15  
Old 04-02-2018, 11:21 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Holy **** sauce there's actually a new client!?
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 12:15 AM.


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