Jump to content
  • 0
Tindrone

'=' expected near 'function'

Question

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

 

 

Share this post


Link to post

14 answers to this question

Recommended Posts

  • 0

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: <=,   >=,   ~=,   ==

Share this post


Link to post
  • 0

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

Share this post


Link to post
  • 0

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

 

Share this post


Link to post
  • 0

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 

Share this post


Link to post
  • 0

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

 

Share this post


Link to post
  • 0

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.

Share this post


Link to post
  • 0
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

Share this post


Link to post
  • 0

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

Share this post


Link to post
  • 0

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.

Share this post


Link to post
  • 0

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.

Share this post


Link to post
  • 0
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?

Share this post


Link to post
  • 0

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.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   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.

×
×
  • Create New...