Jump to content

Rick

Members
  • Posts

    7,936
  • Joined

  • Last visited

Everything posted by Rick

  1. So from what I know if you just make a variable and assign it a value it'll be global unless you put local in front of it. So in the fpscontroller.lua file (which seems to be the file that gets played when you run game from the editor) I make a variable and set it to true: runGame = true Then for the main game loop I do: while runGame do ... end Then I load the monster truck entity just to test this out. I just need an entity to have it's Update() method called each frame. I make a small change inside the Update() method of the monster truck: function Update(model) local model local entity for model,entity in pairs(entitytable) do entity:UpdateTires() end if KeyHit(KEY_ESCAPE) == 1 then runGame = false end end But when I press ESCAPE, the game doesn't end. What gives?
  2. Rick

    Coroutines

    Lua actually has threading as well. So they have both.
  3. Sorry, but I love the cutting off title it gives
  4. Rick

    Coroutines

    Not really. It continues when you tell it to continue not when the called function returns. You tell it to continue with a call to coroutine.resume() passing in the coroutine "handle". Coroutines also store all it's variable data the function had when you left it so when it continues it can call them back on the stack for you to use again once inside the coroutine function again.
  5. Rick

    Coroutines

    Yeah, it can get confusing when working with, but thankfully using it once a system is setup is straight forward. I'll explain: Lua has these things called coroutines. These allow you to stop execution of a function and continue it at a later time (that time is something you define). There are a few functions tied to them, namely: routine = coroutine.create(func) (ties a function to this coroutine that is returned) coroutine.yield() (jumps out of the coroutine function are in. when we resume it'll start back up after this statement) coroutine.resume(routine, args_to_function) (starts/resumes a coroutine function you defined) These are the building blocks for how coroutines work. If I want to be able to stop execution of a function I must create a coroutine for that function. function Test(name) end -- associates the coroutine (cr) to the function (Test) cr = coroutine.create(Test) When I have a coroutine named cr that controls the function execution. I can start the function with: coroutine.resume(cr, "Rick") I can also step out of the function with: coroutine.yield() So to show a complete small & simple example. You can run this script using lua.exe. function Test(name) print(name) coroutine.yield() -- This will jump out of the Test() function and continue to do the while loop below print("is cool") end cr = coroutine.create(Test) -- create a coroutine tied to the function Test coroutine.resume(cr, "Rick") -- This actually starts the function Test passing in "Rick" as the parameter -- will loop for 5 seconds i = os.time() + 5 while os.time() < i do -- This is here to show that it truly did leave the Test function. -- With this here you can't see that the Test function prints "Rick" so if you comment this print out -- you can see it prints "Rick" out on the console. print("Looping") end -- this will resume the Test function and you'll see it starts off where it left off, after the yield -- inside the Test function. At this point it really doesn't matter what value I send it as the 2nd parameter -- which is the parameter to the Test function, because it'll use whatever I sent it the first time. Which is -- kind of a bummer because I thought it would be pretty neat if I could pass different values on every resume -- but it's not a big deal really coroutine.resume(cr, "Rick")
  6. Rick

    The Day After

    As soon as I get my script sequence lua code working, there will be more of a functional reason to use lua.
  7. Rick

    Coroutines

    I'm currently working on coroutines in lua. This will help people setup scripting sequences for their game. The cool part of this will be that you will use it in the editor like any other entity. Drag it into your scene and now in your other scripts you'll have access to these functions. Here is an example usage of what you would do in lua. --- This defines the function that stores your script sequence function MyScript01(cr) -- cr is the coroutine to this function. The functions that pause execution of the script require it to be passed in MoveTo(cr, player, pivot1) -- This method will move the player entity to the pivot1 entity and only return when it has reached it's destination PlaySound(cr, "hello.wav") -- This method will return only after the hello.wav is finished playing PlayAnimation(cr, "animation") -- This method returns after the animation is complete Wait(cr, 2000) -- This method will simply wait for 2 seconds before returning. Time is in ms. end -- When you are ready to run your script function simply pass the function to RunScriptFunction() and it'll start playing out RunScriptFunction(MyScript01) This can give us more interactive games than seen in the past with LE. You could create a volume trigger that runs a script when it's touched by the player, that moves an NPC to a location. Have it start talking. Have it play an animation, etc all in sequence. Maybe you make a button in game that when the player presses the 'use' key it runs a script. The script could do all sorts of things. The main benefit of using coroutines like this is that the function where you define your script is actually "paused" until something restarts it again. This means that your game continues to run. That's where I come in. I'm defining the function that will pause and how they will start up again. I'm interested in hearing if people have other functions that would be useful to them that act like this. So far the functions I have are: Wait() MoveTo() PlayAnimation() PlaySound()
  8. I haven't read through some posts but after I sign up for this new forum do I need to wait to be able to see the programming forums? It says I don't have permissions.
  9. Rick

    Character Thingoids

    I've made and continue to work on 3 objects that you can drag into the editor and get player movement features in about 2 mins. I'll discuss them and their plans here. I prefix them with Pi because that's my sort of namespace so when you place these in your editor's path they won't have the same name as other objects you create/download. Pi is the start of my last name and I'm using it as a brand or sorts also. Character http://leadwerks.com/werkspace/index.php?app=downloads&showfile=46 This is a generic character object. Instead of creating objects for all your characters and placing them in your scene, you just need this object. It creates a character controller and allows you to assign a model. So you can have 100 of these objects in your scene and they can all have different models. This object also accepts some messages for moving the character controller. Right now this is pretty basic but it will get advance and something that should cover most character movement needs. 3rd Person Camera http://leadwerks.com/werkspace/index.php?app=downloads&showfile=45 Drag this into your scene and give it a target (anything). Set some offsetting values and this will give you 3rd person camera controls on that target. The future of this is that it'll be able to handle any type of camera setting. 3rd perseon, first person, top/down etc just by setting some keys. Character Keyboard Controls http://leadwerks.com/werkspace/index.php?app=downloads&showfile=47 This will bring life to your Character object. It's meant to work out of the box with the Character object above, but it can work for any object you have as long as you receive some messages that the Keyboard control sends. This allows you to define which keys will move characters. The part I'm not the most fond of is that the editor uses very specific game scripts. The fps, vehicle, and flythrough. I'm not a fan of how this is setup myself. I think the game script should be very basic and that objects that you drag into your scene are what builds how the game works. So in order for the above 3 objects to work correctly they need the following game script to be running when you run the game. The reason for this is because of keyboard controls and mouse controls. The default game scripts al do something with the keyboard and mouse and if you used them with the above objects they would be fighting for the camera and keyboard controls. require("Scripts/constants/collision_const") require("Scripts/constants/engine_const") require("Scripts/LinkedList") require("Scripts/filesystem") require("Scripts/math/math") if fw==nil then --we are not in Editor RegisterAbstractPath("") Graphics(800,600) -- need a better way to get screen res fw=CreateFramework() scene=LoadScene("") -- need a way to get a scene to load scene:SetCollisionType(COLLISION_SCENE) TFilter(1) AFilter(4) end -- set globals SetGlobalString("mode", "GAME_MODE") SetGlobalString("quit", "false") FlushKeys() FlushMouse() --main function while (KeyHit(KEY_ESCAPE) == 0) and (GetGlobalString("quit") == "false") do fw:Update() fw:Render() Flip(0) end -- reset values SetGlobalString("mode", "DESIGN_MODE") SetGlobalString("quit", "false")
×
×
  • Create New...