Jump to content

Lua/Struggles, and Setinput "LeadWerks


Sweetgebus
 Share

Go to solution Solved by CJO Games,

Recommended Posts

Let me preface by saying that I have no experience in coding, aside from basics. As I am sure everyone has been at one point, I don't even know how to learn it. Is the docs provided in Leadwerks enough for the non programmer to learn? Do I need to learn Lua outside of leadwerks in order to learn to script within it?  I ask this loosely as I know that knowledge is king.  If I base my learning purely off the script documentation provided, is that enough to make a game? 

Example of my current headache regarding set input.

 

function Script:Start()
    self.camera = Camera:Create()
    self:UpdateCamera()
    player = Pivot:Create()
end

function Script:UpdatePhysics()
    local jump = ((window:KeyDown(Key.Space) and 1 or 0))
    player:SetInput(0,jump)
end


function Script:UpdateCamera()
    self.camera:SetRotation(20,90,0)
    self.camera:SetPosition(self.entity:GetPosition())
    self.camera:Move(0,0,-3)
end


function Script:UpdateWorld()
    self:UpdateCamera()
end

image.png.ba08514192c6834d3755747194603397.png

Looking at the the documentation, the example code for SetInput shows the player, world, and controls being created via script, rather than using a model and the editor to create the world. I struggle with understanding how to implement it to work with created models.  As you guys would probably be able to understand why, the above code is incorrect. I'm sure its simple, but even the most simple task is a nightmare for me. I've wanted to learn to script for over a decade and have made numerous attempts to make sense of it all. The internet is flooded with resources for learning, but that even seems to make it even more overwhelming. I once had a friend who was a coder and for a short time he was great at helping me with trying to learn, but unfortunately he passed away. 

I appreciate in advance for your responses. 

 

Link to comment
Share on other sites

I think the best way to answer this question is to ask another - what is it you want your created model to do? Assuming you want to create a player character(?) the first thing to do is place it in the editor (graphically) and then attach a player script to it using the script tab in the editor (you can see it in your post above) - I think you are trying to set up a player character programmatically, which is fine but not necessary; indeed, the graphical option (drag and drop) is an easier start and gives you the chance to get a working prototype up and running. I have found that learning my way around code comes easier to me by looking at and adapting ready built scripts - seeing how they work and then understanding that feels like a more natural approach - but of course this all comes don to personal preference.

In terms of what I think you are trying to do above you might start with the included FPS script and adapt it for your needs - as an example - here is my own adapted FPS script which includes a number of commented sections where I have tried to understand what each part is doing and also has some other changes that I have found useful/necessary for my own projects.

 

I've also attached a script I'm currently working on which is designed with a space exploration game in mind and which I have adapted from the spectator script included with Leadwerks.

 

Hopefully these will be helpful to you. In terms of LUA in general, Leadwerks is more of an implementation of it than its purest form so although its useful to look up other sources it will have differences too. I have actually found that AppGameKit Studio has been useful because although it is a BASIC language its structure and syntax are similar to the way LUA in Leadwerks works. Also useful are the Skyline Game Engine  which also uses LUA but in a different form and Game Guru Max which has its own quirky implementation of LUA.

FPSPlayer.lua BridgeCam.lua

  • Like 1
Link to comment
Share on other sites

36 minutes ago, CJO Games said:

I think the best way to answer this question is to ask another - what is it you want your created model to do? Assuming you want to create a player character(?) the first thing to do is place it in the editor (graphically) and then attach a player script to it using the script tab in the editor (you can see it in your post above) - I think you are trying to set up a player character programmatically, which is fine but not necessary; indeed, the graphical option (drag and drop) is an easier start and gives you the chance to get a working prototype up and running. I have found that learning my way around code comes easier to me by looking at and adapting ready built scripts - seeing how they work and then understanding that feels like a more natural approach - but of course this all comes don to personal preference.

In terms of what I think you are trying to do above you might start with the included FPS script and adapt it for your needs - as an example - here is my own adapted FPS script which includes a number of commented sections where I have tried to understand what each part is doing and also has some other changes that I have found useful/necessary for my own projects.

I've also attached a script I'm currently working on which is designed with a space exploration game in mind and which I have adapted from the spectator script included with Leadwerks.

Hopefully these will be helpful to you. In terms of LUA in general, Leadwerks is more of an implementation of it than its purest form so although its useful to look up other sources it will have differences too. I have actually found that AppGameKit Studio has been useful because although it is a BASIC language its structure and syntax are similar to the way LUA in Leadwerks works. Also useful are the Skyline Game Engine  which also uses LUA but in a different form and Game Guru Max which has its own quirky implementation of LUA.

FPSPlayer.lua 33.94 kB · 1 download BridgeCam.lua 9.89 kB · 0 downloads

I've studied the stock fps script and attempted to break it down. In my code snippet I'm fighting just to get my character to jump. I figured I would make a simple platformer where my character, or cube in the case has a constant move speed. Add in gaps and such for you to have to jump over. And as I learn I could add things like speed boosts and such. my script is attached to my character cube.  

Link to comment
Share on other sites

You need more parameters for SetInput:
https://www.leadwerks.com/learn?page=API-Reference_Object_Entity_SetInput

Syntax

  • SetInput(number angle, number move, number strafe=0, number jump=0, bool crouch = false, float maxaccel = 1, float maxdecel = 0.5, bool detailed = false, float maxrotationspeed=5.0)

Parameters

  • angle: the angle the character faces.
  • move: the forward movement of the character.
  • strafe: the horizontal movement of the character.
  • jump: the jumping force to apply.
  • crouch: set to true for crouching mode (included for future development)
  • maxacceleration: the maximum acceleration the character may use to speed up.
  • maxdecelleration: the maximum acceleration the character may use to slow down.
  • detailed: set this to true for characters that have a camera mounted on them. This will provide more accurate physics, but will also be more expensive to calculate.
  • maxrotationspeed: this is the maximum speed the entity can turn, in degrees per physics step.

self.entity:SetInput(0, 0, 0, 10) should make you jump.

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

4 minutes ago, Josh said:

You need more parameters for SetInput:
https://www.leadwerks.com/learn?page=API-Reference_Object_Entity_SetInput

Syntax

  • SetInput(number angle, number move, number strafe=0, number jump=0, bool crouch = false, float maxaccel = 1, float maxdecel = 0.5, bool detailed = false, float maxrotationspeed=5.0)

Parameters

  • angle: the angle the character faces.
  • move: the forward movement of the character.
  • strafe: the horizontal movement of the character.
  • jump: the jumping force to apply.
  • crouch: set to true for crouching mode (included for future development)
  • maxacceleration: the maximum acceleration the character may use to speed up.
  • maxdecelleration: the maximum acceleration the character may use to slow down.
  • detailed: set this to true for characters that have a camera mounted on them. This will provide more accurate physics, but will also be more expensive to calculate.
  • maxrotationspeed: this is the maximum speed the entity can turn, in degrees per physics step.

I'm a little confused on that part. In the doc it just shows angle,move and strafe used.

player:SetInput(0,move,strafe)

 

 

Link to comment
Share on other sites

1 hour ago, Sweetgebus said:

I'm a little confused on that part. In the doc it just shows angle,move and strafe used.

Many arguments are optional and will supply a default value if you do not use them. The jump parameter is optional. ;)

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

3 hours ago, CJO Games said:

Have a look at the attached fps script - it's a working example of how jump is integrated - it seems counter-intuitive to rewrite what is already available and what is already working...

Self punishment of learning is all. I took your advice though and after a couple of hours of studying and removing functions that were needed for my idea, I got it working.

  • Like 2
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...