For direct access use https://forums.oldunreal.com
It's been quite a while since oldunreal had an overhaul, but we are moving to another server which require some updates and changes. The biggest change is the migration of our old reliable YaBB forum to phpBB. This system expects you to login with your username and old password known from YaBB.
If you experience any problems there is also the usual "password forgotten" function. Don't forget to clear your browser cache!
If you have any further concerns feel free to contact me: Smirftsch@oldunreal.com

Generate random string

The section related to UnrealScript and modding. This board is for coders to discuss and exchange experiences or ask questions.
Post Reply
User avatar
gopostal
OldUnreal Member
Posts: 1005
Joined: Thu Jul 31, 2008 9:29 pm

Generate random string

Post by gopostal »

I'm working on a weather effect mod. It creates it's own lightning, triggered lights, thunder, rain, etc. It occurred to me that it's very possible that multiple actors might be used on the same map so I need to randomize the tag-event strings for each set of weather in a map.

Seems easy but I've not been able to make it work. Is there an elegant solution to generate a 3 or 4 character random string to then plug in as that value so the set is isolated and only triggers the actors in it's pool? I could hard code choices but I know there must be a better way. A google search gives me little to go on that's not UT4 related.
I don't want to give the end away
but we're all going to die one day
User avatar
yrex .
OldUnreal Member
Posts: 273
Joined: Wed May 06, 2015 6:46 am
Contact:

Re: Generate random string

Post by yrex . »

Why not reference them and call Trigger() on them directly?

Also, a random string has a chance of collision.
My work | contact: ampoyrex at wp dot pl
User avatar
gopostal
OldUnreal Member
Posts: 1005
Joined: Thu Jul 31, 2008 9:29 pm

Re: Generate random string

Post by gopostal »

That's probably what I'm going to end up doing, just using the owner value to control the child actors. I was more curious if the random generation was possible since I didn't see an easy way to do it.
I don't want to give the end away
but we're all going to die one day
User avatar
[]KAOS[]Casey
OldUnreal Member
Posts: 4497
Joined: Sun Aug 07, 2011 4:22 am
Location: over there

Re: Generate random string

Post by []KAOS[]Casey »

As mentioned before, it's not ideal, and using children is better.

as for how to generate random strings...

Code: Select all

local string s;

s = chr(rand(255))$chr(rand(255))$chr(rand(255))$chr(rand(255));
you could use a couple RandRanges to print only a-z, or A-Z, or 0-9, 0-255 will result in strings that may contain control characters, non printing characters, etc, but if they're never printed or read anywhere it would work
User avatar
Feralidragon
OldUnreal Member
Posts: 239
Joined: Thu Jul 24, 2008 6:57 pm

Re: Generate random string

Post by Feralidragon »

Strings are NULL-terminated (at least in UT99, not sure about Unreal 227), meaning that if you generate a chr(0) (which generates a NULL control character) you end the string right there, and the engine won't even consider the rest of the generated characters as part of the string.

So yeah, whenever random characters are being generated, you probably want to limit them to VCHARs only (visible ASCII characters essentially).

Do not forget however that a "name" is different from a "string" (Tag and Event are name types), and that means that not only you are even more restricted to the allowed characters in a name, it's also generally a bad idea to generate random "name" strings, since those will just pollute the name table.

I do not understand the original problem very well though, but it sounds to me that what you want in the end is to randomize the triggering of actors with a common Tag, in which case you can build your own Trigger, and when iterating through actors of a specific Tag, you can simply randomize whether or not to trigger each individual actor, like:

Code: Select all

local int probability = 30; //probability to trigger: 0-100

foreach AllActors(class 'Actor', A, Event) {
    if (rand(100) < probability) {
        A.Trigger(...);
    }
}
Last edited by Feralidragon on Sat Oct 19, 2019 8:38 pm, edited 1 time in total.
User avatar
[]KAOS[]Casey
OldUnreal Member
Posts: 4497
Joined: Sun Aug 07, 2011 4:22 am
Location: over there

Re: Generate random string

Post by []KAOS[]Casey »

you're right about the null terminator, I forgot to mention it. so a rand call should be given a +1 to avoid zero if you start from 0 without using randrange.

it's a good thing that UT/Unreal consider 0x0000 a null terminator, otherwise you could have used statlogs to directly write binaries to your system folder, and then execute them with a consolecommmand with no trace of them having existed in cache since they wouldn't have binary equivalents anywhere in cache files :)
User avatar
gopostal
OldUnreal Member
Posts: 1005
Joined: Thu Jul 31, 2008 9:29 pm

Re: Generate random string

Post by gopostal »

I'm working on a general purpose weather mod for mappers. Loading the actor into your map allows you to choose from a number of placeable actors that are pre-configured such as: light rain, heavy rain, thunderstorm, etc.

At the higher levels it spawns an effects package that contains lights and thunder sounds controlled by a stochastic-type trigger. Even though the weather area is large by default (radius of 3072) it occurred to me that mappers might need to place more than one instance. To me it seemed the easiest way to go was to declare tag and events that properly matched. To keep those independent It seemed like a randomly generated string would work.

So to test I added this bit of code

Code: Select all

function PostBeginPlay()
{
      local LightningTrigger LT;
      local string DL;

      LT = Spawn(class'LightningTrigger', Self, , Location + vect (2,2,2),);

      DL = Chr(Rand(254));

      LT.Tag = DL;
}
but I get the error " Error, Type mismatch in '=' " for the last line.

Please bear in mind I work at a lower level than almost everyone here but I do love the problem-solving aspect of uscript. I really appreciate your guy's tolerance for my questions. Oh, and the lightning trigger is used with the original creator's blessing. The mod was done in early 2002 and the maker still responded to an email taken from the readme.

Author: Michael ' Sidewinder ' Geisinger
Copyright: Copyright ( c) 2002 by Michael Geisinger

That's by far my earliest reply and he was excited to here people still worked on this stuff. Thought you all might find this interesting.
I don't want to give the end away
but we're all going to die one day
User avatar
Feralidragon
OldUnreal Member
Posts: 239
Joined: Thu Jul 24, 2008 6:57 pm

Re: Generate random string

Post by Feralidragon »

The mismatch is because Event and Tag are not string types, they are name types.

And although there are ways for converting strings to names (I think there's a direct function in Unreal 227, and there's a workaround as well in UT99), it's generally frowned upon since, as I explained in my previous post, unlike strings, names are saved in an internal table (name table) and they do not leave that table ever until you change the map or close the game, and as aforementioned by others, the names you generate may collide with already existing names and thus resulting in all sorts of weird behaviors (like triggering actors that you didn't mean to trigger), which is easier than it sounds over the fact that names have very restrictive formatting rules, therefore random names is unlikely to ever be a good solution to any problem.

Having that said, I am still struggling in understanding what the actual "problem" is that you want to solve.

Let's say a mapper puts 2 weather actors in the map:
1) If the mapper assigns the same Tag to both, they can be both triggered at the same time by the same trigger;
2) If the mapper assigns a different Tag to each, then that means they will already be triggered at different times, maybe with different conditions.

If your example is the usage of a stochastic trigger, in the 2nd case above the mapper can simply set 2 Events pointing to each Tag, and since the stochastic trigger will only trigger one of them at each time, they will act independently in a random fashion already.

If you are thinking about a use-case that this doesn't address, that goes beyond the capabilities of a stochastic trigger, instead of randomizing tags and events, you probably want to create a new type of trigger for the mapper to use with more functionality.
Last edited by Feralidragon on Sun Oct 20, 2019 11:06 am, edited 1 time in total.
User avatar
gopostal
OldUnreal Member
Posts: 1005
Joined: Thu Jul 31, 2008 9:29 pm

Re: Generate random string

Post by gopostal »

Thanks for that explanation. Now I understand.

I wanted the mapper to just be able to place a single actor for the desired weather effect and if that needed thunder, lightning, wind, etc then it would spawn those effects itself. Since they are area effects they can be spawned near the placed weather actor without the mapper needing to do anything further.

Since there could be multiple copies of the weather on a map I wanted to ensure one didn't trigger the others and so I looked at assigning tag/event. I know I could just use owner but I started down this rabbit hole of how to create a random string and ...well here I am asking another dumb question lol.

I'll use owner on my actor iteration and just be done with it. Thank you for clarifying names and strings. All this time I considered them kind of the same but I see where I was mistaken.
I don't want to give the end away
but we're all going to die one day
Post Reply

Return to “UScript Board”