Jump to content
Basicology Major

Script Help: Random Generating Phrase With Arrays/Displaying As Hints/Code Phrase

Recommended Posts

I am pretty new to doing any sort of SQF/init scripting that isnt just frankensteining code together so I would appreciate any help!

Simply put, I want to make a fun little roleplay mission where for that mission, a phrase comprised of multiple randomly selected arrays is generated.

  • The Indfor act as captives that are all assigned ONE word from the phrase at random to keep secret. (This is where multiple arrays work better since you can just call the arrays by "word1", "word2", "word3". (I intend to have 4 words, 1 array for each.)
  • And ontop of that I want the entire code phrase to be displayed in its entirety to the Blufor.  For this to work, the result of the generation needs to only be prompted once and then have.

    TLDR: Server generates a code phrase from several arrays. I cant figure out the init code and they usually return as null no matter what I attempt. And to be exact, I dont want people to generate a phrase/array results every time they call; I simply want it generated, stored, and have players access the results.

 

I dont know how efficiently this could be done whatsoever outside of theory crafting nonsensical code. If there is a better alternative please help me out.

Please help, Working backwards I am trying to:

  1. I inserted the code into initServer.sqf for simplicity; Can execute it as a script later on? Or is a dedicated SQF necessary?
    This code is almost certainly full of wrong or incorrect syntax/formatting. This is just an outliner of what I am trying. If my method is wrong then please help me goal wise.
    Spoiler


     

    
    ///////////////////// 
    // initServer.sqf for simplicity, or new script SQF on init?
    
    
    
    _array1 = ["Alpha", "Bravo", "Charlie"];
    _array2 = ["Delta", "Echo", "Foxtrot"];
    _array3 = ["Golf", "Hotel", "India"];
    _array4 = ["Juliet", "Kilo", "Lima"];
    
    _codeWord1 = selectRandom _array1;
    _codeWord2 = selectRandom _array2;
    _codeWord3 = selectRandom _array3;
    _codeWord4 = selectRandom _array4;
    
    _codePhrase = [_codeWord1 + " " + _codeWord2 + " " + _codeWord3 + " " + _codeWord4]; //// Spaces/Pluses to append & add spaces when displaying full phrase
    
    publicVariable "codePhrase"; // Make it accessible to all clients


     

    2. Have a way to display either a SINGLE result of that array on individual units, preferably just adding a action into their init boxes to get the array/codephrase.
        I want 2 UserActions I can insert into unit inits; One that displays the codephrase, and one that displays a single array's result- as hints.


     

     

Share this post


Link to post
Share on other sites
13 hours ago, Basicology Major said:

I inserted the code into initServer.sqf for simplicity; Can execute it as a script later on? Or is a dedicated SQF necessary?
This code is almost certainly full of wrong or incorrect syntax/formatting. This is just an outliner of what I am trying. If my method is wrong then please help me goal wise.

 

1. initServer.sqf is a perfect place for something like this and you're actually pretty close with your existing code.

Remove the [ ] from:

_codePhrase = [_codeWord1 + " " + _codeWord2 + " " + _codeWord3 + " " + _codeWord4];

 

Add a new line after to store the constructed "sentence" plus all 4 codewords in an array:

codePhrase = [ _codePhrase, _codeWord1, _codeWord2, _codeWord3, _codeWord4 ];

 

You are already publishing the codePhrase variable so that's it for the server side. The codePhrase array now contains the solution and the individual words and is sent to clients automatically, even for JIP players.

 

2. If you do the above then accessing the phrase or words simply means reading the correct element in codePhrase:

codePhrase select 0   // the entire phrase

codePhrase select 1   // the first word

codePhrase select 4   // the last word

codePhrase select ( 1 + floor random 4 )   // a randomly selected word

 

The one thing to be aware of is that published variables are not immediately available in the early phases of joining a server. Not a problem for actions but if you are to use codePhrase from clients in say init.sqf make sure you check that isNil "codePhrase" is false 

 

The rest is just adding the appropriate actions (see addAction) and hints.

 

If you want to run code only on clients you can use initPlayerLocal.sqf

  • Like 1

Share this post


Link to post
Share on other sites
On 1/9/2024 at 1:58 AM, mrcurry said:

 

1. initServer.sqf is a perfect place for something like this and you're actually pretty close with your existing code.

Remove the [ ] from:

_codePhrase = [_codeWord1 + " " + _codeWord2 + " " + _codeWord3 + " " + _codeWord4];

 

Add a new line after to store the constructed "sentence" plus all 4 codewords in an array:

codePhrase = [ _codePhrase, _codeWord1, _codeWord2, _codeWord3, _codeWord4 ];

 

You are already publishing the codePhrase variable so that's it for the server side. The codePhrase array now contains the solution and the individual words and is sent to clients automatically, even for JIP players.

 

2. If you do the above then accessing the phrase or words simply means reading the correct element in codePhrase:

codePhrase select 0   // the entire phrase

codePhrase select 1   // the first word

codePhrase select 4   // the last word

codePhrase select ( 1 + floor random 4 )   // a randomly selected word

 

The one thing to be aware of is that published variables are not immediately available in the early phases of joining a server. Not a problem for actions but if you are to use codePhrase from clients in say init.sqf make sure you check that isNil "codePhrase" is false 

 

The rest is just adding the appropriate actions (see addAction) and hints.

 

If you want to run code only on clients you can use initPlayerLocal.sqf

Would you mind supplying me with a User action (scroll menu) example to properly access and display via hint? I've made my attempts but it kind of just returns (null) every time and I just don't have much examples I can go off of to read and print variables into hints.

Share this post


Link to post
Share on other sites
18 hours ago, Basicology Major said:

Would you mind supplying me with a User action (scroll menu) example to properly access and display via hint? I've made my attempts but it kind of just returns (null) every time and I just don't have much examples I can go off of to read and print variables into hints.

 

This should get you started:

Spoiler

//initServer.sqf
_array1 = ["Alpha", "Bravo", "Charlie"];
_array2 = ["Delta", "Echo", "Foxtrot"];
_array3 = ["Golf", "Hotel", "India"];
_array4 = ["Juliet", "Kilo", "Lima"];

_codeWord1 = selectRandom _array1;
_codeWord2 = selectRandom _array2;
_codeWord3 = selectRandom _array3;
_codeWord4 = selectRandom _array4;

_codePhrase = _codeWord1 + " " + _codeWord2 + " " + _codeWord3 + " " + _codeWord4; //// Spaces/Pluses to append & add spaces when displaying full phrase
codePhrase = [ _codePhrase, _codeWord1, _codeWord2, _codeWord3, _codeWord4 ];

publicVariable "codePhrase"; // Make it accessible to all clients



//initPlayerLocal.sqf
// This action shows the code phrase to whichever side you wish to know it, just change BLUFOR to OPFOR, independent or civilian
if( playerSide == BLUFOR ) then {
	player addAction [
		"Hint code phrase",										// title
		{												// script start
			params ["_target", "_caller", "_actionId", "_arguments"]; 
			
			// Fetch the phrase, this assumes the initServer.sqf is set up correctly
			_phrase = codePhrase select 0;

			// Hint the phrase
			hint format ["The code phrase is:\n%1", _phrase];
		},												// script end
		nil,												// arguments
		1.5,												// priority
		false,												// showWindow
		true,												// hideOnUse
		"",												// shortcut
		"true",												// condition
		-1,												// radius
		false,												// unconscious
		"",												// selection
		""												// memoryPoint
	];
};



// This may be called from a unit init, will randomly select a word first time its used and after that only show the same word, for all players
// No comments since preproccessing isn't supported in unit init's in the editor.
// Just copy the below code into the init field of a given unit
// if you wish for the action to only be available when they're dead change "true" to "!alive _target"
this addAction [
	"Hint random word",
	{
		params ["_target", "_caller", "_actionId", "_arguments"];
		
		_word = _target getVariable ["unitCodeWord", ""];
		if( _word == "" ) exitWith {
			_word = codePhrase select (1 + floor random 4 );
			_target setVariable ["unitCodeWord", _word, true];
			hint format ["Code word discovered:\n%1", _word];
		}
	
		hint format ["Code word:\n%1", _word];
	},
	nil,
	1.5,
	false,
	true,
	"",
	"true",
	-1,
	false,
	"",
	""
];

 

 

If you want to get fancy with your hints read up on: parseText and Structured Text

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×