UScript for Unturned Quick Tips: Difference between revisions

From Free Knowledge Base- The DUCK Project
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 7: Line 7:
Rewrite it as
Rewrite it as
  if (player != null && player.id != null && player.id == strName) {
  if (player != null && player.id != null && player.id == strName) {
== understanding code ==
=== properties vs. methods and how object data is managed in uScript/C# ===
An object might expose its data as properties.  An object may be private or read-only field—meaning it cannot be set directly.  For example if I have the object "str.owner" which gets its value from server.getStructuresInRadius filling it like an array, it can not be assigned directly.  Take the code example:
foreach(str in server.getStructuresInRadius(player.position, 50)){ str.set_owner(pid); }
You can see str is gathering more information than just on one thing, but on several things "structures" that are within a radius.  For each one, we want to set a new value of "owner" but we can not use Direct Property Assignment (str.owner = pid;) because it will generate the error
error: Info: structure.set_owner: invalid argument(s) for function - set_owner(string)
Therefore we assign the value using a Setter Method
str.set_owner(pid);
The presence of set_owner(string) suggests that owner is controlled via a method.  uScript wants you to use an official function to change ownership rather than allowing direct modification.  Many game engines restrict direct property modification for important in-game objects to prevent unintended behavior or security issues.  By forcing the use of set_owner(string), Unturned ensures that ownership changes go through controlled logic.
* Direct assignment (=) works only if the property is publicly writable.
* Method calls (set_owner()) enforce rules and logic that the game developers want.
This pattern is common in API design where direct field manipulation is restricted to maintain stability and prevent exploits.


== uScript pages ==
== uScript pages ==

Revision as of 12:07, 30 January 2025

resolving errors

object reference not set to an instance of an object

One or more objects used in the script are at some point null during runtime.

For example, take the line of code

if (player.id == strName) {

Rewrite it as

if (player != null && player.id != null && player.id == strName) {

understanding code

properties vs. methods and how object data is managed in uScript/C#

An object might expose its data as properties. An object may be private or read-only field—meaning it cannot be set directly. For example if I have the object "str.owner" which gets its value from server.getStructuresInRadius filling it like an array, it can not be assigned directly. Take the code example:

foreach(str in server.getStructuresInRadius(player.position, 50)){ str.set_owner(pid); }

You can see str is gathering more information than just on one thing, but on several things "structures" that are within a radius. For each one, we want to set a new value of "owner" but we can not use Direct Property Assignment (str.owner = pid;) because it will generate the error

error: Info: structure.set_owner: invalid argument(s) for function - set_owner(string)

Therefore we assign the value using a Setter Method

str.set_owner(pid);

The presence of set_owner(string) suggests that owner is controlled via a method. uScript wants you to use an official function to change ownership rather than allowing direct modification. Many game engines restrict direct property modification for important in-game objects to prevent unintended behavior or security issues. By forcing the use of set_owner(string), Unturned ensures that ownership changes go through controlled logic.

  • Direct assignment (=) works only if the property is publicly writable.
  • Method calls (set_owner()) enforce rules and logic that the game developers want.

This pattern is common in API design where direct field manipulation is restricted to maintain stability and prevent exploits.

uScript pages

related