Help with your scripting

getting started in LUA for Skyline

getting started in LUA for Skyline

Postby AuShadow » 22 Oct 2015, 07:16

Hey guys, Just wanted to start making some basic scripts but am basically full blown noob in this area, what i want to do is

1. check player distance to object
2. if player is within said distance allow an action to happen if a key is pressed, sort of as follows with script attached to a tree

starting variable of each tree would be 5 on starting

if PlayerDistance <50 then
if KeyPressE =1 then
wood = wood + 1
tree = tree - 1
end
else
end
else
end


if tree == 0 then
destroy(tree) -- the one that the script is attached to and not all trees
else
end


any help appreciated, just thinking along the lines of a survival game where you collect resources, cheers
User avatar
AuShadow
Skyline Ensen
 
Posts: 129
Joined: 17 Feb 2015, 11:18
Skill: Great creative

Re: getting started in LUA for Skyline

Postby epsilonion » 22 Oct 2015, 10:58

Using rays to check distance, I have done something like this before by adding a trigger to a tree and if the distance is less then ten then press e to chop down tree. This plays an animation then gets the position of the tree deletes the model and places a model of a log with a pick up script so when the player presses e on the log it gets placed in the inventory and delete the log model or a timer runs out and deletes the model..

I am on my phone at the moment so will look at this later for you..
User avatar
epsilonion
Skyline Lead Moderator
Skyline Lead Moderator
 
Posts: 874
Joined: 26 Feb 2015, 11:51
Location: Hull, East Yorkshire, England
Skill: Business Manager
Skill: Great creative

Re: getting started in LUA for Skyline

Postby ant0N » 22 Oct 2015, 15:16

Hi, AuShadow!
1) You can find the distance:
Code: Select all
--in script for wood
player = 0
wood  = 0
tree    = 10
dist     = 0
obj     = 0
function onInit(objID)
    obj = objID
    player = entity.getIDFromTag("player_tag")
end
function onUpdate()
    dist = entity.getDistance(obj, player)
 
    if dist > 50 then return end
    if input.keyHit("e") == 1 then
        wood = wood + 1
        tree = tree - 1   
    end
end
Sorry for my English. :)
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: getting started in LUA for Skyline

Postby SolarPortal » 22 Oct 2015, 16:29

Thanks for responding guys, i didn't respond on purpose to see if the community would bring a script up and you did :) Very happy :D
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: getting started in LUA for Skyline

Postby epsilonion » 22 Oct 2015, 19:15

been busy all day still have not had chance to do anything but I will soon.. :)
User avatar
epsilonion
Skyline Lead Moderator
Skyline Lead Moderator
 
Posts: 874
Joined: 26 Feb 2015, 11:51
Location: Hull, East Yorkshire, England
Skill: Business Manager
Skill: Great creative

Re: getting started in LUA for Skyline

Postby epsilonion » 23 Oct 2015, 00:33

Not having much look here, couldnt get raycast working (first attempt with skyline), tried the AI sensor thing and it couldnt see the Player when it entered the dome..

Completely lost now, heads exploding and tired...
User avatar
epsilonion
Skyline Lead Moderator
Skyline Lead Moderator
 
Posts: 874
Joined: 26 Feb 2015, 11:51
Location: Hull, East Yorkshire, England
Skill: Business Manager
Skill: Great creative

Re: getting started in LUA for Skyline

Postby AuShadow » 23 Oct 2015, 00:40

thanks all, better rest that head Liam i know the feeling of about to explode, had so much going before during and since the wedding im still waiting for everything to calm down a bit around here :), thanks for all the help I am going to start playing around with that one now ant0N cheers.

another quick question, just want to know how to display a prompt on the screen with the value of the variable, just so i can see things working, thanks again
User avatar
AuShadow
Skyline Ensen
 
Posts: 129
Joined: 17 Feb 2015, 11:18
Skill: Great creative

Re: getting started in LUA for Skyline

Postby epsilonion » 23 Oct 2015, 00:45

This is the reply I got when using the entity.getDistance(obj, player) on another thread/post/thingy..

I will have another look tomorrow night, come back with a fresh mind and all that..

@liam, you could use this but it would be very expensive. Using a raycast would be better as an FPS will need one already for certain mechanics like pressing switches etc.. so it would need a simple
Code: Select all
-- rough code, not copyable all from head
function onUpdate(td)
   hitID = physics.rayFromPoint() -- or whatever command
   if(hitID ~= -1)then
      if( hitID == switchID )then
          -- open door -- trigger explosions etc...
      end

      if( hitID == VehicleID )then
          -- get distance from ray not using the getDistance function
                         if(distance < 2)then
                              -- get in car
                         end
      end
   end
end
User avatar
epsilonion
Skyline Lead Moderator
Skyline Lead Moderator
 
Posts: 874
Joined: 26 Feb 2015, 11:51
Location: Hull, East Yorkshire, England
Skill: Business Manager
Skill: Great creative

Re: getting started in LUA for Skyline

Postby CreativeOcclusion » 23 Oct 2015, 08:43

another quick question, just want to know how to display a prompt on the screen with the value of the variable, just so i can see things working, thanks again

I believe you are looking for sky.lprint to print to console...here is an example...

Code: Select all
function getValue(float value)
    myValue = value * 2;
    return myValue;
end

test = getValue(20);
sky.lprint(test); -- this will print 40 to the console.
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas

Re: getting started in LUA for Skyline

Postby ant0N » 23 Oct 2015, 11:04

or you can use the ui commands.
http://www.chi-ad.com/Skyline/API/Lua/html/classui.html

Code: Select all
obj         = 0
function onInit(objID)
    obj = objID;

    ui.newLayer("ui1");
    ui.setLayer("ui1");
    ui.setFontSize(14);
    ui.newText("ui1",100,100);
    ui.setColor("ui1", 1, 0, 0);
    ui.text("ui1", "NEW TEXT 1");
    ui.setLayerVisible("ui1",1); 
end

function onKeyDown( key )
    if(key=="j") then
        ui.setLayerVisible("ui1",1);
    end
   
    if(key=="k") then
        ui.setLayerVisible("ui1",0);
    end
end

function onStop()
    ui.setLayerVisible("ui1",0);
end
Sorry for my English. :)
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: getting started in LUA for Skyline

Postby SolarPortal » 23 Oct 2015, 13:05

great responses guys, i think a raycast would be the best method as liam says, especially since the FPS already needs one.
@liam, i sometimes go into lua and go "i cant find or remember how to do this..." lmao :D Fresh head will always help :)

If you want to print directly onto the render screen instead of in the console log, then simply use:
Code: Select all
-- | Set the "state" to 1 to clear all lines before printing, and 0 to allow lines to be added down the screen.
sky.print("myVar: "..myVar, (optional) state);


@antON, yes that is also possible, however, i do not trust the ui.function() library as it was the first system before all the rest.
The only thing is does do for you is positional text whereas the simple prints are where they are. or are they lol :P maybe you can use the gui commands to position the text... thinking out loud again lmao :D

After loading many, many scenes using the ui.function() library, it seems to stop working and displaying text, which is why GUI is better to use now lol :P

I will put a raycast demo together today if i find time. Really coming close to the end of this release now :D
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: getting started in LUA for Skyline

Postby AuShadow » 23 Oct 2015, 15:33

Thanks guys will have a crack at this Monday, got family duties for the weekend :), young fella walking already so keeps me busy :)
User avatar
AuShadow
Skyline Ensen
 
Posts: 129
Joined: 17 Feb 2015, 11:18
Skill: Great creative

Re: getting started in LUA for Skyline

Postby SolarPortal » 23 Oct 2015, 19:05

:)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: getting started in LUA for Skyline

Postby AuShadow » 24 Oct 2015, 04:21

thanks all the input guys, not sure on using raycast at the moment, would like to just not sure how to write it :P, and testing using the third person character, here is what I have now:
Code: Select all
--in script for wood
player = 0
wood  = 0
local tree    = 10
local dist     = 0
local obj     = 0
local vis    = 1
local timer = 0

function onInit(objID)
    obj = objID
    player = entity.getIDFromTag("player_tag")
end
function onUpdate()
    dist = entity.getDistance(obj, player)
   -- | Set the "state" to 1 to clear all lines before printing, and 0 to allow lines to be added down the screen.
   sky.print("wood: "..wood.."tree:"..tree, 1);
 
    if dist > 50 then return end
    if input.keyHit("e") == 1 and vis == 1 then
        wood = wood + 1
        tree = tree - 1   
    end
   
   if tree == 0 then
   entity.setVisible( obj , 0 );
   vis = 0;
   timer = 1
   end
   
   if timer == 1 then
   -- Need something here to reset vis to 1 after a certain amount of time has passed
   end
end


this all works a treat and stops the tree from being harvested once it is hidden, but not sure how to get the timer to work so i can reset the vis variable and allow to tree to seem to Respawn, in my old engine there was an inbuilt tag called Gtime and i use to write a small timer as:

local timestamp = 1
local time1 = 0
local time2 = 0
local timepassed = 0

function main
time2 = Gtime
timepassed = time2 - time 1

if timestamp == 1 then
time1 = Gtime
timestamp = 0
end

and that would give me a basic timer by checking the timepassed variable, is something like this easier and better, easy enough to do in skyline?
User avatar
AuShadow
Skyline Ensen
 
Posts: 129
Joined: 17 Feb 2015, 11:18
Skill: Great creative

Re: getting started in LUA for Skyline

Postby Shando » 24 Oct 2015, 05:25

Hey buddy,

There is an in-built timer which is detailed in the API here:

http://www.chi-ad.com/Skyline/API/Lua/html/classtime.html

Basically, if you want multiple timers then you simply set them up when your tree disappears with the following:

Code: Select all
time.startMultiTimer ( 1, objID, 1000 );


where the first param is the number of the timer, the 2nd is the object ID and the last is the time in milliseconds.

Then, you create an "onMultiTimer" function, which will be called automatically by Skyline:

Code: Select all
function onMultiTimer ( timerID )
    if ( timerID == 1 ) then
        time.stopMultiTimer ( 1, objID );
        -- RESPAWN OF TREE GOES HERE
    end
end


HTH

Shando

PS: MultiTimers DO NOT take any notice of the current Game State, whereas the "startTimer" and "stopTimer" functions will ONLY work in a specific Game State

PPS: you could also use the Lua time functions, but the above is much easier :)
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: getting started in LUA for Skyline

Postby SolarPortal » 24 Oct 2015, 10:38

Instead of raycast or distance check, you could use an AI Sensor or triggerbox as well. Then an AI could harvest the tree aswell :)

It all sounds like it is starting to get where you want now :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: getting started in LUA for Skyline

Postby AuShadow » 26 Oct 2015, 01:46

ok guys, so now I have this:
Code: Select all
--in script for wood
player = 0
wood  = 0
local tree    = 10
local dist     = 0
local obj     = 0
local vis    = 1
local timer = 0

function onInit(objID)
    obj = objID
    player = entity.getIDFromTag("player_tag")
end

function onUpdate()
    dist = entity.getDistance(obj, player)
   -- | Set the "state" to 1 to clear all lines before printing, and 0 to allow lines to be added down the screen.
   sky.print("wood: "..wood.."tree:"..tree, 1);
 
    if dist > 50 then return end
    if input.keyHit("e") == 1 and vis == 1 then
        wood = wood + 1
        tree = tree - 1   
    end
   
   if tree == 0 then
   entity.setVisible( obj , 0 );
   vis = 0;
   timer = 1
   end
   
   if timer == 1 then
   -- Need something here to reset vis to 1 after a certain amount of time has passed
   time.startMultiTimer(1,obj,3000);
   end
end

function onMultiTimer ( 1 )
    if ( 1 == 1 ) then
        time.stopMultiTimer ( 1, obj );
        -- RESPAWN OF TREE GOES HERE
      tree = 10
      vis = 1
      timer = 0
      entity.setVisible( obj , 1 );
   end
end


which works, until i added the function onMultiTimer, what have i buggered up?, cheers again especially for being patient :D
User avatar
AuShadow
Skyline Ensen
 
Posts: 129
Joined: 17 Feb 2015, 11:18
Skill: Great creative

Re: getting started in LUA for Skyline

Postby SolarPortal » 26 Oct 2015, 02:21

Here you go m8, i believe this is what you wanted.

What was happening is that the multi timer was being retriggered and reset every update cycle which is why the onMultiTimer() function was never called. Also the onMultiTimer() function had an integer sent through as its argument, i changed this to timerID as this is the variable which is the number of the timer being triggered.
I also lowered the distance check on the player to only a couple of meters instead of 50.

Anyway here is the script back:
Code: Select all
--in script for wood
player = 0
wood  = 0
local tree    = 10
local dist     = 0
local obj     = 0
local vis    = 1
local timer = 0
local timerTriggerd = false

function onInit(objID)
    obj = objID
    player = entity.getIDFromTag("player_tag")
end

function onUpdate()
   dist = entity.getDistance(obj, player)
   -- | Set the "state" to 1 to clear all lines before printing, and 0 to allow lines to be added down the screen.
   sky.print("wood: "..wood.."tree:"..tree, 1);

   if (dist > 2) then return end
   
   if input.keyHit("e") == 1 and vis == 1 then
      wood = wood + 1
      tree = tree - 1   
   end
   
   if (tree == 0 and timerTriggerd == false) then
      entity.setVisible( obj , 0 );
      vis = 0;
      timer = 1
      
      time.startMultiTimer(1, obj, 3000)
      timerTriggerd = true; -- Use a run trigger to only make it happen once.
   end
   
    -- | Edit: Removed as its added it to the compare above.
   --[[
   if (timer == 1) then
      -- Need something here to reset vis to 1 after a certain amount of time has passed
      time.startMultiTimer(1,obj,3000)
      
      timer = 2;
   end
   ]]
end

function onMultiTimer(timerID)
   if ( timerID == 1 ) then
      time.stopMultiTimer ( 1, obj );
      -- RESPAWN OF TREE GOES HERE
      tree = 10
      vis = 1
      timer = 0
      entity.setVisible( obj , 1 );
      timerTriggerd = false
   end
end
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: getting started in LUA for Skyline

Postby AuShadow » 26 Oct 2015, 02:26

yes, that works perfectly, thank you very much for this, now to play with it some more :P
User avatar
AuShadow
Skyline Ensen
 
Posts: 129
Joined: 17 Feb 2015, 11:18
Skill: Great creative


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 5 guests