Jump to content

Need help with lua- arrays


Recommended Posts

I'm no lua expert and need some help :D.

 

Here is my pseudo code:

validprefabs = {

  { prefabname="goldnugget" , level=0 },

  { prefabname="dug_marsh_bush", level=1 },

  { prefabname="dug_berrybush", level=1 },

  { prefabname="honeycomb", level=2 },

  { prefabname="fireflies", level=2 },

  { prefabname="butter", level=3 }

}

 

function isprefabvalid(inst)

  --return true if inst prefab is in validprefabs regardless of level

end

 

function giveprefabofnextlevel(inst)

  --random(), if 10%

  --spawn prefab of inst.level+1 randomly from validprefabs

  --if none, then spawn itself (ie: give item back)

  --else if not 10% (ie 90%)

  --spawn prefab of inst.level randomly from validprefabs

  --if none, then spawn itself (ie: give item back)

end

 

...

 

How?

 

Thank you!

Link to comment
Share on other sites

It's not easy to answer but it takes time to write code. It would be better if you learn lua. :)

validprefabs = {  { prefabname="goldnugget" , level=0 },  { prefabname="dug_marsh_bush", level=1 },  { prefabname="dug_berrybush", level=1 },  { prefabname="honeycomb", level=2 },  { prefabname="fireflies", level=2 },  { prefabname="butter", level=3 }}--It's not a good idea to find prefab each time you want to test it.--So we need "associated array" (actually just lua table but with prefabs as keys).local validprefabs_assoc = {}for i,v in ipairs(validprefabs) do	validprefabs_assoc[v.prefabname] = vend--So we can easily return if prefab exists in an array.function isprefabvalid(inst)	return validprefabs_assoc[inst.prefab]end--if you want exactle true/false valuse then use this string:--return validprefabs_assoc[inst.prefab] and true or false --It's not a good idea to find level each time you want to compare it with other levels.--I guess "validprefabs" is a static table, and you are not going to change levels in it.--So we can prepare another sorted table for faster search.local validprefabs_levels = {}for i,v in ipairs(validprefabs) do	if not validprefabs_levels[v.level] then		validprefabs_levels[v.level] = {} --creating "container" for all prefabs with same level	end	table.insert(validprefabs_levels[v.level],v) --copying prefab to that "container"end function giveprefabofnextlevel(inst)	if not inst.level then		return --Protect from fool mistakes.	end	--Well, we just need to get prefab name. "Spawn" is next task. Don't mix it	local prefab --undefined yet	local level = ( math.random() < 0.1 ) and (inst.level + 1) or inst.level -- level+1 if less than 10%, or just inst.level	--now level variable contains valid level that we need	if validprefabs_levels[level] then --There are some prefabs		local sub_table = validprefabs_levels[level] --table of prefabs with equal level		local cnt = #sub_table --size of table		local pick_number = math.random(1,cnt) --take any random prefab		prefab = sub_table[pick_number].prefabname --Saving prefab name.	else --There is no valid prefab. Spawn itself (ie: give item back)		prefab = inst.prefab	end	--So prefab variable contains valid prefab to spawn	--You can "return prefab" or continue work in this function depending on logic of your mod.	local loot = GLOBAL.SpawnPrefab(prefab)	if loot then		--do something. For example, give item to player. But which player?	endend

Link to comment
Share on other sites

@SenL, You can organize the table in a much nicer way for this purpose.

validprefabs = {	{"goldnugget"},	{"dug_marsh_bush", "dug_berrybush"},	{"honeycomb", "fireflies"},	{"butter"}}--build a lookup tableprefablevel = {}for level,group in pairs(validprefabs) do	for _,prefab in ipairs(group) do		prefablevel[prefab] = level	endend function isprefabvalid(inst)	--return true if inst prefab is in validprefabs regardless of level	return prefablevel[inst.prefab] ~= nilend function giveprefabofnextlevel(inst)	local prefab = inst.prefab -- the default behavior is to give back the current prefab	local level = prefablevel[prefab]	if math.random() < 0.1 then	--10% chance of trying the next level up		level = level + 1	end -- otherwise try the current level	local choices = validprefabs[inst.level+1] --find what options we have at this level	if choices ~= nil then -- there are options at this level, spawn one		prefab = choices[math.random(1,#choices)]	end --otherwise, we can just use the default	SpawnPrefab(prefab)	--[[ I'm guessing this is an inventoryitem that you're activating,		So maybe you want to be giving it to the player like so:			inst.components.inventoryitem.owner.components.inventory:GiveItem(SpawnPrefab(prefab))	]]end

@Maris, ... that's monstrous o_o. 

 

Edit: Changed the lookup table to provide the level of the item currently being activated instead of assuming a character level.

Edited by rezecib
Link to comment
Share on other sites

@SenL, I assumed you had your own system for setting inst.level, since you didn't provide any information on how you wanted it to get set.

 

inst.prefab I'm just getting from the inst that you're passing to the isprefabvalid function.

 

Edit: Ohhh, you want it to be the level of the item that was activated in your table? I can update the code to reflect that, by changing the lookup table to give the level instead of just "true".

Edited by rezecib
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...