Tindrone Posted June 28, 2019 Share Posted June 28, 2019 Hi, I'm new to Leadwerks and am attempting to make a point and click game as my first project, however when I use a function I always get the response Quote '=' expected near 'function' I also used the example custom function code in the documentation. Here is my code Quote function Script:Start() rotateC = 0 End function CameraRotate() self.entity:SetRotation(0,rotateC*90,0) End function Script:UpdateWorld() End Here is the example code that also returned the same error Quote function SayHello() print("Hello!") end SayHello() I would be grateful if anybody could help me figure this out. I am new to LUA and the syntax is very annoying EDIT: I've solve the issue by changing the script, but now I get Quote attempt to index global 'Script' (a nil value) here is my code now Quote function Script:Start() rotateC = 0 end function Script:CameraRotate() self.SetRotation(0,rotateC*90,0) end function Script:UpdateWorld() Script:CameraRotate() end Quote Link to comment Share on other sites More sharing options...
Daemoc Posted June 28, 2019 Share Posted June 28, 2019 I don't know much about the scripting language myself, but try "== 0" rather than "= 0". I had an issue like that, the == through me for a loop as well. These are the important ones to remember: <=, >=, ~=, == Quote Link to comment Share on other sites More sharing options...
Tindrone Posted June 28, 2019 Author Share Posted June 28, 2019 I might be wrong but isn't == used for comparisons? I meant to assign 0 to rotateC. I'm concerned more about why Script is considered a nil value Quote Link to comment Share on other sites More sharing options...
Daemoc Posted June 29, 2019 Share Posted June 29, 2019 My bad, I misunderstood what you were trying to do. I guess I just glossed over it and saw the error. Should that be?... function Script:Start() self.rotateC = 0 end function CameraRotate() self.entity:SetRotation(0,rotateC+90,0) End function Script:UpdateWorld() End Quote Link to comment Share on other sites More sharing options...
Tindrone Posted June 29, 2019 Author Share Posted June 29, 2019 Sorry, I am not that great at getting messages across. I suppose I should give you an idea of what I am trying to do. In my point and click game, two green arrows (represented by cones) are nested within my main camera. Clicking on the arrows changes the rotateC value to 1 and -1 respectively. I want the rotation to be set to multiply 90 by rotateC and would like for it to update constantly and thus want to call the function in UpdateWorld (in order to rotate the camera in 90 degree increments). Perhaps the way I'm going about it is completely out of whack, but I hope you can see what I'm trying to do Quote Link to comment Share on other sites More sharing options...
Daemoc Posted June 29, 2019 Share Posted June 29, 2019 No need to apologize. I was way off on what you were doing. I have my 3/4 view camera pretty much doing what you are trying to do. I can not say if it is in fact the right way to do it, but it works. You are welcome to take a look below. Script.rotation = 0 function Script:Start() end function Script:UpdateWorld() if window:KeyHit(Key.Q) then self.rotation = self.rotation +45 end if window:KeyHit(Key.E) then self.rotation = self.rotation -45 end end Quote Link to comment Share on other sites More sharing options...
Tindrone Posted June 29, 2019 Author Share Posted June 29, 2019 Thanks! I looked over your code and modified mine, my question now is how do I click on specific objects, Unity has the function OnMouseDown() which detects when a specific object (IE a key or other item) is clicked, I was wondering how to do this in Leadwerks. Quote Link to comment Share on other sites More sharing options...
Angelwolf Posted June 29, 2019 Share Posted June 29, 2019 9 hours ago, Tindrone said: Thanks! I looked over your code and modified mine, my question now is how do I click on specific objects, Unity has the function OnMouseDown() which detects when a specific object (IE a key or other item) is clicked, I was wondering how to do this in Leadwerks. Could be wrong here (haven't used it myself) but I believe for a left mouse button click it is: window:MouseDown(Key.LButton) There are more details on the mouse down syntax here Quote My Games: Nightmare Prism:::WolfTale:::Dungeon Creeper:::Despair of Ordinary Men:::Piano Simulator 2016:::House (W.I.P) Link to comment Share on other sites More sharing options...
Tindrone Posted June 30, 2019 Author Share Posted June 30, 2019 From testing it myself, it seems that MouseDown only registers when the mouse is pressed, meaning no matter where you click, you'll get the same result Quote Link to comment Share on other sites More sharing options...
Thirsty Panther Posted June 30, 2019 Share Posted June 30, 2019 You need to make a camera pick after you make your mouse down is registered. https://www.leadwerks.com/learn?page=API-Reference_Object_Entity_Camera_Pick I explain its use in a post I made for Operation Wolf remake. 1 1 Quote Link to comment Share on other sites More sharing options...
Tindrone Posted July 15, 2019 Author Share Posted July 15, 2019 my script now looks like this Quote function Script:Start() rotateC = 0 end function Script:UpdateWorld() self.entity:SetPosition(self.entity:GetPosition()) self.entity:SetRotation(0,rotateC,0) if window:MouseDown(1) then local window = Window:GetCurrent() local pickinfo = PickInfo() local mpos= window:GetMousePosition() if (self.camera:Pick(mpos.x,mpos.y,pickinfo,0,true,2)) then pentity= pickinfo.entity name= pentity:GetKeyValue("name") System:Print( "picked!!!!!") System:Print( mpos.x) System:Print( mpos.y) System:Print( name) if name == "ArrowR" then rotateC = rotateC + 90 end if name == "ArrowL" then rotateC = rotateC - 90 end end end end whenever I click on my arrows i get this Quote attempt to index field 'camera' (a nil value) my camera is named lowercase like in the script and is nested in my player entity. Quote Link to comment Share on other sites More sharing options...
havenphillip Posted July 15, 2019 Share Posted July 15, 2019 Possibly you need to change every "rotateC" to "self.rotateC" ? Quote Link to comment Share on other sites More sharing options...
Rick Posted July 16, 2019 Share Posted July 16, 2019 If this is your entire script then you can't just do self.camera like that. As the error is saying self.camera is nil. Just curious as to your thinking on why you think it would work as it can help explain why it doesn't better. Quote Link to comment Share on other sites More sharing options...
Tindrone Posted July 16, 2019 Author Share Posted July 16, 2019 14 hours ago, Rick said: If this is your entire script then you can't just do self.camera like that. As the error is saying self.camera is nil. Just curious as to your thinking on why you think it would work as it can help explain why it doesn't better. I believed it would work because from what i understand self.camera means I am selecting the object camera which is nested in my player pivot which the script is attached to. Is there a way to do that? Quote Link to comment Share on other sites More sharing options...
Rick Posted July 16, 2019 Share Posted July 16, 2019 So in the scene graph you have a camera object as a child of your player pivot? If that's the case then from your script that is attached to your player pivot you'd do something like this in the Start() function: self.camera = self.entity:GetChild(0) This assumes you only have 1 child to your player pivot and it's the camera. If you have more than one child I think you can use self.entity:FindChild("camera_name_here") Scripts don't automatically get variables like you're thinking because they are children or parents of things, but there are ways to get them with GetChild(), FindChild(), GetParent() calls. This isn't 100% ideal though. Ideally you'd create a script parameter and drag and drop the camera in the scene graph to that parameter. To do this at the top of this script add: Script.camera = nil --entity Then when you select your player entity in the scene graph you'll see the camera parameter show up. Drag and drop the camera to this slot and now you've made a link between the camera entity and this script camera variable. Now you can use self.camera in your script. You can do this with any entity btw. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.