Help with your scripting

True Global Variables

True Global Variables

Postby Shando » 11 Dec 2013, 03:26

Hi Again,

One quick question.... ( I may be being dumb, but I can't seem to locate anything :oops: )

How can I implement "True" Global Vars? i.e. vars that persist across scripts?

Thanks

Shando

PS: I'm adding the script to the mesh at Runtime
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: True Global Variables

Postby SolarPortal » 11 Dec 2013, 13:12

To set and get global variables, use:

Code: Select all
-- set global variable
sky.setVar("varName", varValue);

-- get global variable
myNewVar = sky.getVar("varName");


These exist in the engine and are accessible from any script.
Hope this helps.

For more information, look in the Lua API "Sky" library.
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: True Global Variables

Postby Shando » 11 Dec 2013, 21:50

For more information, look in the Lua API "Sky" library


Whoops, looks like I missed them :oops:

Shando

PS: Looks like getVar only returns a String, is this correct????
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: True Global Variables

Postby SolarPortal » 11 Dec 2013, 22:51

yes, just as it can only set a string, but lua automatically typecasts integers and floats to string, which is why they work, it will also convert a string back to the needed type.

e.g.
Code: Select all
sky.setVar("intVar", 10);
number = sky.getVar("intVar");
sky.lprint(number);
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: True Global Variables

Postby Shando » 12 Dec 2013, 03:30

Hi again,

One more question.

How can I make a Lua Table a global variable?

Do I have to set each entry as a separate variable, or is there some other way?

Basically, I need a table with approx. 1000 entries, containing 6 different values (x coord, z coord, "lush", "fissure", player, building) for each playing board location (192 in total).

Thanks

Shando
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: True Global Variables

Postby SolarPortal » 12 Dec 2013, 11:32

we don't have this feature yet, but we will have a look at this for the next release. :)
The next release could be in the next week.
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: True Global Variables

Postby Shando » 12 Dec 2013, 12:37

The next release could be in the next week.


An early Christmas present :lol:
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: True Global Variables

Postby CreativeOcclusion » 24 Feb 2016, 19:02

SolarPortal wrote:To set and get global variables, use:

Code: Select all
-- set global variable
sky.setVar("varName", varValue);

-- get global variable
myNewVar = sky.getVar("varName");


These exist in the engine and are accessible from any script.
Hope this helps.

For more information, look in the Lua API "Sky" library.


I know this is an old post....I am not sure if this is still the way to pass global variables between scripts...

I am trying to pick up a key and set a variable called redKey = 1...I am using your code above to set the variable in my redkey script...

I have another script at a gate...gate script....this script is attached to a trigger....I am trying to sky.getVar in the onTriggerEnter()...I am printing the result to the console and it does pass the value of the var...BUT...It does not let me use the value to open the gate....If the value is 0...It should text "Gate is locked...Need red key!"...If value is 1 then gate should open....The code should work...I can change the value directly in the gate script and everything works like it should...

In the console I lprint the value when entering the trigger and it prints the correct value...Like if I have the key it prints 1 and if I don't have the key it prints 0....When 0 it does not text the door locked and when 1 it doesn't open the door...

This has me confused because if it is passing the global var, then it should work...Any guidance would be appreciated...I can post the code from both scripts if needed...Like I said though, the code is working when I just make redKey = 1 or 0 at the top of the script...Thanks
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas

Re: True Global Variables

Postby SolarPortal » 24 Feb 2016, 19:12

The getVar returns as a string in which you need a number to be sent through. Try surrounding the value passed into the 1 / 0 thing with

Code: Select all
tonumber(strReturnVal);


There is also the callFn and PublicFn in lua scripts. Check the forums for some examples :)
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: True Global Variables

Postby StarFire » 24 Feb 2016, 20:17

When I get a chance I will set up a quick test, I am sure its working, or should be :?
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: True Global Variables

Postby CreativeOcclusion » 24 Feb 2016, 21:14

StarFire wrote:When I get a chance I will set up a quick test, I am sure its working, or should be :?

Thanks...I still can't get it working...I will try the callPublicfn tonight when I get off...
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas

Re: True Global Variables

Postby StarFire » 24 Feb 2016, 21:47

In a new scene try putting the following in a scene script.

Code: Select all
function onInit(objID)
   val = 123;
   sky.setVar("test",val);
   result = sky.getVar("test");
   sky.print("Scene Result = "..result)
end



This works for me.

Then you could try this in a scene script:
Code: Select all
function onInit(objID)
   val = 123;
   sky.setVar("test",val);
end

and add this to a micro scripted object:
Code: Select all
function onInit(objID)
   result = sky.getVar("test");
   sky.print("MS Result = "..result)
end


let me know if this works for you. Then we can look further into it for you :D
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: True Global Variables

Postby CreativeOcclusion » 24 Feb 2016, 22:14

let me know if this works for you.

I am trying to do this between 2 microscripts....But in the second example I added an if statement...To see if the variable really equals 123...It does not work...I added this in the microscript attached to a cube....

Code: Select all
function onInit(objID)
   result = sky.getVar("test");
   sky.print("MS Result = "..result)
   
   if (result == 123) then
    sky.print("MS Really Does = "..result)
   end
end


This should give you an idea of what I am trying to achieve....
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas

Re: True Global Variables

Postby StarFire » 24 Feb 2016, 22:44

You may be facing an initialising ordering issue. Each entity gets initialised onInit() in a sequential loop. At each step the system processes that entity and all of its actions. If this entitie's script references something in a script that has not yet been initialized, due to the parent entity being further along the sequence.

To get around this condition we use the postInit() event. This is only called once all of the entities have had their onInit() called.

Give this a try as it should fix your problem.
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: True Global Variables

Postby StarFire » 24 Feb 2016, 22:48

On cube 1:
Code: Select all
function onInit(objID)
   val = 123;
   sky.setVar("test",val);
end


In cube 2 use the postInit() event function:
Code: Select all
function postInit(objID)
   result = sky.getVar("test");
   sky.print("MS Result = "..result)
   
   if (result == tostring(123)) then
    sky.print("MS Really Does = "..result)
   end
end


We use the quotes as the value is returned as a string from the global var or you can surround a script value with tostring(myValue) for the condition to work. :D
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: True Global Variables

Postby CreativeOcclusion » 25 Feb 2016, 09:26

Thank you so much for your help...This works perfectly... :D :D :D
I have been fighting with this for four days now...So close yet so far away...Now thanks to you it works...I can do a lot more now that I can pass the variables between scripts :idea: ....Thanks
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas

Re: True Global Variables

Postby Ryder » 25 Feb 2016, 09:39

CreativeOcclusion wrote:Thank you so much for your help...This works perfectly... :D :D :D
I have been fighting with this for four days now...So close yet so far away...Now thanks to you it works...I can do a lot more now that I can pass the variables between scripts :idea: ....Thanks


Oh wow... 4 days on that... you, sir, are a man of great character. I'd have gone through several stages of grieving... depression, angst, denial...

One of the things I take for granted, and I shouldn't, is when developing something insane in Microsoft Excel (and I do truly insane things in it... you'd not believe it)... and that is the truly MASSIVE user base, and instant google finds of 30 ways to solve any problem.

But it's lonely out here in the wild west
(in the distance, a tumbleweed rolls silently by)

-Ryder-
Last edited by Ryder on 26 Feb 2016, 05:01, edited 1 time in total.
"A scale model is never done. You just stop working on it."
User avatar
Ryder
Skyline Apprentice
 
Posts: 35
Joined: 21 Feb 2016, 20:35
Skill: 3D Modeller
Skill: Scripter
Skill: Animator
Skill: Great creative

Re: True Global Variables

Postby CreativeOcclusion » 25 Feb 2016, 09:49

But it's lonely out here in the wild west
(in the distance, at tumbleweed rolls silently by)


One thing we have here is a great dev team that go way out of their way to help us out...That's what keeps me here...I am not so sure if it's great character that I have...I just like to solve things myself until I get so pissed off that I have to ask for help before I go crazy :lol:
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas

Re: True Global Variables

Postby SolarPortal » 25 Feb 2016, 12:43

:D Yes, one day skyline will return 30 answers to the problems on google rather than "does this engine even exist???" lol :P We have collected a great deal of tumbleweed :lol:

Great you got it working @creativeOcclusion :)
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: True Global Variables

Postby StarFire » 25 Feb 2016, 15:28

So pleased its working for you now, Phew lol :D

Ah going crazy is part of the development pipeline ;)
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 6 guests

cron