Offline documents and videos and notification on new documents,tutorials and videos

[Beginner] AI tutorial for beginners

[Beginner] AI tutorial for beginners

Postby SpiderMack » 29 Sep 2017, 12:32

Hi,

This tutorials is for beginners to learn coding game character zombie AI in Skyline.

It's a step by step and progressive tutorial, we'll start with very basics and each new tutorial will add new features.


1) Drop "Fps player" object in the scene
2) With "Asset Manager" seek character file in "Models" folder : "Zombie.mesh" and drop it in the scene
3) Add a "Simple character controller" to the zombie

4) Check physics scene debugger and adjust Capsule collision values
Image

5) Crate a Lua file with code below


Code: Select all
sky.include ( "random.lua" )


obj = 0;
moveMaxSpeed          = 5;
interval_change_direction    = 1700;
TIMER_DIRECTION       = 1;

function onInit(objID)

   obj = objID;
   
   character.setGravity(obj, 0, -10, 0);

   playerID = entity.getEntityIDFromTag("Player");
 
   -- create a new timer to change direction
    time.startTimer(TIMER_DIRECTION, obj, interval_change_direction);
   
end

function onUpdate( td )
   character.move(obj, moveMaxSpeed );   
   --entity.turn(obj, 0, 60*td, 0)
   --getOppositeDirection();
end

function getRandomDirection()
   
   -- get zombie position
   direction = newType.vec3(entity.getPosition(obj));
   -- get random values
   randomX =  math.random(10,10);
   randomZ =  math.random(-10,10);
   
     sky.print(randomX);
   sky.print(randomZ);
   --add random values to zombie position , used as next position to move
   direction.x =  direction.x + randomX; 
   direction.z =  direction.z + randomZ ;
   
    --rotate zombie to next position
   entity.setDirection(obj,direction.x, 0, direction.z);
   
end

function onTimer()
   -- every time intervall call function to change zombie direction
    getRandomDirection() ;
end


6) Attach previous Lua code file to zombie
Image

Press run to see the zombie moving and changing direction.

In this tutorial you learned how to use :
* Simple character controller set up
* "sky.include" needed to use external Lua scripts It looks it's not needed.
* set character gravity
* one simple timer
* Random function usage
* get entity position and Vector 3 manipulation


Later we can extend this tutorials adding animations and using a smooth turn function.


Note: How to change image size on forums :?:
Last edited by SpiderMack on 29 Sep 2017, 17:36, edited 7 times in total.
User avatar
SpiderMack
Skyline Warrior
 
Posts: 346
Joined: 02 Dec 2016, 09:15
Skill: Concept artist
Skill: 3D Modeller
Skill: Level Designer
Skill: Scripter

Re: [Beginner] AI tutorial for beginners

Postby StarFire » 29 Sep 2017, 13:28

Nice to see a tutorial from you :)

note you did not provide the random.lua include file ;)
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1371
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: [Beginner] AI tutorial for beginners

Postby SpiderMack » 29 Sep 2017, 15:55

StarFire wrote:Nice to see a tutorial from you :)

note you did not provide the random.lua include file ;)



The include is the code file itself, there is no other file to include, it includes itself :lol:
If you don't put that line in top of the script that is external , the editor crashes.
User avatar
SpiderMack
Skyline Warrior
 
Posts: 346
Joined: 02 Dec 2016, 09:15
Skill: Concept artist
Skill: 3D Modeller
Skill: Level Designer
Skill: Scripter

Re: [Beginner] AI tutorial for beginners

Postby StarFire » 29 Sep 2017, 16:05

Ah no, excerpt from manual:
External Script
This script resides on your hard drive and can be referenced by any object from any scene. Great for when you want to make an update to the script then all objects referencing this script will also update. Downside is that all scripts remain the same and any customisation is done via the dynamic properties. These are used a lot in the game objects where systems can develop and grow then all projects using them are automatically updated.

The external script is accessed in a similar way to the micro script. As this script is file based you can either load an existing file or create a new one from scratch. So let’s take a look at how to use this script type.

1) Select the object to attach the external script to. Then RMB to open the menu and choose “Lua – Add External Script”
From the rollout menu you have the option to open a script from file or create a new script.

2) Depending upon your choice this will open a file dialog for you to choose a script or you selected the “New Script” option for you to create a new script. Then this script will be opened in the script editor for you. Once opened you can make your changes and press the save icon to save this script back to your hard drive.

You mentioned a crash, there will be another reason for the crash as this method of scripting is used daily with us. It would be good to locate it ;)
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1371
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: [Beginner] AI tutorial for beginners

Postby StarFire » 29 Sep 2017, 16:39

The script file must be saved to your hd as for example "random.lua" then load this file into the external script property.

Still not working?
When a script is created it needs to be added to the resources system, now this is done for you automatically but will throw up a box asking you do you want to add this script.... if you clicked no then it will be ignored but this will prevent you script form running correctly. If this is the case for you try this:
Go the scripts folder in asset manager and click on the folder to display its contents. Now if it’s added to resources nothing will happen, except showing the folder contents. If it’s not then the Message box will show.
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1371
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: [Beginner] AI tutorial for beginners

Postby StarFire » 29 Sep 2017, 16:46

I have just tried this tutorial and it works as long as you save the script to file and remove the include "sky.include ( "random.lua" )"

Nice simple tutorial.It would be good for other users if you could give a little explanation of what the script is actually doing, nothing too in depth but why the script does what it does :D
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1371
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: [Beginner] AI tutorial for beginners

Postby StarFire » 29 Sep 2017, 17:19

you may want to change the line randomX = math.random(10,10); to randomX = math.random(-10,10);
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1371
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: [Beginner] AI tutorial for beginners

Postby SpiderMack » 29 Sep 2017, 17:24

StarFire wrote:I have just tried this tutorial and it works as long as you save the script to file and remove the include "sky.include ( "random.lua" )"

Nice simple tutorial.It would be good for other users if you could give a little explanation of what the script is actually doing, nothing too in depth but why the script does what it does :D


Yes it's strange, i removed the include and it still works :lol:
I don't know what was happening.
User avatar
SpiderMack
Skyline Warrior
 
Posts: 346
Joined: 02 Dec 2016, 09:15
Skill: Concept artist
Skill: 3D Modeller
Skill: Level Designer
Skill: Scripter

Re: [Beginner] AI tutorial for beginners

Postby SpiderMack » 29 Sep 2017, 17:24

2nd part tutorial

The zombie will now become some more zombie :lol:
* it will always face the player
* it has some game logic (purchase and attack the player)

Edit the previous script, and replace it with the new one below :

Code: Select all


obj = 0;
moveMaxSpeed          = 20;
state = "walk ";

function onInit(objID)
   obj = objID; 
end

function postInit()
   playerID = entity.getEntityIDFromTag("Player");   
   character.setGravity(obj, 0, -10, 0);
end

function onUpdate( td )
   rotateToPlayer();
   doAction();
   playAnimation(state) ;
end


 function doAction()
   --Change state on distance to player
   distPlayer = entity.getDistanceToVector( obj, playerPos.x,playerPos.y,playerPos.z);
   --sky.print(distPlayer);
   if(distPlayer<=2.5)then
      character.move(obj, 0 );
      state = "attack";
   end
   if (distPlayer>2.5) then
      character.move(obj, moveMaxSpeed );
      state = "run";
   end
 end
 
 
 function rotateToPlayer()
    playerPos=  newType.vec3(entity.getPosition(playerID));
   yawDeg    = entity.getHeadingV3(obj,playerPos.x,playerPos.y,playerPos.z);
   entity.yaw(obj, yawDeg, 0);
end   
   
 function playAnimation(state)

   if (state == "run") then 
      anim.playAnimation(obj, "zombie_run", 30, 1);
   end
   if (state == "attack") then
      anim.playAnimation(obj, "zombie_attack", 10, 1);
   end
   
end


What you have learned :
- use postInit, to ensure all objects have been initialized before calling any function like getEntityIDFromTag
- play animations including blending (anim.playAnimation)
- rotate zombie to another object
- some game logic ( doAction() function )

What can be improved :
* keep the zombie attacking until it finishes the attack animation
instead of switching to run if the player gets away before zombie has finished his attack animation.
* Turning to player needs to be smoothed
* add some sounds for zombie
Last edited by SpiderMack on 29 Sep 2017, 18:02, edited 2 times in total.
User avatar
SpiderMack
Skyline Warrior
 
Posts: 346
Joined: 02 Dec 2016, 09:15
Skill: Concept artist
Skill: 3D Modeller
Skill: Level Designer
Skill: Scripter

Re: [Beginner] AI tutorial for beginners

Postby StarFire » 29 Sep 2017, 17:54

A bit of advice when setting up variable on the start of the game. The onInit() is called when an object is initialized in the scene and there are times that you may want to access other objects data such as the players ID. Imagine if the player was added to the scene after the ai player. In this case the player is not yet in the scene but you will be trying to get its ID which doesnt yet exist.
As you can imagine this could cause a bug in your game.
To get around this we use the function postInit()
This postInit() is called once all of the scenes objects have been initialized. So now if you call for the Players ID it will definatly exist.

Code: Select all
function postInit()
   playerID = entity.getEntityIDFromTag("Player");   
   character.setGravity(obj, 0, -10, 0);
   time.startTimer(enum.GS_GameMain(), obj, interval_change_direction);
end
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1371
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: [Beginner] AI tutorial for beginners

Postby SpiderMack » 29 Sep 2017, 18:01

Thanks for the advice.

I corrected the previous code to include the post init :)
User avatar
SpiderMack
Skyline Warrior
 
Posts: 346
Joined: 02 Dec 2016, 09:15
Skill: Concept artist
Skill: 3D Modeller
Skill: Level Designer
Skill: Scripter

Re: [Beginner] AI tutorial for beginners

Postby SpiderMack » 10 Oct 2017, 09:13

New tutorial : Moving randomly around a position


Code: Select all
obj = 0;
moveMaxSpeed          = 20;
state = "walk ";

xOrigin,yOrigin,zOrigin = 0;
destinationX,destinationZ=0;
 
stateWait = 1;

function onInit(objID)
   obj = objID;

end
 

function postInit()
   character.setGravity(obj, 0, -10, 0);
   playerID = entity.getEntityIDFromTag("Player");   
   xOrigin,yOrigin,zOrigin   = entity.getPosition(obj);
   
   time.startTimer(1, obj, 1500);
   chooseRandomDestination(); 
    
end
 

function onUpdate( td ) 
   doState();
   playAnimation(state) ;
end


function onTimer()
   stateWait = stateWait * -1 ;
   --choose a random destination
   if(stateWait ==1) then
    chooseRandomDestination(); 
   end
   --wait at destination
   --if(stateWait ==-1) then
   --end
end

 function doState()
 
   --Change state based on distance to destination
   distDestination = entity.getDistanceToVector( obj, destinationX,yOrigin,destinationZ);
 
   if(distDestination<=0.5)then
      character.move(obj, 0 );
      state = "idle"; 
   else
      character.move(obj, moveMaxSpeed );
      state = "run";
   end
   
 end

function chooseRandomDestination()
xRandom = math.random(0,5);
zRandom = math.random(0,5);
destinationX = xOrigin + xRandom;
destinationZ = zOrigin + zRandom;
rotateToDestinationr();
end

 
 function rotateToDestinationr()
    playerPos=  newType.vec3(entity.getPosition(playerID));
   yawDeg    = entity.getHeadingV3(obj,destinationX,yOrigin,destinationZ);
   entity.yaw(obj, yawDeg, 0);
end   
   
 function playAnimation(state)
   if (state == "run") then 
      anim.playAnimation(obj, "zombie_run", 30, 1);
   end
   if (state == "idle") then
      anim.playAnimation(obj, "zombie_idle", 10, 1);
   end
end



What you learned :
*How to use Random
*Use a timer

What can be improved :
- change timer use : if timer ticks before it reaches destination it will choose another direction, timer should be used only when character reached the destination to make some pause.
User avatar
SpiderMack
Skyline Warrior
 
Posts: 346
Joined: 02 Dec 2016, 09:15
Skill: Concept artist
Skill: 3D Modeller
Skill: Level Designer
Skill: Scripter


Return to Documents, Tutorials and Videos

Who is online

Users browsing this forum: No registered users and 1 guest