User Tools

Site Tools


scripting:snippets

This is an old revision of the document!


Snippets

Basics : Get a Entity

One of the most used commands is probably 'FindEntityByName'. Whevever you want to script something in the game, you should find its entity. This is simply done by the below line.

Notice the variable name 'soldier' and the entityname 'soldier_scripted' - You have to change these variables to reflect your entity ingame.

Example1: get the entity of a soldier with the name 'soldier_scripted'

GenericEntity soldier = GenericEntity.Cast(GetWorld().FindEntityByName("soldier_scripted"));

Example1: get the entity of a vehicle with the name 'BTR1'

GenericEntity btr = GenericEntity.Cast(GetWorld().FindEntityByName("BTR1"));

Lets break down this line.

This is where we declare our variablename and type, where the name is 'btr' and the type is 'GenericEntity'

GenericEntity btr = 

So, the variable 'btr' wil contain the results of a combination of 2 commands.

When we break it down you'll notice these 2 commands.

GetWorld().FindEntityByName("BTR1")

GenericEntity.Cast()

Lets not get too technical, but what you need to know is that we search for the entity 'BTR1' - The result of this command is passed to the CAST function of the GenericEntity object.

GenericEntity.Cast(GetWorld().FindEntityByName("BTR1"));  

What does that mean? - it means that the result of your FindEntityByName is 'casted' (*converted) to a GenericEntity Object. And with a genericEntity object we can do things…

Ok. Now we have the entity of BTR1 - lets do something with it.


//Get the entity (soldier)
GenericEntity soldier = GenericEntity.Cast(GetWorld().FindEntityByName("soldier_scripted"));


//Control the Soldier.
CharacterControllerComponent soldier_c = CharacterControllerComponent.Cast(soldier.FindComponent(CharacterControllerComponent));

// change stance
soldier_c.SetStanceChange(ECharacterStanceChange.STANCECHANGE_TOCROUCH);

// move soldier
vector direction = {0,0,1};
soldier_c.SetMovement(2, direction);


// shoot phase 1
soldier_c.SetWeaponRaised(true);
soldier_c.SetSafety(false, true);

// shoot phase 2
soldier_c.SetFireMode(2);
soldier_c.SetFireWeaponWanted(1)
scripting/snippets.1660220777.txt.gz · Last modified: 2022/08/11 13:26 by jerryhopper

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki