How to define gravity in source code? Created 6 months ago2023-10-03 17:20:07 UTC by Niborius Niborius

Created 6 months ago2023-10-03 17:20:07 UTC by Niborius Niborius

Posted 6 months ago2023-10-03 17:20:07 UTC Post #347911
Hi!

I'm working on a randomizer for Half-Life 1, and I was wondering where in the source code the gravity is defined? I know it's ultimately defined with a console command (sv_gravity), but is it possible to overwrite this in the source code with my own value instead? I already know how to do the randomizing part.

(Simply said, I am looking to replace something like Gravity = sv_gravity's value with Gravity = myvalue

Hope that makes sense. Thanks in advance!
Posted 6 months ago2023-10-03 18:14:14 UTC Post #347913
In the latest HL Updated SDK, some places in the game code use the gravity CVar for certain things:
User posted image
Other than that:
  • HUD_TempEntUpdate has a cl_gravity parameter,
  • pmove_t has a gravity member,
  • CWorld::Spawn sets the value of sv_gravity
Definitely do a keyword search for "gravity" across the entire codebase, you'll find all possible references to it there. For a randomiser, I believe it may be the best to do it in CWorld::Spawn if each level is essentially randomised.
Admer456 Admer456If it ain't broken, don't fox it!
Posted 6 months ago2023-10-08 09:26:43 UTC Post #347926
Hey thank you for the detailed response, and sorry for coming back to you so late!

I've tried a number of things but somehow can't get it to work. I have to add that I am not a professional (c++) programmer, my understanding is quite basic.
I think you are correct in that the best way is to do it in CWorld::Spawn as that is where the global sv_gravity value is assigned. However I am unsure how to, so to speak, change this value with an rng if it's in quotation marks (as it will literally paste my code as text as the value for sv_gravity). What would be the logical approach to do this?
Posted 6 months ago2023-10-09 16:59:33 UTC Post #347932
You can just use CVAR_SET_FLOAT:
// Gravity will vary +/- 400
#define GRAVITY_VARIATION 400

int myRandomGravity = 800 + rand() % GRAVITY_VARIATION;

CVAR_SET_FLOAT( "sv_gravity", myRandomGravity );
But of course, there are ways to convert numbers into strings (the values in quotation marks are called "strings").
Admer456 Admer456If it ain't broken, don't fox it!
Posted 6 months ago2023-10-09 23:00:02 UTC Post #347936
Thanks Admer! This worked!
You must be logged in to post a response.