logo
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Generate random string (Read 1143 times)
gopostal
Betatester
Offline


Hack coder

Posts: 943
Joined: Jul 31st, 2008
Gender: Male
Generate random string
Oct 17th, 2019 at 1:44pm
Print Post  
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
Back to top
 
IP Logged
 
yrex .
Betatester
Offline


Oldunreal member

Posts: 227
Joined: May 6th, 2015
Re: Generate random string
Reply #1 - Oct 17th, 2019 at 2:59pm
Print Post  
Why not reference them and call Trigger() on them directly?

Also, a random string has a chance of collision.
  

Quality content | mail me instead of PMing: ampoyrex(a)wp(.)pl
Back to top
 
IP Logged
 
gopostal
Betatester
Offline


Hack coder

Posts: 943
Joined: Jul 31st, 2008
Gender: Male
Re: Generate random string
Reply #2 - Oct 18th, 2019 at 1:34pm
Print Post  
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
Back to top
 
IP Logged
 
[]KAOS[]Casey
Developer Team
Betatester
Offline


nedm

Posts: 3214
Joined: Aug 7th, 2011
Gender: Male
Re: Generate random string
Reply #3 - Oct 19th, 2019 at 7:02pm
Print Post  
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
  
Back to top
 
IP Logged
 
Feralidragon
Betatester
Offline



Posts: 224
Location: Lisbon - Portugal
Joined: Jul 24th, 2008
Gender: Male
Re: Generate random string
Reply #4 - Oct 19th, 2019 at 8:33pm
Print Post  
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 (Java)
Select All
local int probability = 30; //probability to trigger: 0-100

foreach AllActors(class 'Actor', A, Event) {
    if (rand(100) < probability) {
        A.Trigger(...);
    }
}
 

  
Back to top
IP Logged
 
[]KAOS[]Casey
Developer Team
Betatester
Offline


nedm

Posts: 3214
Joined: Aug 7th, 2011
Gender: Male
Re: Generate random string
Reply #5 - Oct 20th, 2019 at 4:45am
Print Post  
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 Smiley
  
Back to top
 
IP Logged
 
gopostal
Betatester
Offline


Hack coder

Posts: 943
Joined: Jul 31st, 2008
Gender: Male
Re: Generate random string
Reply #6 - Oct 20th, 2019 at 9:42am
Print Post  
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
Back to top
 
IP Logged
 
Feralidragon
Betatester
Offline



Posts: 224
Location: Lisbon - Portugal
Joined: Jul 24th, 2008
Gender: Male
Re: Generate random string
Reply #7 - Oct 20th, 2019 at 10:56am
Print Post  
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.
  
Back to top
IP Logged
 
gopostal
Betatester
Offline


Hack coder

Posts: 943
Joined: Jul 31st, 2008
Gender: Male
Re: Generate random string
Reply #8 - Oct 20th, 2019 at 2:53pm
Print Post  
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
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint
Bookmarks: del.icio.us Digg Facebook Google Google+ Linked in reddit StumbleUpon Twitter Yahoo