Jump to content

'=' expected near 'function'


Tindrone
 Share

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 3 weeks later...

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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