Giving number values to strings

I have been as of recently going through my ever-so long list of old projects and I have noticed my project about trying to create a life simulation, inspired by Primer’s blob simuatlions.

I had long ago abandoned the project to focus on new, simpler games, so the code that I will be showing is quite aged and I was not as experienced as I am today at RbxLua. However, while revisiting it, I remembered that I made a number incoding system. Here are some script segments and explanation on how it works:

Unfortunetly back then I barely, if at all, commented on my scripts, so I will be explaining how it works from memory and anylizing the script.

First, here are the values:

module.GeneSelection = {"a","g","u","m","d","c","q"}
module.GeneValues = {a=1,g=-1,u=0,m=.5,d=-.5,c=2,q=2}

GeneSelection are the letters that the string will contain of. GeneValues is an array of the values assigned to the ‘Genes’ in GeneSelection.

Here is a main function in the ModuleScript:

module.GeneConverter = function(Organism)
	local Speed = 0

	local GeneCheck = 1
	repeat
		task.wait(0.05)
		local Genes = Organism:GetAttribute("Genes")
		local FocusGene = string.sub(Genes,GeneCheck,GeneCheck)
		local GeneVal = GeneValues[FocusGene]
		Speed = Speed + GeneVal
		GeneCheck = GeneCheck + 1
	until GeneCheck == #Genes
	Organism:SetAttribute("Speed",Speed)
end

Basically, every organsim (For now just a Part with attached Values) has an attribute attached called ‘Genes’, it is a long string of the GeneSelection characters in a random order.

(Back then I did not know how to use for v=1,n loops, so I had a repeat in there.)

What the function does is go through all the characters in the GeneSequence, then it adds the ‘Gene’'s value to the total.

There is a limit to how high or low the GeneValue can go. The longer the string of genes, the wider the gap is. This system also only has addition and subtraction parameters, so very large, small or precise numbers cannot be represented, but honestly, it does not need to, as the total value should not be too big or small.

Yes, this can make the value a negative number, since it is counting speed, it would be an awkward mess…

The Mutate function:

module.Mutate = function(intensity,Organism)
	local suc, err = pcall(function()
		local ranD = {} 
		local RepeatTries = 0
		local sel = {} 
		local VTPrint
		repeat
			wait(0.05)
			table.insert(sel,GeneSelection[math.random(1,#GeneSelection)])
			table.insert(ranD,math.random(1,#Organism:GetAttribute("Genes")))
			RepeatTries = RepeatTries + 1
		until RepeatTries == intensity

		for i, v in pairs(sel) do
			wait(0.05)
			VTPrint = string.sub(Organism:GetAttribute("Genes"),1,ranD[i]-1)..v..string.sub(Organism:GetAttribute("Genes"),ranD[i]+1,#Organism:GetAttribute("Genes"))
		end
		Organism:SetAttribute("Genes",VTPrint)
		return VTPrint
	end)
	
	if not suc then
		warn(err)
	end
	
end

What this function does is it takes a random ‘Gene’ in the string and mutates it, or in other words, changes it.

You probably have already noticed by now, but of course, these are not T,G,C,A and U genes, that are present in real life. The genes are not pared and in all honesty, is just a string of random characters that has a value. But I am calling them Genes as it makes it clearer throughout that I am doing a ‘life’ simulation, and not so it is confused for security-based encryption.

Why use it?’ You may ask; changing letters in a string results in far more interesting outputs then alternatives like math.random or math.noise

1 Like