size help

Ok so here’s the situation :slight_smile:
I am making a test code where you click ‘a’ to go to the selection screen to make a character. Then you can make the character bigger or smaller clicking the arrow keys up or down but you cant make Ralph too big, bigger than 2. So far everything’s good except when I tested it it only lets me click the arrow key once and then it won’t let me again.

Here’s my code:

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.gui.OnscreenText import OnscreenText
from direct.fsm import FSM
from direct.task import Task

import sys




class ralph(FSM.FSM, DirectObject):
	
	def __init__(self):
		
		self.ralphSize = 1
		
		self.loadModels()
		
		FSM.FSM.__init__(self,'avatarFSM')
		
		self.accept('arrow_up',self.sizeBigger)
		self.accept('arrow_down',self.sizeSmaller)
		
	def loadModels(self):
	
		self.ralph = loader.loadModel("models/ralph")
		self.ralph.reparentTo(render)
		self.ralph.setPosHpr(0, 16.51, -3.02, 0, 0, 0)
		self.ralph.setScale(self.ralphSize)
		
	def sizeBigger(self):
		
		if self.ralphSize < 2.0:
			self.ralph.setScale(self.ralphSize + .05)
			return Task.again
		else:
			return Task.cont
		#elif self.ralphSize < 2.0:	
		#	self.ralph.setScale(self.ralphSize + .05)
		#	return Task.cont
		#return Task.done

		
	def sizeSmaller(self):
		
		self.ralph.setScale(self.ralphSize - .05)
	


class world(DirectObject):
	
	def __init__(self):
		
	
		self.mode = 'title'
		
		self.title = OnscreenText(text="Press a for character selection",
			style=1, fg=(1,1,1,1),
			pos=(0.87,-0.95), scale = .07)
		
		self.accept("escape", sys.exit)
		base.disableMouse()
		self.acceptOnce("a", self.prnt)
		
		
	
	def prnt(self):
		
		self.title.destroy()
		
		self.mode = 'select'
		
		self.select = OnscreenText(text="Character selection",
			style=1, fg=(1,1,1,1),
			pos=(0.3,.9), scale = .1)
			
		ralph()
	
		

w = world()

run()

Thanks for looking at this :slight_smile:
I’m kinda new (I’m starting again…)

wait I got it…

if I change

 def sizeBigger(self):
      
      if self.ralphSize < 2.0:
         self.ralph.setScale(self.ralphSize + .05)
         return Task.again
      else:
         return Task.cont 

to

 def sizeBigger(self):
      
      if self.ralphSize < 2.0:
         self.ralph.setScale(self.ralphSize + .05)
         self.ralph.setScale(self.ralphSize)
         return Task.again
      else:
         return Task.cont 

:slight_smile: Or is there a better way?

Also how do I make it so when I hold it it goes?? :open_mouth:

You’re not changing your self.ralphSize variable when you do your actual scaling so what happens after you do your first arrow_up is that it always stays at a scale of 1.5 (1 + 0.5). You should modify the variable like so:

 def sizeBigger(self):
     
      if self.ralphSize < 2.0:
         self.ralphSize += 0.5
         self.ralph.setScale(self.ralphSize)
         return Task.again
      else:
         return Task.cont 

You should also do the same for your sizeSmaller function.

Thanks! :slight_smile:

But do you have any suggestions on making it so when I hold the button it keeps doing the action. Like in car games when you hold up it keeps going forward.

You need to use tasks for that. Also, your indentations are off standard. Thats fine if you’re keeping your code to yourself but a pain when others wish to edit your code. The standard number of spaces to use for indentation is 4.

class ralph(FSM.FSM, DirectObject):
   
   def __init__(self):
      
      self.loadModels()
      
      FSM.FSM.__init__(self,'avatarFSM')
      
      self.action = None
      self.ralphSize = 1
      
      self.accept('arrow_up', self.setAction, extraArgs=['up'])
      self.accept('arrow_down', self.setAction, extraArgs=['down'])
      
      self.accept('arrow_up-up', self.setAction)
      self.accept('arrow_down-up', self.setAction)
      
      taskMgr.add(self.sizeTask, "Resizer")
      
   def loadModels(self):
   
      self.ralph = loader.loadModel("ralph")
      self.ralph.reparentTo(render)
      self.ralph.setPosHpr(0, 16.51, -3.02, 0, 0, 0)
      
   def setAction(self, action=None):
       self.action = action

   def sizeTask(self, task):
      sizeSpeed = 2
      dt = globalClock.getDt()
      if self.action:
         if self.action == 'up' and self.ralphSize < 2.0:
            self.ralphSize += sizeSpeed * dt
         elif self.action == 'down':
            self.ralphSize -= sizeSpeed * dt
         print self.ralphSize
         self.ralph.setScale(self.ralphSize)
      return task.cont

Thanks it runs way smoother now.

I was originally using tab but I’ll do it your way. :slight_smile: