Skip to content
View in the app

A better way to browse. Learn more.

GTAGames.nl - De Nederlandse Grand Theft Auto Community!

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[IV|C++] Variabele van ander subprogramma gebruiken

Hallo,

Om een beetje te leren scripten in GTA4 ben ik begonnen met het aanpassen van de 'CustomFiberThread' voorbeelden die bij de C++ scripthook van aru zitten. Sorry als het een beetje een domme vraag is ^^

Ik heb nu een werkend script om een explosieve barrel(MODEL_CJ_GAS_CANZ) te spawnen boven de speler, alleen probeer ik nu om deze met een ander subprogramma(of hoe je het ook noemt :clown: ) te laten verplaatsen in de x-as en dat lukt niet helemaal.

De functie voor het spawnen van de barrel is void CustomFiberThread::SpawnBarrel(eModel model) en die voor het verplaatsen void CustomFiberThread::MoveBarrelx(Object obj). Hieronder de code

CustomFiberThread.h:

#pragma once

#include "../ScriptHook/ScriptThread.h"
#include "../ScriptHook/ScriptingEnums.h"
#include "../ScriptHook/ScriptingTypes.h"

class CustomFiberThread : 
public ScriptThread
{
private:
Scripting::Player GetPlayer();
Scripting::Ped GetPlayerPed();

void SpawnBarrel(Scripting::eModel model);
void MoveBarrelx(Scripting::Object obj);

protected:
   // We don't want a Tick based script, so we override RunScript.
void RunScript();

public:
CustomFiberThread();
};

CustomFiberThread.cpp:

#include "CustomFiberThread.h"
#include "Scripting.h"
#include "../ScriptHook/Log.h"

#include <windows.h>

// Pull in all our scripting functions/types
using namespace Scripting;

CustomFiberThread::CustomFiberThread()
{
   // Give your own name here!
SetName("rommel");
}

// Some helper functions

Player CustomFiberThread::GetPlayer()
{
Player playerIndex = ConvertIntToPlayerIndex(GetPlayerId());
return playerIndex;
}

Scripting::Ped CustomFiberThread::GetPlayerPed()
{
Ped ped;
GetPlayerChar(GetPlayer(), &ped);
return ped;
}

void CustomFiberThread::SpawnBarrel(eModel model)
{
RequestModel(model);

while(!HasModelLoaded(model))
{
	Wait(0);
}

LogInfo("Barrel geladen");

Ped ped = GetPlayerPed();
Vehicle vehicle;
Object obj;
f32 x,y,z;
GetCharCoordinates(ped, &x, &y, &z);
z = z + 5; //z + 5 om de barrel boven de speler te spawnen
CreateObject(model, x, y, z, &obj, true);
FreezeObjectPosition(obj, true); //Object bevriezen om hem nog te kunnen verplaatsen
Wait(100);
MarkModelAsNoLongerNeeded(model);
LogInfo("Barrel is gespawnd");

}

void CustomFiberThread::MoveBarrelx(Object obj)
{
SetObjectCoordinates(obj, (x + 15), y, z); //x + 15 om de x-coordinaat met 5 te vergroten)
FreezeObjectPosition(obj, false); //Object ontdooien om physics te laten werken
LogInfo("Barrel is verplaatst");
}


// The real script

void CustomFiberThread::RunScript()
{
// This is a fiber thread, so we use an loop to run the contents of this script.
// The thread will terminate when we return from this function.

while(IsThreadAlive())
{


	if ((GetAsyncKeyState(VK_F6) & 1) != 0)
	{
		LogInfo("Barrel wordt gespawnd");
		SpawnBarrel(MODEL_CJ_GAS_CANZ);
	}

	else if ((GetAsyncKeyState(VK_F7) & 1) != 0)
	{
		LogInfo("Barrel wordt in x vooruit verplaatst");
		MoveBarrelx(obj);
	}



	// Call Wait() so we can process other scripts/game code
	// You must call Wait(...) in your loop code for a fiber thread!
	Wait(100);

}


}

Alleen, ik krijg bij het builden melding dat de variabelen in MoveBarrelx niet bestaan(x,y,z en obj), terwijl het dus de bedoeling is dat die uit SpawnBarrel gebruikt worden.

Build log van Visual C++ Express 2010:

2>c:\map\customfiberthread.cpp(58): error C2065: 'x' : undeclared identifier
2>c:\map\customfiberthread.cpp(58): error C2065: 'y' : undeclared identifier
2>c:\map\customfiberthread.cpp(58): error C2065: 'z' : undeclared identifier
2>c:\map\customfiberthread.cpp(84): error C2065: 'obj' : undeclared identifier

Ik heb het vermoeden dat ze in een class gezet moeten worden die door beide scripts gebruikt kunnen worden, maar heb eerlijk gezegd geen idee waar ik dat moet doen voor dit script. Van algemene C++ tutorials werd ik ook niet veel wijzer.

Kan iemand me dus helpen om MoveBarrelx gebruik te laten maken van de coordinaten en het barrel van SpawnBarrel?

Alvast bedankt :)

Featured Replies

Je moet de variabelen obj, x, y en z dan wel in de global scope zetten. Als je bijvoorbeeld x definieert in een functie, bestaat die alleen in die functie en wordt daarna verwijderd (local scope). Wat je moet doen is:

CustomFiberThread.h:

#pragma once

#include "../ScriptHook/ScriptThread.h"
#include "../ScriptHook/ScriptingEnums.h"
#include "../ScriptHook/ScriptingTypes.h"

class CustomFiberThread : 
       public ScriptThread
{
private:
       Scripting::Player GetPlayer();
       Scripting::Ped GetPlayerPed();
       Scripting::Object obj;
       f32 x, y, z;

       void SpawnBarrel(Scripting::eModel model);
       void MoveBarrelx(Scripting::Object obj);

protected:
   // We don't want a Tick based script, so we override RunScript.
       void RunScript();

public:
       CustomFiberThread();
};

Je moet dan in het script verder wel zorgen dat je naar deze wijst en geen nieuwe maakt, dus:

CustomFiberThread.cpp:

#include "CustomFiberThread.h"
#include "Scripting.h"
#include "../ScriptHook/Log.h"

#include <windows.h>

// Pull in all our scripting functions/types
using namespace Scripting;

CustomFiberThread::CustomFiberThread()
{
   // Give your own name here!
       SetName("rommel");
}

// Some helper functions

Player CustomFiberThread::GetPlayer()
{
       Player playerIndex = ConvertIntToPlayerIndex(GetPlayerId());
       return playerIndex;
}

Scripting::Ped CustomFiberThread::GetPlayerPed()
{
       Ped ped;
       GetPlayerChar(GetPlayer(), &ped);
       return ped;
}

void CustomFiberThread::SpawnBarrel(eModel model)
{
       RequestModel(model);

       while(!HasModelLoaded(model))
       {
               Wait(0);
       }

       LogInfo("Barrel geladen");

       Ped ped = GetPlayerPed();
       Vehicle vehicle;
       GetCharCoordinates(ped, &x, &y, &z);
       z = z + 5; //z + 5 om de barrel boven de speler te spawnen
       CreateObject(model, x, y, z, &obj, true);
       FreezeObjectPosition(obj, true); //Object bevriezen om hem nog te kunnen verplaatsen
       Wait(100);
       MarkModelAsNoLongerNeeded(model);
       LogInfo("Barrel is gespawnd");

}

void CustomFiberThread::MoveBarrelx(Object obj)
{
       SetObjectCoordinates(obj, (x + 15), y, z); //x + 15 om de x-coordinaat met 5 te vergroten)
       FreezeObjectPosition(obj, false); //Object ontdooien om physics te laten werken
       LogInfo("Barrel is verplaatst");
}


// The real script

void CustomFiberThread::RunScript()
{
       // This is a fiber thread, so we use an loop to run the contents of this script.
       // The thread will terminate when we return from this function.

       while(IsThreadAlive())
       {


               if ((GetAsyncKeyState(VK_F6) & 1) != 0)
               {
                       LogInfo("Barrel wordt gespawnd");
                       SpawnBarrel(MODEL_CJ_GAS_CANZ);
               }

               else if ((GetAsyncKeyState(VK_F7) & 1) != 0)
               {
                       LogInfo("Barrel wordt in x vooruit verplaatst");
                       MoveBarrelx(obj);
               }



               // Call Wait() so we can process other scripts/game code
               // You must call Wait(...) in your loop code for a fiber thread!
               Wait(100);

       }


}

(Ik doe dit even zonder het zelf in een editor geladen te hebben, dus het kan zijn dat het nog anders moet)

  • Author

Bedankt, precies wat ik zocht, het werkt zo perfect! :dans:

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.