Scripts / Code walk-through of whacky racers script

Author
Message
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 3rd Jul 2017 22:11 Edited at: 4th Jul 2017 22:52
In honour of my shiny new badge I've decided to walk through one of my recent scripts explaining how it works.

In order that this thread doesn't get too derailed can I ask that any questions or comments be put in a separate thread (i.e. first person to pipe up gets to make the new thread! ).

So for this first post I am attaching a (hopefully) fully working demo that people can play with and then in subsequent posts I will break down the scripting into manageable chunks explaining what each bit does as I go along.

To use the demo you should be able to just unpack it into your game guru files folder. When you load the fpm and run it use the W/S keys to accelerate/decelerate and the E/R keys to steer (I know that combination is bloody awkward but I wanted to keep the code simple). Oh and the left mouse to fire the hammer.

Not sure how long this will take me so if any MODs are reading this could you please make this a sticky, maybe once I've finished this could be transferred to somewhere more appropriate.

Edited to add: Ooops, first boob of the thread, had an error in the main script, fixed and zip file replaced.
Been there, done that, got all the T-Shirts!

Attachments

Login to view attachments
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 3rd Jul 2017 22:38
Right, on with the show. First a few words on what I won't be covering:

1) How quaternions actually work! To be frank I'm not sure I understand that bit fully myself, if anyone really wants to give themselves a bad headache there are numerous explanations on the web.
2) The math regarding 3d positioning, again there are plenty of sources that explain that bit. I might mention in passing some limitations or gotchas to watch out for but mainly I'll simply state what the math is achieving, or attempting to achieve, and leave it as the readers exercise to figure out the minutia.

So to start with I'll cover the two little scripts, I like to think of them as helper scripts which are simply there to enable the main script to identify which entities it has to deal with:

First the wheels, wr_wheel.lua, this script is attached to the wheel entity (obviously).


Next up is the weapon, wr_hammer.lua, you may be surprised to find how similar
this is to the wheel script:



See what I mean?

Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 3rd Jul 2017 22:53
Ok, a bit more meaty now, I won't go into the guts of the quaternion library but I will explain how it is used by covering in this post the parts of the main script that tell Lua to instantiate it, along with a few other parts that might look strange at first sight .

Script whacky_racer_walkthru.lua is attached to the vehicle chassis entity:


Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 3rd Jul 2017 23:19 Edited at: 3rd Jul 2017 23:26
Before I leap into the 'main' function, note that I have fixed a problem with the original download so if you have already downloaded it and it didn't work please try the new one.

Ok, so the main function in the whacky_racer_walkthru.lua script. I prefer my main function to be as simple as possible so I farm out as much functionality to sub functions as I can:


See how simple this all is?

Tomorrow I'll launch into the individual functions called by main and try to explain how they do what they do!

Phew.
Been there, done that, got all the T-Shirts!
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 3rd Jul 2017 23:41 Edited at: 3rd Jul 2017 23:41
Quote: "See how simple this all is? "


Nope, you lost me and line 1

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
warlock12
7
Years of Service
User Offline
Joined: 12th Sep 2016
Location: Argentina
Posted: 4th Jul 2017 00:02
Woow Its hard... or im fool
The game is a serious thing (El juego es una cosa seria)
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jul 2017 07:44
I find it helps to actually read the posts, particularly this bit:

"In order that this thread doesn't get too derailed can I ask that any questions or comments be put in a separate thread (i.e. first person to pipe up gets to make the new thread!)"

Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jul 2017 09:09 Edited at: 4th Jul 2017 23:11
Now I'll cover a few functions that we have previously seen being called by the 'helper' scripts:



Next the GotEnts function, the version I posted is missing the check for the hammer so here is what it is supposed to look like:


Attached the Lua script with bug fixes!
Been there, done that, got all the T-Shirts!

Attachments

Login to view attachments
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jul 2017 09:26 Edited at: 4th Jul 2017 09:28
Finally before I head off to work (yes I do actually have a day job ) here is a function that gets used a lot:



An example of use for this function would be that if I have an entity with 'forward' being in the +z direction (which seems to be the default 'standard' in GG as it is 'into' the screen in the initial state., but it is rotated in the game by Euler angles a, b, c. This function can tell us the new 'forward' direction vector vx, vy, vz by doing:

vx, vy, vz = Rotate3D( 0, 0, 1, a, b, c )

The first three parameters here are the unit vector for the z axis (i.e. our default 'forward') and the other 3 are the Euler angles (in radians!)

If we also have a value 'speed' which is defined as units per frame then we can move the entity in the forward direction with:

ResetPosition( e, current + vx * speed, currenty + vy * speed, currentz + vz * speed )

Phew, later I'll delve into the GetWheelHeights code.
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jul 2017 09:37
btw, make sure the setup.ini file in the Game Guru folder has 'vsync = 1' otherwise you may get some strange results!
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jul 2017 19:26 Edited at: 4th Jul 2017 19:31
Before diving into the code I'll introduce something else that you will see a lot of in my scripts:



Next up we have another list and a little helper function:

Been there, done that, got all the T-Shirts!

Attachments

Login to view attachments
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jul 2017 19:44 Edited at: 4th Jul 2017 19:49
Time for a big function, what this function does is work out the height of the terrain at the points under the four wheels and then uses that info to figure out how to orientate the vehicle in such a way that the four wheels will appear to be touching the ground.

The function takes the vehicle and the Eular angles representing it's current orientation above the terrain and returns three values, the y-axis value needed to place the model correctly and the pitch/roll angles to apply to it.

(btw since writing this routine I've thought of another way of doing this which should work on entities, i.e. roads and bridges. Also worth noting is that to add 'jumping' abilities to the vehicle we simply need to filter the return values such that we only 'drop' by a limited amount but always 'rise' immediately)



Next up is the code that positions the wheels.

Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jul 2017 20:14
Wheel Positioning.

Because the code required for each wheel is very similar this is split, first we have a small function:


And then the biggy which does all the hard work:



Short break then I'll continue, need some liquid refreshment!
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jul 2017 20:48
Bud in one hand, mouse in the other .. hold on what am I typing with???

Anywho, the rest of this is gonna be a lot of deja-vu but onward and upward.

Boring bit now, actually positioning the vehicle entity:



Slightly more interesting is positioning the hammer:



Next up will be the player controls bit.
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jul 2017 20:59
This code is a bit rough and ready, it was added to make the demo a bit more interactive but really needs to be rewritten to use mouse controls or joystick/controller or at least more sensible keys!



Next up moving the vehicle, mush simpler than you would imagine
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jul 2017 21:07
This is how you move something simply in GG, note that there is no complicated correction for frame rates or complicated physics involved, in fact as this is a demo the vehicle entity has physics 'off' and as long as your PC can handle 60FPS this method is fine with vsync 'on'



Now wasn't that rather simpler than you expected!

Only one bit too go now, attaching the player to mimic a sort of rudimentary third person view of things.
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jul 2017 21:25
Final part of the walk-through, taking the player along for the ride.



A few points here about this bit:
1) I haven't used the new camera commands (I haven't even really looked at them yet)
2) Of all the parts of the script this bit could do with a lot more work, in particular the camera needs to be more mobile and to move smoothly but with a slight lag when turning so that a slight side view of the vehicle is seen.


So that's it for the walk-through, comments/suggestions/questions are now welcome.
Been there, done that, got all the T-Shirts!
PM
GraPhiX
Forum Support
19
Years of Service
User Offline
Joined: 15th Feb 2005
Playing:
Posted: 5th Jul 2017 20:44 Edited at: 5th Jul 2017 20:45
errrr errrr yeah I get it

Seriously though this is excellent whether or not I understand what you have done I can see the logic
Welcome to the real world!
Windows 10 Pro x64 - Core i7-2600K @3.40GHz - 32.0GB RAM - GeForce GTX 950 2GB - 4x500GB SSD Striped
Blacknyt46
8
Years of Service
User Offline
Joined: 29th Feb 2016
Location:
Posted: 9th Jul 2017 04:32
Amen.M great scripting and a very well earned badge! I just read your walk-through. Now my brain hurts. But it does make sense. It's so confusing when I can't ask a bunch of questions as I'm reading through it. Anyway, I made a couple race tracks. I managed to get 2 buggies racing each other. Using wave points. But when I add a 3rd drive able buggy it just sits there with the following error "need more tire entity's?" Or something along them lines? I can drive the green buggy with the hammer alone. But as soon as I add a 2nd wave point driven buggy. Some of the red and green tires from the other buggies get switched around. I'll have 2 green tires in the front. And 2 reds on the back. 2 more laying on the track. And the 2nd buggy does nothing but displays that error message. With 2 missing tires also. So it seems that if I'm using a drive able buggy, I can't add any more buggies? I don't know what I'm doing wrong? I read through both threads. I could not find anything. Do I need to adjust one of the scripts to add more buggies? I'm using honkeyboys buggies and your scripts. Thanks
Jim C
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 9th Jul 2017 08:30 Edited at: 9th Jul 2017 08:30
The script in this thread was intentionally simplified for the walk-thru so I could show the basics of how it was done, in order to do what you are trying to do will require a few modifications.

The simplest way would be to have one script for each colour of buggy and wheel, a better way would be to use the name of the entity within the scripts to match buggy with wheel. I did start adding this but got sidetracked a bit , I'll take a look at it later today and see if I can complete it.

In order to make some sort of game out of these scripts still needs a lot of work, collision detection needs to be added, the other weapons need to be added etc.
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 9th Jul 2017 13:59
Here you go as a starting point:




Relies on the first 6 characters of the wheel 'name' being the same as the first 6 characters or the buggy 'name'.

Also by default the car with the hammer is the player controlled car. Haven't tested it with waypoints but it should work, although that part of the code could do with a lot of improvements!

Some of the wheels are the wrong way round btw, I've added the .x file for the right-way-round wheel, just copy it into the buggy directory and rename it.
Been there, done that, got all the T-Shirts!

Attachments

Login to view attachments
PM
Blacknyt46
8
Years of Service
User Offline
Joined: 29th Feb 2016
Location:
Posted: 11th Jul 2017 06:43
Thanks AmenMoses! Your scripting skills are Awesome. I'm going to play around with it for now. I hope I can get it to work now? Thanks again!
Jim C

Login to post a reply

Server time is: 2024-04-18 16:30:39
Your offset time is: 2024-04-18 16:30:39