This is a thread that people can use to share useful custom functions or scripts they have written that they feel the community would find useful.

Be sure to also explain what each does and how to use it.


INDEX


To present code on this forum:
it's slightly different to the discord approach.

Here's a snapshot of how to use the three backticks on here. (seperate lines above/below the code).

produces this:

function min($num_array) begin
    if $num_array == null then
        print("Not a valid array");
        return false;
    end;
    $smallest = $num_array[0];
    for $num in $num_array do
        if $num < $smallest then
            $smallest = $num;
        end;
    end;
    return $smallest;
end;

MIN/MAX

These two functions will add the ability to scan an array and return either the smallest (min) or the largest (max) number.

To install, copy the following code into the Global Functions panel found in Game Configuration.

function min($num_array) begin
    if $num_array == null then
        print("Not a valid array");
        return false;
    end;
    $smallest = $num_array[0];
    for $num in $num_array do
        if $num < $smallest then
            $smallest = $num;
        end;
    end;
    return $smallest;
end;

function max($num_array) begin
    if $num_array == null then
        print("Not a valid array");
        return false;
    end;
    $largest = $num_array[0];
    for $num in $num_array do
        if $num > $largest then
            $largest = $num;
        end;
    end;
    return $largest;
end;

To use, simply pass in an array consisting of numerical values into the function within a script and it will return the smallest/largest number into a variable.

Example:

SWAP ITEMS

Ths function will allow you to specify two items, one found in your inventory, and one to swap it with, and it will scan your inventory and if it finds the matching item, will swap it out.

To install. copy & paste the following code into the Global Functions panel of Game Configuration in your project.

function swap_items($ITEM_1, $ITEM_2) begin
	$slot_num = 0;
	for $i in player.inventory do 
	    $slot_num += 1;
	    if $slot_num < 10 then
	        $itm = widget["inventory"].element["000${$slot_num}"].item;
	        if $itm.id == $ITEM_1 then
	            $itm_count = widget["inventory"].element["000${$slot_num}"].item.count;
	            remove_item($ITEM_1, $itm_count);
	            give_item($ITEM_2, $itm_count);
	        end;
	    else
	        $itm = widget["inventory"].element["00${$slot_num}"].item;
	        if $itm.id == $ITEM_1 then
	            $itm_count = widget["inventory"].element["00${$slot_num}"].item.count;
	            remove_item($ITEM_1, $itm_count);
	            give_item($ITEM_2, $itm_count);
	        end;	        
	    end;
	end;
end;

To use in a script, simply call the function by name, passing in the Item ID's of the one you want to swap out, with the one you want to swap in.
It will even track and swap the amount of items too.

Example:

ASSIGN SKILL

This function will allow you to assign a newly given skill into a skill widget slot.

To install. copy & paste the following code into the Global Functions panel of Game Configuration in your project.

function assign_skill($skill, $slot) begin
    if $slot < 10 then
        widget["skill_bar"].element["000${$slot}"].skill = $skill;
    else
        widget["skill_bar"].element["00${$slot}"].skill = $skill;
    end;
end;

If your project uses a custom widget, just change the widget ID inside the function code (widget["skill_bar"]) to match.

To use, simply call assign_skill in a script that has just given a skill, passing in both the skill ID and the slot number to assign it to.

Example:

CLEAR ENTRY

This function will allow you to scan through a provided array for a provided entry, and if it finds such a entry, will remove it.

There is a built in remove(index) ability you can also use to remove array entries too, which this function actually uses.

However for that you need to provide the index number, while there may be instances where you want to remove an entry by it's actual data instead.


To install.
copy & paste the following code into the Global Functions panel of Game Configuration in your project.

function clear_entry($array, $entry) begin
	$index = 0;
	for $i in $array do
		if $i == $entry then
			$array.remove($index);
			break;
		end;
		$index += 1;
	end;
end;

To use,
simply call the function and provide the array to search, and the data to search for.

Example:


By default, this function will find and delete the first entry it finds that matches the provided data, and then stop.
If you wish to find and remove all entries that match the provided data, simply replace break; from the function code with $index -= 1;.

The result would be something like this:

COUNT ITEMS

This function will scan your inventory for a provided item and return how many items matching the request are found.
It does not matter if items are stackable in a single slot, or repeated in different slots, it will find all instances and add them into a final total, which is then returned into a variable in a main script.

To Install:
copy & paste the following code into the Global Functions panel of Game Configuration in your project.

function count_items($item_id) begin
	$slot_num = 0;
	$itm_count = 0;
	for $i in player.inventory do 
		$slot_num += 1;
		if $slot_num < 10 then
			if widget["inventory"].element["000${$slot_num}"].item != null then
				$itm = widget["inventory"].element["000${$slot_num}"].item;
				if $itm.id == $item_id then
					$itm_count += widget["inventory"].element["000${$slot_num}"].item.count;
				end;
			end;
		else
			if widget["inventory"].element["00${$slot_num}"].item != null then
				$itm = widget["inventory"].element["00${$slot_num}"].item;
				if $itm.id == $item_id then
					$itm_count += widget["inventory"].element["00${$slot_num}"].item.count;
				end;
			end;            
		end;
	end;
	return $itm_count;
end;

IMPORTANT NOTE:
If you are using a custom inventory widget in your project, you will need to alter the widget["inventory"] parts of the function, replacing "inventory" with the ID of your custom widget.


To Use:
Simply call the function into a variable in a script when necessary, and provide an item id of the item you wish to search the inventory for.

Example:

9 days later

REWARD ITEM

This function will scan your inventory for any free slots, and if so, will give the player the reward item(s), if not, will drop them at their feet instead.

To install. copy & paste the following code into the Global Functions panel of Game Configuration in your project.

function reward_item($item_id, $count) begin
  $free_slot = false; // assume no slots free
  for $i in player.inventory do // check every slot
     if $i == null then // if the current slot is null (empty)
      $free_slot = true; // found a slot
      break; // end loop..no point checking rest
    end;
  end;
  if $free_slot == true then // if it found a slot
    give_item($item_id, $count); // give item
  else
    add_item_to_tile(tile[player.coord.x, player.coord.y, player.coord.z], $item_id, $count); // otherwise drop item
  end;
end;

To use, simply call reward_item in any quest/dialogue scripts that reward items.

Example:

a month later

This function will return the remainder of a string start from a position you give it. Remember, the first letter of a string is always position 0.

So if you do something like this:

$word = retStringFromPosition("Yaba Daba Doo", 0)

then $word will return "Yaba Daba Doo".

But if you did this instead:

$word = retStringFromPosition("Yaba Daba Doo", 5)

then it will return "Daba Doo".

function retStringFromPosition($string, $pos) begin
$counter = 0;
$newStr = "";
	for $letter in $string do
		if $counter < $pos then
		     $counter += 1;
			  else	
				  $newStr = $newStr + $letter;
		end;
	end;
return $newStr;
end;

I've been working on trying to create a string find and replace function. So this is my starting point. I will add more functions as I create them. Hope you find this one useful.

a month later

autoZoomIn($from, $to, $speed, $lockcamera)

$from: FOV position it will start.
$to: Where it will end
$speed: how fast it will go. The lower the number the faster. 0 is instant.
$lockcamera: true or false if it will allow the user to adjust the camera while performing the zoom.

Code Function:

function autoZoomIn($from, $to, $speed, $lockcamera) begin
	if $lockcamera == true then
		lock_camera();
	end;
$i = $from;
while $i > $to do
wait($speed);
set_gameplay_property("camera_fov", $i);
$i -=1;
end;
reset_camera();
end;

EG:

autoZoomIn(74, 30, 0.01, true)

Same idea as autoZoomIn, just this time it zooms out. Great for cutscenes.

function autoZoomOut($from, $to, $speed, $lockcamera) begin
	if $lockcamera == true then
		lock_camera();
	end;
$i = $from;
while $i < $to do
wait($speed);
set_gameplay_property("camera_fov", $i);
$i +=1;
end;
reset_camera();
end;
2 months later

CUSTOM USE

This is the function from my recent Free Movement Interaction video.

This function provides an alternate way of interacting with objects and characters in Free Movement if you are experiencing issues with the collision based system.

This function will check all four tiles immediately around the player's tile at the time of execution (usually a keybind) in all four directions (north_tile, south_tile, west_tile, east_tile), store any characters and objects found in that radius, check the player's camera direction and then if in the direction of a valid object/character, will trigger a remote interact of the entity, using the extremely useful new (V1.1) interact_with_entity() command.

Few things to note:
This function prioritises Characters above Objects if both are found on the same tile.
If you wish to swap that, then check for object first, then else, check for character.
If you want to trigger both, then you could check if both are valid, then interact_with_entity character followed by object.

Also:
NAVIGATION REQUIRED!
For this function to work, although in Free Movement, the player no longer needs the navigation lines, for the north_tile, east_tile process to work, all tiles should be linked up in some fashion on the navigation grid.
Pending can be used as an alternative to none functionality wise.

To install, somply copy pase the following code into the Game Configuration > Global Functions panel.

function custom_use() begin
	$player_tile = tile[player.coord.x, player.coord.y, player.coord.z];
	if $player_tile.north_tile.objects != null then
		$north_object = $player_tile.north_tile.objects[0];
	end;
	if $player_tile.east_tile.objects != null then
		$east_object = $player_tile.east_tile.objects[0];
	end;
	if $player_tile.south_tile.objects != null then
		$south_object = $player_tile.south_tile.objects[0];
	end;
	if $player_tile.west_tile.objects != null then
		$west_object = $player_tile.west_tile.objects[0];
	end;
	if $player_tile.north_tile.characters != null then
	     $north_character = $player_tile.north_tile.characters[0];
	end;
	if $player_tile.east_tile.characters != null then
		$east_character = $player_tile.east_tile.characters[0];
	end;
	if $player_tile.south_tile.characters != null then
		$south_character = $player_tile.south_tile.characters[0];
	end;
	if $player_tile.west_tile.characters != null then
		$west_character = $player_tile.west_tile.characters[0];
	end;
	if player.direction == NORTH then
		if $north_character != null then
			interact_with_entity($north_character);
		else
			if $north_object != null then
				interact_with_entity($north_object);
			end;
		end;
	elseif player.direction == EAST then
		if $east_character != null then
			interact_with_entity($east_character);
		else
			if $east_object != null then
				interact_with_entity($east_object);
			end;
		end;
	elseif player.direction == SOUTH then
		if $south_character != null then
			interact_with_entity($south_character);
		else
			if $south_object != null then
				interact_with_entity($south_object);
			end;
		end;
	elseif player.direction == WEST then
		if $west_character != null then
			interact_with_entity($west_character);
		else
			if $west_object != null then
				interact_with_entity($west_object);
			end;
		end;
	end;
end;

To use, make a script and add the following code to it to call the function.

custom_use();

Feel free to bind this script over the top of the base interact key, or to another key to benefit from both interaction styles.