Jump to content

Using Variable from one Script in another Script


Husarenkiller
 Share

Recommended Posts

Hello everyone :) I'm completly new to Leadwerks and Lua, but I've tried out a few things. At the moment i'm trying to programm tic tac toe. But therefor I designed the field and placed 9 pivots to mark the place where the signs should get placed. My first Problem is that I don't have a clue how I could get the Position of the Waypoint to the Script with my 'player' / Camera. With the following Code i got this Error Message: ' attempt to index global 'Waypoint1' (a nil value)'

And Secondly I've tried a few things how i could produce the signs i designed at the Position of the Waypoints but i am only able to produce normal boxes/cylinders.

 

This is my Code yet:

TicTacToeCam:

function Script:Start()
--Create a camera
self.camera = Camera:Create()
--Update the camera
self:UpdateCamera()
end
--Locating Field
function Script:Field(num)
-----------------------------------------1. Reihe -----------------------
   if num == 1 then
 Feld1 = Waypoint1.Script:MyPosition()
 return Feld1
end
if num == 2 then
 Feld2 = GetPosition(Waypoint2)
 return Feld2
end
if num == 3 then
 Feld3 = GetPosition(Waypoint3)
 return Feld3
end
------------------------------------------2.Reihe -----------------------
if num == 4 then
 Feld4 = GetPosition(Waypoint4)
 return Feld4
end
if num == 5 then
 Feld5 = GetPosition(Waypoint5)
 return Feld5
end
if num == 6 then
 Feld6 = GetPosition(Waypoint6)
 return Feld6
end
------------------------------------------3.Reihe -----------------------
if num == 7 then
 Feld7 = GetPosition(Waypoint7)
 return Feld7
end
if num == 8 then
 Feld8 = GetPosition(Waypoint8)
 return Feld8
end
if num == 9 then
 Feld9 = GetPosition(Waypoint9)
 return Feld9
end
return 
end
--Adjust the camera orientation relative to the field
function Script:UpdateCamera()
self.camera:SetRotation(90,0,0)
self.camera:SetPosition(self.entity:GetPosition())
self.camera:Move(0,0,6)
end
function Script:UpdateWorld()
--Update the camera each frame
self:UpdateCamera()
end
function Script:UpdatePhysics()
--Start Game bei 0 Durchgängen
Durchgang = 0
print('Hey')
if Durchgang == 0 then
self:Gameplay()
Durchgang = Durchgang + 1
print('Durchgang ist: '..Durchgang)
end
end
function Script:FeldSetzen(zug)
FeldSetzen = nil
--Placing Object for Player 1
if (zug == 1) or (zug == 3) or (zug == 5) or (zug == 7) or (zug == 9) then
 if window:MouseHit(Key.LButton) then 
   entity = Model:Box(1,0.5,1)
   entity:SetColor(0,128,255)
   entity:SetPosition(self:Field(1))
 end
elseif (zug == 2) or (zug == 4) or (zug == 6) or (zug == 8) then
 --Placing Object for Player 2
 print('in Schleife 2')
 if window:MouseHit(Key.LButton) then
   entity = Model:Cylinder()
   entity:SetColor(128,26,0)
   entity:SetPosition(3.9,1,3.8)
 end
end
end
function Script:Check()
--Check ob Feld frei ist
end
----------------------------Programm------------------------------------
function Script:Gameplay()
print('Bin hier')
zug = 1
if (1 <= zug) and (zug <= 9) then
 self:FeldSetzen(zug)
 print('Zug:'..zug)
 zug= zug + 1
end
end

and the Waypoint File:

function Script:MyPosition()
--Get own Position
Position = self.GetPosition()
return Position
end

 

And if these Problems are fixed i could need some help to find a way to use the mouse position while using MouseButton to mark the place where the signs should be placed. Otherwise I've already got an idea how could solve this by just using numberkeys 1-9..

 

Many thanks in advance :) I hope my Mistakes aren't too stupid and the Code is implemented correctly :D

Link to comment
Share on other sites

I assume that Waypoint1 is the name of one of the entities which an X or an O can be placed. In order for TicTacToeCam to call functions on your Waypoint script you would need to do something like this:

 

At the top of TicTacToeCam add:

Script.Waypoint1 = nil --Entity

 

Next, find your TicTacToeCam entity, go to the script properties and from the Scene tab drag your Waypoint1 entity to the "Waypoint1" entry in the Script tab.

 

Finally change

Feld1 = Waypoint1.Script:MyPosition()

to(note "self." and lower case "s" in Script:

Feld1 = self.Waypoint1.script:MyPosition()

 

Check out this tutorial on script properties:

http://www.leadwerks.com/werkspace/page/tutorials/_/script-properties-r20

 

As far as finding the position in relation to the mouse cursor check out the Camera:Pick() function:

http://www.leadwerks.com/werkspace/page/api-reference/_/camera/camerapick-r199

Link to comment
Share on other sites

Ok thanks alot for your help :) Your first tip works well but i don't understand what should do here:

Next, find your TicTacToeCam entity, go to the script properties and from the Scene tab drag your Waypoint1 entity to the "Waypoint1" entry in the Script tab.

 

I will check the 2 tutorials out but before i can do that i have to fix a huge problem because now i always get an : Exception_Access_Violation Error. It seems to happen because of this error:

-Error: Script Connection source function "waypoint6" not found

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...