Help with your scripting

first script

first script

Postby cassius_b » 01 Mar 2018, 21:14

I am trying to write my first small script. To keep it simple I just want to make a box turn continually.
When I invoke the script editor ( micro ) I see there is some default code there. Do I need that? If so where do I insert my own code. Thanks.
cassius_b
Skyline Padawan
 
Posts: 98
Joined: 21 Mar 2016, 13:45
Skill: 3D Modeller
Skill: Programmer

Re: first script

Postby planetX » 01 Mar 2018, 21:39

Better if you keep that code.

If you're using a microscript, I assume you've added it to the entity you want to rotate. Then you have to insert this code inside the onUpdate() function:

Code: Select all
    entity.turn( objID, 0, 1, 0 )


This will turn the entity 1 unit in the Y axis while the game is running (I guess).

Have a look at the API, there you can find many functions that you can use, some with examples.

https://api.aurasoft.co.uk/index.html

Welcome to the wonderful world of scripting! :D

My specs: Windows 10 - Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz - 16 Gb ram DDR4 - NVIDIA GeForce GTX 960M 4 Gb

planetX
Skyline Mage
 
Posts: 203
Joined: 28 Nov 2016, 18:27
Skill: 3D Modeller
Skill: Level Designer
Skill: Concept artist
Skill: Great creative
Skill: Programmer

Re: first script

Postby cassius_b » 01 Mar 2018, 22:03

I already did this but nothing happens. Do I need the default code that's already there?
cassius_b
Skyline Padawan
 
Posts: 98
Joined: 21 Mar 2016, 13:45
Skill: 3D Modeller
Skill: Programmer

Re: first script

Postby planetX » 02 Mar 2018, 07:39

As I said, you have to keep that code. You need at least the onInit() and onUpdate() functions. Then add your code inside those functions depending on what you want to do. In your case, you want to turn the entity while the game is running, so you have to put your code inside the onUpdate() function.

Keep also the obj variable, as it is needed to link the script to the entity you're transforming.

My specs: Windows 10 - Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz - 16 Gb ram DDR4 - NVIDIA GeForce GTX 960M 4 Gb

planetX
Skyline Mage
 
Posts: 203
Joined: 28 Nov 2016, 18:27
Skill: 3D Modeller
Skill: Level Designer
Skill: Concept artist
Skill: Great creative
Skill: Programmer

Re: first script

Postby epsilonion » 02 Mar 2018, 10:22

Basically I would keep the obj =0;, onInit and update functions and if its not needed delete the onUpdate, it all depends on what your script is trying to do, if you do not need the onUpdate then you can delete that as well..

Code: Select all
obj = 0;

-- System initialisation - Skyline script entry point
function onInit(objID)
   sky.lprint("LUA: Script Active!");
   obj = objID;
end

-- Called after all the scenes onInit() have been called.If you need to ensure your data exits, then do you set up here
function postInit()

end

-- Updated every frame
function onUpdate(timeDelta)

end


onInit(objID)
This is called when the entity is first loaded into the scene, this is used normally to set up the object.
for instance:
Code: Select all
obj = 0; declaires the obj variable


function onInit(objID)
   sky.lprint("LUA: Script Active!");            <-- this is for debugging and prints to the console
   obj = objID;                                       <-- This gets the ID of the object from the
                                                      --paramater that has been passed to the onInit(objID)
                                                      --function and places it in the obj variable
end                                                      to be used in the script


PostInit
function postInit()

end

This is where you want to make sure your data exists and is called after the onInit(objID) function, for instance setup a table with a npc stats, I usually call a setup() function from the onInit() function to do this as it is my way of working and delete the postInit() function from the script.


onUpdate(timeDelta)

This is the update function, it updates every tick and is the most used function.

Code: Select all
function onUpdate(timeDelta)

end



Example
This is what your doing with your code, basically making an object spin..


Code: Select all
obj = 0;

-- System initialisation - Skyline script entry point
function onInit(objID)
   sky.lprint("LUA: Script Active!");
   obj = objID;
end

-- Updated every frame
function onUpdate(timeDelta)
      entity.turn( obj, 0, 1, 0 )
end


On every tick the object is moved...
User avatar
epsilonion
Skyline Moderator
Skyline Moderator
 
Posts: 798
Joined: 26 Feb 2015, 11:51
Location: East Yorkshire, England
Skill: Business Manager
Skill: Great creative

Re: first script

Postby epsilonion » 02 Mar 2018, 10:28

I should add...
This is already done for you as an action...

Place object in the scene,
RMB click the object Action - Editor
Under Entity, select entity - Rotate and click the right arrow button.
Close the action editor using the X in the top right of the diag box.

Under properties panel on the right side of teh screen expand the Entity - Rotate
Select SpeedX, Y or Z (or how ever you want it to spin.

To preview it - click the 3rd from the right icon on the tool bar (film real) (turn it off after to save etc).
To play it in editor - Click the 2nd from right icon in the tool bar (play button)

Once you have finished playing/preview the scene then make sure you exit the preview/play mode otherwise it will not save (unless the devs have already sorted this out I am not sure of it)..and any updated to code, material editor etc will not be made... exit the preview and play modes to continue editing your scene..


Hope this helps
User avatar
epsilonion
Skyline Moderator
Skyline Moderator
 
Posts: 798
Joined: 26 Feb 2015, 11:51
Location: East Yorkshire, England
Skill: Business Manager
Skill: Great creative

Re: first script

Postby cassius_b » 02 Mar 2018, 15:00

Thanks for replies, very usefull.
I don't really NEED a spinning object in my game I just wanted to do something visible with my first script attempt. Thanks.
cassius_b
Skyline Padawan
 
Posts: 98
Joined: 21 Mar 2016, 13:45
Skill: 3D Modeller
Skill: Programmer

Re: first script

Postby SolarPortal » 02 Mar 2018, 15:28

It seems everyone else got to it before us and great responses guys :)
Conceive, Quantify & Do!
Skyline Game Engine - Lead Developer.
Please provide as much information as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 3770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3071
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: first script

Postby cassius_b » 02 Mar 2018, 20:20

Working fine now. I think the problem was I used a preset model. As soon as I loaded in my own box model it worked.
Is it possible to program a whole game in one big script. Is that what scene scripts are for?
cassius_b
Skyline Padawan
 
Posts: 98
Joined: 21 Mar 2016, 13:45
Skill: 3D Modeller
Skill: Programmer

Re: first script

Postby planetX » 02 Mar 2018, 21:27

cassius_b wrote:Is it possible to program a whole game in one big script. Is that what scene scripts are for?


Exactly. :D

You just have to create some variables referencing the entities you want to control. You can either link entities you have in the editor or spawn them from the script. In the scene script there's a variable called obj as well, but in this case it refers to the scene entity. Don't delete it. :lol:

Then, let's say you have a cube mesh in the editor, it will have an ID that you can find in the properties list. In the script, just create a variable like this (Assuming that 3 is the ID of the cube):

Code: Select all
CUBE = 3


Then you can use CUBE in any of the functions you write, i.e:

Code: Select all
entity.turn( CUBE, 0, 1, 0 )


Hope it helps!

My specs: Windows 10 - Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz - 16 Gb ram DDR4 - NVIDIA GeForce GTX 960M 4 Gb

planetX
Skyline Mage
 
Posts: 203
Joined: 28 Nov 2016, 18:27
Skill: 3D Modeller
Skill: Level Designer
Skill: Concept artist
Skill: Great creative
Skill: Programmer

Re: first script

Postby cassius_b » 02 Mar 2018, 21:43

It sure does help. Thanks to all.
cassius_b
Skyline Padawan
 
Posts: 98
Joined: 21 Mar 2016, 13:45
Skill: 3D Modeller
Skill: Programmer


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 1 guest

cron