Jump to content

Recommended Posts

On another note, GetModelHash doesn't always return a valid model hash. It gives me negative value for the Banshee, for example. And for most of the DLC bikes.

MODEL_BANSHEE = 0xC1E908D2

= 3253274834

= -1041692462

 

They are all the same... just different representations of the same thing in memory.

Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058859585
Share on other sites

Could anyone provide a good example or explanation on how to properly use RunTick() and states please?

What would be a good way to do or check something at all times, without performance penalty? Is it ok to do something like GetClosestCar() in the main RunTick() loop?

Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058860339
Share on other sites

This may sound confusing, but I hope its not...

 

What essentially is going on here is that there are multiple different script threads running in a single game operating system thread (so really its Single Threaded). The game uses something called co-operative multithreading to allow multiple scripts to run at once... which means one thread gives up control when its waiting on a resource so that other threads/the game can do its work too.

 

In the game script code... they normally would have...

 

 

if (KeyPressed(a)){ RequestModel(x); while (!HasModelLoaded(x)) {    Wait(0);    SomeOtherInstruction(); } CreateSomethingFromModel(x);}

 

 

When the game executes this script code... when it comes a Wait(), it saves the state of the script and goes and processes other scripts and other game code (like actually loading the requested model). When it gets back to this script, it'll continue running the instruction after the Wait which is SomeOtherInstruction();

 

Unfortunately, in the C++ hook code, its too hard to save the state of the code... so we can't use Wait() and the code above won't work! The general rule to use here is that you do not run any infinite loops that wait on resources... instead you set a flag or change the state (so that you know you requested a model, etc) and return back to the game. The next time RunTick() executes we'll use the flag or the state to identify what to do.

 

So for the code above... we break it apart into something like:

 

 

if (KeyPressed(a)){ RequestModel(x); modelRequested = true;}if (modelRequested){ if (!HasModelLoaded(x)) {    SomeOtherInstruction(); } else {    CreateSomethingFromModel(x);    modelRequested = false; }}

 

 

And the infinite wait loop is gone... and the game can process the actual loading of the model, and other threads are not blocked while you are waiting.

 

A state machine is just a fancy implementation of this... we just change the state to let the program know where the next execution should start instead of setting a flag.

 

You only need to do this when you have to wait for something to load/process/complete... this is usually the case for functions like RequestModel, RequestAnims, etc. Some other examples where you should be using states/flags to process the next section of code:

 

 

while(!IsScreenFadingOut()) {  Wait(0);}MoreCode();

 

 

 

while(TimerA() < 500) {  Wait(0);}MoreCode();

 

 

99% of the functions would not require you to do this. To use GetClosestCar() you do NOT need to break it apart into a new state and can just call it normally in a linear fashion. You can check anything, anywhere... just as long as you make sure you don't put a non-deterministic loop around it smile.gif

Edited by aru
Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058860804
Share on other sites

I'm having fun with this, and have quite a few natives that I'm using in a single asi.

 

Now I do have a question about something I cannot seem to be getting right.

 

Simple example in LUA,

 

I would declare

 

GOD_MODE = 0

 

then in the main function:

 

if (IsKeyPressed(97) == 1) then

GodMode()

end

 

function GodMode()

 

if (GOD_MODE == 0) then

PushInt(PLAYER_INDEX)

PushInt(1)

CallNative("SET_PLAYER_INVINCIBLE")

GOD_MODE = 1

else

PushInt(PLAYER_INDEX)

PushInt(0)

CallNative("SET_PLAYER_INVINCIBLE")

GOD_MODE = 0

end

end

 

Now, I am not able to get this simple thing working in C++, press one key, and depending on the value of GOD_MODE, act accordingly.

 

Any help is appriciated.

 

 

 

 

Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058860954
Share on other sites

 

I'm having fun with this, and have quite a few natives that I'm using in a single asi.

 

Now I do have a question about something I cannot seem to be getting right.

 

Simple example in LUA,

 

I would declare

 

GOD_MODE = 0

 

then in the main function:

 

if (IsKeyPressed(97) == 1) then

     GodMode()

     end

 

function GodMode()

 

if (GOD_MODE == 0) then

  PushInt(PLAYER_INDEX)

  PushInt(1)

  CallNative("SET_PLAYER_INVINCIBLE")

  GOD_MODE = 1

else

  PushInt(PLAYER_INDEX)

  PushInt(0)

  CallNative("SET_PLAYER_INVINCIBLE")

  GOD_MODE = 0

  end

end

 

Now, I am not able to get this simple thing working in C++, press one key, and depending on the value of GOD_MODE, act accordingly.

 

Any help is appriciated.

So you would declare...

 

 

bool GOD_MODE = false;

 

 

Then in the main function you would have:

 

 

if ((GetAsyncKeyState('A') & 1) != 0){  GodMode();}

 

 

Then outside the main function you would declare:

 

 

void GodMode(){    if (GOD_MODE == false)    {        SetPlayerInvincible(PLAYER_INDEX, true);        GOD_MODE = true;    }    else    {        SetPlayerInvincible(PLAYER_INDEX, false);        GOD_MODE = false;    }}

 

 

Then in scripting.h you would add (since SetPlayerInvincible is not defined in your copy of scripting.h):

 

 

static void SetPlayerInvincible(u32 playerIndex, b8 invincible) { NativeInvoke::Invoke< ScriptVoid >("SET_PLAYER_INVINCIBLE", playerIndex, invincible); }

 

 

And viola, you're done!

 

Ofcourse.. if you want to be an awesome programmer, you would do this instead:

 

 

void GodMode(){    SetPlayerInvincible(PLAYER_INDEX, !GOD_MODE);    GOD_MODE = !GOD_MODE;}

 

Edited by aru
Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058861085
Share on other sites

 

if ((GetAsyncKeyState('A') & 1) != 0){  GodMode();}

 

 

 

That was the only thing that was missing, I don't know how to declare a new function, I did declare the native in scripting.h

and was using:

 

else if ((GetAsyncKeyState(VK_MENU)&&(GetAsyncKeyState(VK_NUMPAD1) & 0x8000) != 0))

{

 

u32 playerIndex = ConvertIntToPlayerIndex(GetPlayerId());

SetPlayerInvincible(playerIndex, 1);

}

 

else if ((GetAsyncKeyState(VK_RCONTROL)&&(GetAsyncKeyState(VK_NUMPAD1) & 0x8000) != 0))

{

 

u32 playerIndex = ConvertIntToPlayerIndex(GetPlayerId());

SetPlayerInvincible(playerIndex, 0);

 

I feel stupid now smile.gif

 

(alltough it did work, but not with a simple one key keypress. )

 

Thank you very much smile.gif

Edited by sjaak327
Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058861499
Share on other sites

@Sacky... If you mean operating system thread, then yes... this is actually a good thing because the game uses TLS for data allocations. Just try running CreateCar from a seperate OS thread... it won't be pretty. If you mean game script thread, then no... you can always set the active script to another script and run a command that will attach the created resource to that thread.

 

@dingbat2000... I have no intentions of getting it to work under MP. Although I can appreciate the usefulness of it for some non-interfering mods, I really rather not be the root cause for a cheating epidemic. However, that being said... if a new game is loaded, it should rehook.... so that's a bug that I will look at smile.gif.

Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058861645
Share on other sites

Asking,

how to build "equal" or =?

in this?

 

(&p4 = x + p1 );

(&p5 = y + p2 );

(&p6 = z );

 

i made something with this but deadlier/ sucide

 

Got This:

1>CustomThread.cpp

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(114) : error C2440: '=' : cannot convert from 'f32' to 'Scripting::ScriptAny *'

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(115) : error C2440: '=' : cannot convert from 'f32' to 'Scripting::ScriptAny *'

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(116) : error C2440: '=' : cannot convert from 'f32' to 'Scripting::ScriptAny *'

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(118) : warning C4244: 'argument' : conversion from 'Scripting::ScriptAny' to 'f32', possible loss of data

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(118) : warning C4244: 'argument' : conversion from 'Scripting::ScriptAny' to 'f32', possible loss of data

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(118) : warning C4244: 'argument' : conversion from 'Scripting::ScriptAny' to 'f32', possible loss of data

Edited by aceship
Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058861809
Share on other sites

Still error

1>CustomThread.cpp

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(112) : error C2440: '=' : cannot convert from 'f32' to 'f32 *'

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(113) : error C2440: '=' : cannot convert from 'f32' to 'f32 *'

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(114) : error C2440: '=' : cannot convert from 'f32' to 'f32 *'

 

My Build

 

Char c;

  f32 x,y,z, x1, y1, z1;

  f32 p1,p2,p3;

 

 

  u32 playerIndex = ConvertIntToPlayerIndex(GetPlayerId());

  GetPlayerChar(playerIndex, &c);

 

  GetCharCoordinates(c, &x, &y, &z);

  GetCharVelocity(c, &x1, &y1, &z1);

 

  (&p1 = x + x1 );

  (&p2 = y + y1 );

  (&p3 = z );

 

Edit all: New One

Edited by aceship
Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058861871
Share on other sites

 

@Sacky... If you mean operating system thread, then yes... this is actually a good thing because the game uses TLS for data allocations. Just try running CreateCar from a seperate OS thread... it won't be pretty. If you mean game script thread, then no... you can always set the active script to another script and run a command that will attach the created resource to that thread.

 

Funnily enough I've tried running CreateCar from the main OS Thread, and sure enough it crashes. Although the game uses TLS for data allocations, isn't there a way to execute a chunk of code in that thread 'context' (that would allow the code to see the TLS allocations without actually being in the thread itself)? The reason I ask is I find it a bit restrictive that it's only possible to execute natives within the thread tick, for example let's say I hook the keyboard with SetWindowsHookEx in another DLL, and I want to inject natives off that callback? I've tried different variations of Suspend/Resume thread, but nothing's really working for me, any ideas?

Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058861880
Share on other sites

Is there some way to check if a vehicle exists(not model)? I'm asking this, because of my Car Spawner. I added the DLC bikes to the spawn list, but if someone without the DLC mod spawns any of them, it crashes.

 

I *think* checking for the models won't help, because HasModelLoaded returns true, since the model files are there, but not in the default.ide.

Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058862052
Share on other sites

@dingbat2000... I have no intentions of getting it to work under MP. Although I can appreciate the usefulness of it for some non-interfering mods, I really rather not be the root cause for a cheating epidemic. However, that being said... if a new game is loaded, it should rehook.... so that's a bug that I will look at smile.gif.

haha. its more to do with it not functioning after loading a savegame, or entering then exiting the video editor etc... but I can see your point about MP

Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058862077
Share on other sites

 

Still error
1>CustomThread.cpp

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(112) : error C2440: '=' : cannot convert from 'f32' to 'f32 *'

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(113) : error C2440: '=' : cannot convert from 'f32' to 'f32 *'

1>e:\program files\rockstar games\grand theft auto iv\customthread.cpp(114) : error C2440: '=' : cannot convert from 'f32' to 'f32 *'

 

My Build

 

Char c;

  f32 x,y,z, x1, y1, z1;

  f32 p1,p2,p3;

 

 

  u32 playerIndex = ConvertIntToPlayerIndex(GetPlayerId());

  GetPlayerChar(playerIndex, &c);

 

  GetCharCoordinates(c, &x, &y, &z);

  GetCharVelocity(c, &x1, &y1, &z1);

 

  (&p1 = x + x1 );

  (&p2 = y + y1 );

  (&p3 = z );

 

Edit all: New One

try:

 

Char c; f32 x,y,z, x1, y1, z1; f32 p1,p2,p3;  u32 playerIndex = ConvertIntToPlayerIndex(GetPlayerId()); GetPlayerChar(playerIndex, &c); GetCharCoordinates(c, &x, &y, &z); GetCharVelocity(c, &x1, &y1, &z1); (p1 = x + x1 ); (p2 = y + y1 ); (p3 = z );

 

 

You don't need the address of a variable to use it as a left hand argument in an assignment.

 

(Sorry, for misunderstanding your previous post tounge.gif)

Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058862125
Share on other sites

Im still wondering, how to teleport a player to a coord? Is it possible with a function or does it need memory editing?

You can easily do that with this hook.

 

Look at the LUA main thread (page 36 or so) where I have posted how to do it in LUA, but it's basically the same in C++ of course you need to include the native set_char_coordinates into scripting.h

 

 

Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058862241
Share on other sites

I Made SOMETHING with help from here of course.

 

Deleted, Need Some Approvement

 

 

Running Explosion!

Faster You Run , Farther Your Explosion Skill

(Reccomended to use Hand,not pistol or other weapon,Can Be A sucide one If you Don Run)

Edited by aceship
Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058862251
Share on other sites

Hey aru, it really works! colgate.gif I've got a plugin ready now, which is able to load scripts written in any .Net language! In enumerates through all DLLs in the plugins folder, checks if the DLL is a managed assembly, loads it, enumerates through all classes, checks if the class in a subclass of the GTA.GtaScript class, loads it, and runs all those collected scripts from within the CustomThread class.

 

You can really use the whole power of the .Net Framework within those scripts. And since the scripts are written and compiled in Visual Studio you'll have full code highlighting, error checks and stuff. It's amazing! Now i have to clean it up and get it ready for a release (still a lot of work).

 

Simple Script like this one work already:

 

Public Class TestScript  Inherits GTA.GtaScript  Public Overrides Sub Startup()  End Sub  Public Overrides Sub Tick()     If isKeyPressed(Windows.Forms.Keys.M) Then        Dim PlayerIndex As Integer = ConvertIntToPlayerIndex(GetPlayerID)        AddScore(PlayerIndex, 5000)     End If  End SubEnd Class

 

 

I'll make a new thread when it's ready, since i don't want to clutter your one. smile.gif Thanks ALOT aru! icon14.gif

Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058862283
Share on other sites

Im still wondering, how to teleport a player to a coord? Is it possible with a function or does it need memory editing?

You can easily do that with this hook.

 

Look at the LUA main thread (page 36 or so) where I have posted how to do it in LUA, but it's basically the same in C++ of course you need to include the native set_char_coordinates into scripting.h

Thanks, found it. Must have overlooked that function lol.gif

Link to comment
https://gtaforums.com/topic/390582-c-script-hook/page/4/#findComment-1058862365
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 0 User Currently Viewing
    0 members, 0 Anonymous, 0 Guests

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.