Jump to content

randomizer not working


Recommended Posts

local function FindTreeToChopAction(inst)
    local target = FindEntity(inst, SEE_TREE_DIST, function(item) return item.components.workable and item.components.workable.action == ACTIONS.CHOP end)
    if target then
	inst:DoPeriodicTask(math.random(10, 17),(function(inst) 
	inst.components.talker:Say(STRINGS.SMPERD.WOD["WOD" .. math.random(1, 7)])
	end))
        return BufferedAction(inst, target, ACTIONS.CHOP)
    end
end

rn i can't get the math.random to work, it just keeps on counting to a certain point then keeps on spewing dialogue but instead it should 

randomly say 1 out of 7 things

inst:Doperiodictask(math.random(10,17),(function(inst) 

(this is what i used to create this but it doesn't work)

Link to comment
Share on other sites

It seems you only want to say something once per action right instead maybe DoTaskIntime instead of DoPeriodicTask.

DoTaskInTime accepts similar parameters but only does the task once, where as DoPeriodicTask will go on forever until its canceled.

Also DoPeriodicTask will always update at the same interval period so it only seems to accept the math.random once

Its also a good idea to store tasks inside variables so they can be canceled or you could have multiple tasks running simultaneously.

local function FindTreeToChopAction(inst)
	local target = FindEntity(inst, SEE_TREE_DIST, function(item) return item.components.workable and item.components.workable.action == ACTIONS.CHOP end)
	if target then
		if inst.WODSPEECHTASK ~= nil then
			inst.WODSPEECHTASK:Cancel()
			inst.WODSPEECHTASK = nil
		end
		inst.WODSPEECHTASK = inst:DoTaskInTime(math.random(10,17),(function(inst) 
			inst.components.talker:Say(STRINGS.SMPERD.WOD[math.random(1,#STRINGS.SMPERD.WOD)])
		end))
		return BufferedAction(inst, target, ACTIONS.CHOP)
	end
end

As for the random speech, it seems you are just turning your random number into a string rather than calling a random string.
You could always use the built in function GetString(inst, stringtable) stringtable is a table inside STRINGS.CHARACTERS by default

I am assuming you have a table full of strings here STRINGS.SMPERD.WOD so all you really should need to do is

STRINGS.SMPERD.WOD[math.random(1,#STRINGS.SMPERD.WOD)]
--returns random number between 1 and size of the table, of STRINGS.SMPERD.WOD
--which is the key for said simple table using default numerical keys.

Without access to other information I am just guessing what your issues are. As I don't know how you set up your table and what your goals with this task is. Where this is being called, why are you needing the FindEntity etc.

  • Like 1
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...