Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Tuners
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    2. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    3. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    1. Grand Theft Auto Series

      1. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    6. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

      1. GTANet 20th Anniversary
    2. Support

    3. Suggestions

C++ Script Hook


aru
 Share

Recommended Posts

Call it in the tick event, then it will display every frame. Make some kind of check to see, if you need to display text, or not.

 

 

 

That works,

 

Of course I now need to find a way to make a check smile.gif

 

 

Link to comment
Share on other sites

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
Share on other sites

This looks very nice, I'll try to learn C++ if I have patience biggrin.gif

 

also, I need a ASI or DLL loader right?

 

Where can I get that, like are there any good? biggrin.gif

Edited by ExtraOrdinary
Link to comment
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
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
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
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
Share on other sites

Nice work. But am I correct in thinking it doesn't work outside the thread it's contained in? (For example only able to be used in ::RunScript)

Link to comment
Share on other sites

in order to have a godmode where explosions affect niko i tried to set health to 1000 each tick. Niko would still die in explosions, but the screen stayed in full color(no black and white death scene). Anybody know why or have any suggestions?

Link to comment
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
Share on other sites

Scripts don't seem to re-hook when there is a new player creation beyond the first instance.

 

e.g. if you are in game, then join an mp game, then return to single player a script no longer functions.

 

How do I correct this?

Link to comment
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
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
Share on other sites

In C++ the equality comparison operator is "==".

 

e.g.

 

(&p4 == 3)

 

bS8xA.png
Link to comment
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
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
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
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
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)

bS8xA.png
Link to comment
Share on other sites

thanks for the help but it cant be used when i used it like this

 

(p1 = x + x1*1.2);

(p2 = y + y1*1.2);

(p3 = z );

 

 

im not using this for super running

Link to comment
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
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
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
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
Share on other sites

Wasnt this is not a plugin?

see the first page

and..

how to displaytext?

 

 

Link to comment
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
 Share

  • 2 Users Currently Viewing
    0 members, 0 Anonymous, 2 Guests

×
×
  • Create New...

Important Information

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